ram-output.c 8.14 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * ram-output.c  -  write objects to disk.
 *
 * This is a hack. It shouldn't be used except for debugging and as a
 * temporary substitute for what Willf|r is (or should:-) be doing.
 *
 * Written by ceder 1990-07-13. Rewritten 1990-08-31.
 * Some functions rewritten for speed by Inge Wallin.
 * (It worked - now saving is twice as fast.)
 */

Per Cederqvist's avatar
Per Cederqvist committed
12 13 14
static char *rcsid = "$Id: ram-output.c,v 0.2 1991/08/28 02:20:53 ceder Exp $";


Per Cederqvist's avatar
Per Cederqvist committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
#include <kom-types.h>
#include <stdio.h>
#include <sys/file.h>
#include <time.h>
#include "lyskomd.h"
#include "ram-output.h"
#include "com.h"
#include "isc.h"
#include "connections.h"
#include "prot-a-output.h"
#include "log.h"

void
foutput_person (FILE *fp,
		Person *person)
{
    foutput_string (fp, person->username);
    foutput_priv_bits (fp, person->privileges);
    foutput_personal_flags (fp, person->flags);

    foutput_text_list (fp, person->created_texts);
    foutput_mark_list (fp, person->marks);
    foutput_membership_list (fp, person->conferences);

    foutput_time(fp, person->last_login);

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu %lu %lu",
	     (u_long) person -> user_area,
	     (u_long) person -> total_time_present, /* This is not a time,
						     * but a number of seconds.
						     */
	     (u_long) person -> sessions,
	     (u_long) person -> created_lines,
	     (u_long) person -> created_bytes,
	     (u_long) person -> read_texts,
	     (u_long) person -> no_of_text_fetches,
	     (u_long) person -> created_persons,
	     (u_long) person -> created_confs);
}



void
foutput_conference (FILE *fp,
		    Conference *conf_c)
{
    foutput_string(fp, conf_c->name);
    foutput_member_list(fp, conf_c->members);
    foutput_text_list(fp, conf_c->texts);
    foutput_conf_type(fp, conf_c->type);

    foutput_time(fp, conf_c -> creation_time );
    foutput_time(fp, conf_c -> last_written );

    fprintf (fp, " %lu %lu %lu %lu %lu %lu %lu",
	     (u_long) conf_c -> creator,
	     (u_long) conf_c -> presentation,
	     (u_long) conf_c -> supervisor,
	     (u_long) conf_c -> permitted_submitters,
	     (u_long) conf_c -> super_conf,
	     (u_long) conf_c -> msg_of_day,
	     (u_long) conf_c -> nice);
}


void
foutput_text_stat(FILE *fp,
		  Text_stat *t_stat)
{
    int i;

    foutput_time(fp, t_stat->creation_time);
    
    foutput_ulong((u_long) t_stat->author, fp);
    foutput_ulong((u_long) t_stat->file_pos, fp);
    foutput_ulong((u_long) t_stat->no_of_lines, fp);
    foutput_ulong((u_long) t_stat->no_of_chars, fp);
    foutput_ulong((u_long) t_stat->no_of_marks, fp);
    foutput_ulong((u_long) t_stat->no_of_misc, fp);

    if ( t_stat->misc_items != NULL && t_stat->no_of_misc > 0 )
    {
	fputs(" {", fp);
	for ( i = 0; i < t_stat->no_of_misc; i++ )
	    foutput_misc_info(fp, t_stat->misc_items[ i ]);
	fputs(" }", fp);
    }
    else
	fputs(" *", fp);
}	


extern void
foutput_string(FILE *fp,
	       String str)
{
    foutput_ulong((u_long)str.len, fp);
    putc('H', fp);

    if (str.len)
	fwrite(str.string, str.len, 1, fp);
}

extern void
foutput_priv_bits(FILE *fp,
		  Priv_bits bits)
{
    putc(' ', fp);
    putc(bits.wheel + '0', fp);
    putc(bits.admin + '0', fp);
    putc(bits.statistic + '0', fp);
    putc(bits.create_pers + '0', fp);
    putc(bits.create_conf + '0', fp);
    putc(bits.change_name + '0', fp);
    putc(bits.flg7 + '0', fp);
    putc(bits.flg8 + '0', fp);
    putc(bits.flg9 + '0', fp);
    putc(bits.flg10 + '0', fp);
    putc(bits.flg11 + '0', fp);
    putc(bits.flg12 + '0', fp);
    putc(bits.flg13 + '0', fp);
    putc(bits.flg14 + '0', fp);
    putc(bits.flg15 + '0', fp);
    putc(bits.flg16 + '0', fp);
}

extern void
foutput_personal_flags(FILE *fp,
		       Personal_flags flags)
{
    putc(' ', fp);
    putc(flags.unread_is_secret + '0', fp);
    putc(flags.flg2 + '0', fp);
    putc(flags.flg3 + '0', fp);
    putc(flags.flg4 + '0', fp);
    putc(flags.flg5 + '0', fp);
    putc(flags.flg6 + '0', fp);
    putc(flags.flg7 + '0', fp);
    putc(flags.flg8 + '0', fp);
}	


void
foutput_text_list(FILE *fp,
		  Text_list text_list)
{
    int i;

    foutput_ulong((u_long)text_list.first_local_no, fp);
    foutput_ulong((u_long)text_list.no_of_texts, fp);

    if ( text_list.texts != NULL && text_list.no_of_texts > 0 )
    {
	fputs(" {", fp);
	for ( i = 0; i < text_list.no_of_texts; i++ )
	    foutput_ulong((u_long)text_list.texts[ i ], fp);
	fputs(" }", fp);
    }
    else
	fprintf(fp, " *");
}


void
foutput_mark_list(FILE *fp,
		  Mark_list mark_list)
{
    int i;

    fprintf(fp, " %lu", (u_long)mark_list.no_of_marks);

    if ( mark_list.marks != NULL && mark_list.no_of_marks > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < mark_list.no_of_marks; i++ )
	    foutput_mark(fp, mark_list.marks[ i ]);
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
    
}


extern void
foutput_mark(FILE *fp,
	     Mark mark)
{
    fprintf(fp, " %lu %lu", (u_long)mark.text_no, (u_long)mark.mark_type);
}

void
foutput_membership_list (FILE		* fp,
			 Membership_list  mlist)
{
    int i;
    
    fprintf(fp, " %lu", (u_long)mlist.no_of_confs);

    if ( mlist.confs != NULL && mlist.no_of_confs > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < mlist.no_of_confs; i++)
	    foutput_membership(fp, mlist.confs + i);
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}



void
foutput_membership(FILE *fp,
		   Membership *mship)
{
    int i;
    
    foutput_time(fp, mship->last_time_read );
    
    if ( mship->read_texts == NULL && mship->no_of_read != 0 )
    {
	log("foutput_membership(): no_of_read forced to 0"
	    " in someones membership in %lu.\n",
	    mship->conf_no);
	mship->no_of_read = 0;
    }
    
    fprintf(fp, " %lu %lu %lu %lu",
	    (u_long)mship->conf_no,
	    (u_long)mship->priority,
	    (u_long)mship->last_text_read,
	    (u_long)mship->no_of_read);
    
    if ( mship->read_texts != NULL && mship->no_of_read > 0)
    {
	fprintf(fp, " {");
	for ( i = 0; i < mship->no_of_read; i++)
	    fprintf(fp, " %lu", (u_long)mship->read_texts[ i ]);
	
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}

void
foutput_time(FILE *fp,
	     time_t clock)
{
    foutput_ulong((u_long) clock, fp);
}


extern void
foutput_member_list(FILE *fp,
		    Member_list m_list)
{
    int i;

    fprintf(fp, " %lu", (u_long)m_list.no_of_members);
    if ( m_list.members != NULL && m_list.no_of_members > 0 )
    {
	fprintf(fp, " {");
	for ( i = 0; i < m_list.no_of_members; i++ )
	    foutput_member(fp, m_list.members[ i ]);
	fprintf(fp, " }");
    }
    else
	fprintf(fp, " *");
}


void
foutput_member(FILE *fp,
	       Member member)
{
    fprintf(fp, " %lu", (u_long)member.member);
}

extern void
foutput_conf_type(FILE *fp,
		  Conf_type type)
{
    putc(' ', fp);
    putc(type.rd_prot + '0', fp);
    putc(type.original + '0', fp);
    putc(type.secret + '0', fp);
    putc(type.letter_box + '0', fp);
}


extern void
foutput_misc_info(FILE *fp, 
		  Misc_info misc)
{
    foutput_ulong((u_long)misc.type, fp);
    
    switch(misc.type)
    {
    case recpt:
	foutput_ulong((u_long)misc.datum.recipient, fp);
	break;
	
    case cc_recpt:
	foutput_ulong((u_long)misc.datum.cc_recipient, fp);
	break;
	
    case loc_no:
	foutput_ulong((u_long)misc.datum.local_no, fp);
	break;
	
    case rec_time:
	foutput_time(fp, misc.datum.received_at);
	break;
	
    case comm_to:
	foutput_ulong((u_long)misc.datum.comment_to, fp);
	break;
	
    case comm_in:
	foutput_ulong((u_long)misc.datum.commented_in, fp);
	break;
	
    case footn_to:
	foutput_ulong((u_long)misc.datum.footnote_to, fp);
	break;
	
    case footn_in:
	foutput_ulong((u_long)misc.datum.footnoted_in, fp);
	break;
	
    case sent_by:
	foutput_ulong((u_long)misc.datum.sender, fp);
	break;
	
    case sent_at:
	foutput_time(fp, misc.datum.sent_at);
	break;

#ifndef COMPILE_CHECKS
    default:
	restart_kom("prot_a_output_misc_info: Illegal misc\n");
#endif
    }
}


/* 
 * Output the unsigned long L in the fastest way possible to the file
 * FP. Ok, it's ugly, but it's fast (or is it?).
 */

void
foutput_ulong (u_long l,
	       FILE *fp)
{
    static char   buf[20];
    char         *cp;

    putc(' ', fp);
    if (l < 10)
	putc("0123456789"[l], fp);
    else {
	cp = buf + 19;
	while (l > 0) {
	    *cp-- = (l % 10) + '0';
	    l /= 10;
	}
	fwrite(cp + 1, buf + 19 - cp, 1, fp);
    }
}