Debian Codegraph @

gdbload.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* GDBLOAD.C -- Put data into an HP100LX database.
2 *
3 * usage: gdbload [-an] <database file> [<input file>]
4 *
5 * -a means add records to the database. The default is to replace the
6 * records in the database.
7 * -n means do not make a backup of the database before beginning. The
8 * default is to back up the database.
9 * <database file> is a 100LX database file (.GDB, .PDB, .NDB, or .WDB).
10 * Appointment book files (.ADB) are not supported (yet).
11 * <input file> is a file containing the records to add to the database.
12 * If not specified, standard input is used.
13 * The first line of the file must contain a list of field names,
14 * separated by commas and quoted with "" marks if they contain
15 * commas. Subsequent lines of the file represent records (one
16 * per line except that line breaks may occur within note fields),
17 * with the values of the fields in the record presented
18 * in the same order as the field names were on the first line.
19 * String fields must be quoted using "" if they contain commas
20 * or line breaks.
21 * Within strings, the usual C escape codes \r \n \\ \" \nnn and
22 * \xnn are understood. Dates should be encoded as YYYYMMDD, for
23 * example 19930730. Times should be encoded as HHMM, with HH in
24 * the range 00-23. Radio buttons and check boxes are considered
25 * off if the field is empty or contains 0, on otherwise.
26 *
27 * The database file written by gdbload will not be accepted
28 * immediately by the 100LX database engine. Rebuild the index by
29 * pressing F6 and selecting any "subset" (even the current one).
30 *
31 * Programming notes:
32 * There are lots of magic numbers in this code, which come from
33 * the document "Format of 100LX Databases" posted by Andrew J.
34 * Gryc of Hewlett-Packard to the comp.sys.palmtops newsgroup on
35 * 18 June 1993.
36 * I do not use structures in this code to map into the database format,
37 * because they are not portable across architectures with different
38 * byte orders.
39 *
40 * Steve Roth's disclaimer:
41 * These programs are released into the public domain and I place no
42 * restrictions on their use. I make no warranties or guarantees for
43 * these programs and you use them at your own risk. These programs are
44 * supplied by me personally and not by Hewlett-Packard Co., which
45 * incurs no obligations pertaining to them. (Please note that my work
46 * with HP has no connection with palmtops or PC products, and only
47 * information and resources available to the general public were used
48 * in writing these programs. Thanks to Andy Gryc for publishing the
49 * necessary information!)
50 *
51 * Arne Christensen's disclaimer:
52 * Read the above again except for the HP sentences :-)
53 *
54 * Revision History:
55 * 1.03 - Now accepts line breaks in string (note) fields.
56 * 1.02 - (Maintenance taken over by [email protected])
57 * Added handling of national characters.
58 * Removed compiler dependency (credit Charles Stroom)
59 * 1.01 - Fixed -n bug and added comments.
60 * 1.00 - Initial version.
61 *
62 * Written by:
63 * Steve Roth ([email protected])
64 *
65 * Currently maintained by:
66 * Arne Christensen ([email protected])
67 *
68 * 24 July 1995
69 */
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74
75 /* This define is needed for HP-UX systems, and probably not anywhere else
76 * I would guess. Comment it in if needed.
77 */
78 /*#define stricmp strcasecmp*/
79
80 #define MAXFIELDS 100 /* Max. fields in a database */
81
82 #if !defined(TRUE)
83 #define TRUE 1
84 #define FALSE 0
85 #endif
86
87 typedef unsigned char uchar;
88
89 int fAdd; /* Add to existing database? */
90 int fBackup; /* Backup existing database? */
91 int cFields = 0; /* # fields in database */
92 int cInFields = 0; /* # fields in input stream */
93 int mapFields[MAXFIELDS]; /* mapping from input stream to db */
94 int hiNote; /* First available note record # */
95 int hiData; /* First available data record # */
96 int relStart; /* Offset of relative data in record */
97 int cRecords; /* Number of records in output */
98 int lineNum = 0; /* Line number in input stream */
99 char * pszDatabase = NULL; /* Database file name */
100 char * pszInput = NULL; /* Input file name */
101 char szNew[BUFSIZ]; /* Database.NEW */
102 char szOld[BUFSIZ]; /* Database.BAK */
103 char szCategories[257]; /* Categories in database */
104 uchar dbhdr[29]; /* Buffer for database header */
105 uchar record[32769]; /* Buffer for a record */
106 uchar fields[MAXFIELDS][28]; /* Field definitions */
107 uchar * lineFields[MAXFIELDS]; /* Fields in a line of the input */
108 FILE * hfNew; /* New database handle */
109 FILE * hfOld; /* Old database handle */
110 FILE * hfIn; /* Input stream handle */
111
112 /* This routine exits the program with an error message. */
113 die(char * message)
114 {
115 fprintf(stderr, "gdbload(%d): %s\n", lineNum, message);
116 exit(1);
117 }
118
119 /* This routine exits the program with a usage message. */
120 usage()
121 {
122 fprintf(stderr,
123 "usage: gdbload [-an] <database file> [<input file>]\n");
124 exit(1);
125 }
126
127 /* This routine parses the command line arguments. */
128 void parseArgs(int argc,
129 char * argv[])
130 {
131 /* Switch defaults: */
132 fAdd = FALSE; /* Replace the contents of the db */
133 fBackup = TRUE; /* Back up the unchanged db */
134
135 for (argc--, argv++; argc; argc--, argv++)
136 { /* For each argument */
137 if (**argv == '-') /* Is it an option? */
138 for ((*argv)++; **argv; (*argv)++)
139 /* For each character in it */
140 switch (**argv)
141 {
142 case 'a': fAdd = TRUE; break;
143 case 'n': fBackup = FALSE; break;
144 default:
145 usage();
146 break;
147 }
148 else if (!pszDatabase) /* Is it the database name? */
149 pszDatabase = *argv;
150 else if (!pszInput) /* Is it the input file name? */
151 pszInput = *argv;
152 else
153 usage(); /* I don't know what it is! */
154 }
155 if (!pszDatabase) /* Did we get a database name? */
156 usage();
157 }
158
159 /* Generate the file names we need. */
160 void makeFileNames()
161 {
162 char * pch;
163
164 /* Work backwards through the database file name */
165 pch = strchr(pszDatabase, '\0');
166 while (pch > pszDatabase)
167 switch (*(--pch))
168 {
169 case '.':
170 /* Found a (.). Replace extension with .new and .bak
171 * and return.
172 */
173 *pch = '\0';
174 strcpy(szNew, pszDatabase);
175 strcpy(szOld, pszDatabase);
176 strcat(szNew, ".new");
177 strcat(szOld, ".bak");
178 *pch = '.';
179 pch = pszDatabase; /* breaks while loop */
180 break;
181
182 case '/':
183 case '\\':
184 /* Found a path separator. Add .new or .bak
185 * extension and return.
186 */
187 strcpy(szNew, pszDatabase);
188 strcpy(szOld, pszDatabase);
189 strcat(szNew, ".new");
190 strcat(szOld, ".bak");
191 pch = pszDatabase; /* breaks while loop */
192 break;
193 }
194
195 if (!*szNew) /* Did we find either one? */
196 {
197 strcpy(szNew, pszDatabase);
198 strcpy(szOld, pszDatabase);
199 strcat(szNew, ".new");
200 strcat(szOld, ".bak");
201 }
202 }
203
204 /* This array of structures is used to build the record index table at the
205 * end of the database file. The 32 array elements correspond to the 32
206 * record types in a database. For each, the array contains the location
207 * (file offset) and size of the first 32 records of that type, plus a
208 * link to additional, identical structures in case there are more than
209 * 32 records of that type.
210 */
211 struct index_s
212 {
213 long location[32];
214 int size[32];
215 struct index_s * next;
216 } Index[32];
217
218 /* This array contains the number of the highest record in use of each type,
219 * plus 1. (E.g. if the highest data record is #11, highIndex[11] == 12;
220 * if there are no note records, highIndex[9] == 0.)
221 */
222 int highIndex[32];
223
224 /* This routine adds an entry to the index table described above. */
225 void addIndexEntry(uchar * rechdr,
226 long location)
227 {
228 struct index_s * indexptr; /* Ptr to the index table entry */
229 int recnum; /* Record number */
230 int i;
231
232 indexptr = &Index[rechdr[0]]; /* Find the head of the index list */
233 recnum = rechdr[4] + rechdr[5] * 256;
234 if (recnum > highIndex[rechdr[0]] - 1)
235 /* We have a new high record number for this type. */
236 highIndex[rechdr[0]] = recnum + 1;
237
238 /* Scan through the linked list of index table entries, looking
239 * for the one that should contain this record. We may have to
240 * allocate and and new entries to the list.
241 */
242 for (i = 0; i < recnum / 32; i++)
243 if (indexptr->next)
244 indexptr = indexptr->next;
245 else
246 {
247 indexptr->next = (struct index_s *)
248 malloc(sizeof (struct index_s));
249 memset((char *)indexptr->next, 0,
250 sizeof(struct index_s));
251 indexptr = indexptr->next;
252 }
253
254 /* Store the location and size in the index table. */
255 indexptr->location[recnum % 32] = location;
256 indexptr->size[recnum % 32] = rechdr[2] + rechdr[3] * 256;
257 }
258
259 /* This routine writes the index table into the new database. */
260 void writeIndexEntries(void)
261 {
262 struct index_s * indexptr; /* Index pointer */
263 uchar rechdr[6]; /* Record header buffer */
264 uchar indexent[8]; /* Index entry */
265 uchar intbuf[2]; /* Buffer for integer write */
266 int reclen; /* Length of the index table */
267 int first; /* First record # of each type */
268 int i;
269 int j;
270
271 /* Count the number of records. Note that, for the purposes of the
272 * database manager, we don't care exactly how many records are in
273 * the file; what we want is the sum of the highest record numbers
274 * of each type.
275 */
276 cRecords = 0;
277 for (i = 0; i < 32; i++)
278 cRecords += highIndex[i];
279
280 /* Generate and write the record header for the index table. */
281 rechdr[0] = 31;
282 rechdr[1] = 2;
283 reclen = cRecords * 8 + 6;
284 rechdr[2] = reclen % 0x100;
285 rechdr[3] = reclen / 0x100;
286 rechdr[4] = 0;
287 rechdr[5] = 0;
288 if (1 != fwrite(rechdr, 6, 1, hfNew))
289 die("can't write index table header");
290
291 /* Write the index table itself. */
292 for (i = 0; i < 32; i++) /* For each record type */
293 {
294 indexptr = &Index[i];
295 for (j = 0; j < highIndex[i]; j++)
296 { /* For each record of that type */
297 if (j % 32 == 0 && j > 0)
298 indexptr = indexptr->next;
299
300 /* Generate and write the index table entry. */
301 indexent[0] = indexptr->size[j%32] % 0x100;
302 indexent[1] = indexptr->size[j%32] / 0x100;
303 indexent[2] = -1;
304 indexent[3] = -1;
305 if (indexptr->location[j%32])
306 indexent[4] = 0;
307 else /* Record was deleted */
308 indexent[4] = 0xc0;
309 indexent[5] = indexptr->location[j%32] % 0x100;
310 indexent[6] = indexptr->location[j%32] / 0x100 % 0x100;
311 indexent[7] = indexptr->location[j%32] / 0x10000;
312 if (1 != fwrite(indexent, 8, 1, hfNew))
313 die("can't write index table entry");
314 }
315 }
316
317 /* Now generate and write the type first table. */
318 first = 0;
319 for (i = 0; i < 32; i++)
320 {
321 intbuf[0] = first % 0x100;
322 intbuf[1] = first / 0x100;
323 if (1 != fwrite(intbuf, 2, 1, hfNew))
324 die("can't write type first table entry");
325 first += highIndex[i];
326 }
327 }
328
329 /* This routine copies a record from the old database to the new, unchanged. */
330 void CopyRecord(uchar * rechdr,
331 uchar * record,
332 int reclen)
333 {
334 int recnum;
335
336 if (rechdr[1] & 0x01) /* Record was deleted */
337 return;
338
339 /* Save the record's information in our index table. */
340 addIndexEntry(rechdr, ftell(hfNew));
341
342 /* Write it out. */
343 if (1 != fwrite(rechdr, 6, 1, hfNew) ||
344 (reclen != 0 && 1 != fwrite(record, reclen, 1, hfNew)))
345 die("can't write record");
346
347 /* Update the available data and note record numbers */
348 recnum = rechdr[5] * 256 + rechdr[4];
349 if (rechdr[0] == 9 && recnum > hiNote-1)
350 hiNote = recnum+1;
351 if (rechdr[0] == 11 && recnum > hiData-1)
352 hiData = recnum+1;
353 }
354
355 /* This routine copies a record from the old database to the new, but only
356 * if -a was specified.
357 */
358 void CopyIfAdd(uchar * rechdr,
359 uchar * record,
360 int reclen)
361 {
362 if (fAdd)
363 CopyRecord(rechdr, record, reclen);
364 }
365
366 /* This routine saves the categories of the old database if -a was specified,
367 * and ignores them otherwise.
368 */
369 void Categories(uchar * rechdr,
370 uchar * record,
371 int reclen)
372 {
373 if (fAdd)
374 {
375 szCategories[0] = ';';
376 strcpy(szCategories+1, (char *)record);
377 strcat(szCategories, ";");
378 }
379 }
380
381 /* This routine copies a field definition from the old database to the new,
382 * and saves it in memory for later use also.
383 */
384 void DoFieldDef(uchar * rechdr,
385 uchar * record,
386 int reclen)
387 {
388 int ptr; /* Data offset for field */
389
390 /* Save the field def. */
391 memcpy(fields[cFields], record, reclen);
392 ptr = record[3] * 256 + record[2] + 2;
393 if (!(record[4] & 0x80)) /* Field contains data? */
394 {
395 /* Save the beginning of relative data in relStart. */
396 if (record[0] == 8 && !(record[4] & 0x20))
397 /* Date fields take 3 bytes */
398 ptr++;
399 if ((record[0] == 0 || record[0] == 9) && !(record[4] & 0x20))
400 /* Byte booleans and radio buttons take 1 byte */
401 ptr--;
402 if (ptr > relStart)
403 /* The absolute data for this field ends later than
404 * any other we have looked at. Save this new end
405 * point.
406 */
407 relStart = ptr;
408 }
409 cFields++;
410 CopyRecord(rechdr, record, reclen);
411 }
412
413 /* This routine writes an empty viewpoint table to the new database for
414 * every viewpoint table found in the old.
415 */
416 void ViewTable(uchar * rechdr,
417 uchar * record,
418 int reclen)
419 {
420 rechdr[2] = 6+2;
421 rechdr[3] = 0;
422 record[0] = 0xff;
423 record[1] = 0xff;
424 addIndexEntry(rechdr, ftell(hfNew));
425 if (1 != fwrite(rechdr, 6, 1, hfNew) ||
426 1 != fwrite(record, 2, 1, hfNew))
427 die("can't write viewpoint table");
428 }
429
430 /* This array contains the routines to be used to handle each type of record
431 * in the database during a copy operation.
432 */
433 typedef void (*RecordHandler)(uchar * rechdr,
434 uchar * record,
435 int reclen);
436 RecordHandler RecordHandlers[32] =
437 {
438 CopyRecord, CopyRecord, CopyRecord, CopyRecord,
439 CopyRecord, Categories, DoFieldDef, CopyRecord,
440 CopyRecord, CopyIfAdd, ViewTable, CopyIfAdd,
441 CopyRecord, CopyRecord, CopyRecord, CopyRecord,
442 CopyRecord, CopyRecord, CopyRecord, CopyRecord,
443 CopyRecord, CopyRecord, CopyRecord, CopyRecord,
444 CopyRecord, CopyRecord, CopyRecord, CopyRecord,
445 CopyRecord, CopyRecord, CopyRecord, CopyRecord
446 };
447
448 /* This routine copies the appropriate records of the old database to the
449 * new.
450 */
451 void copyDatabase()
452 {
453 uchar rechdr[6]; /* Record header buffer */
454 int reclen; /* Record length */
455
456 if (!(hfOld = fopen(pszDatabase, "rb")) &&
457 !(hfOld = fopen(pszDatabase, "r")))
458 die("can't read database");
459 if (!(hfNew = fopen(szNew, "wb")) &&
460 !(hfNew = fopen(szNew, "w")))
461 die("can't create .new file");
462
463 /* Copy the database header from old to new. Note that the database
464 * header in the new database will be overwritten once we know the
465 * number of records and such. For now, it's just a placeholder.
466 * However, the magic ViewPtHash value is important to read, we would
467 * not know how to compute it otherwise.
468 */
469 if (1 != fread(dbhdr, 29, 1, hfOld))
470 die("can't read database header");
471 addIndexEntry(dbhdr+4, 4);
472 if (1 != fwrite(dbhdr, 29, 1, hfNew))
473 die("can't write database header");
474
475 /* Various checks for valid data. */
476 if (memcmp(dbhdr, "hcD", 4))
477 die("not a database file");
478 if (dbhdr[11] != 1 || dbhdr[10] != 2)
479 die ("wrong version of database format");
480 if (dbhdr[12] == '2')
481 die ("can't handle appointment book databases");
482 if (dbhdr[12] == 'W')
483 die ("can't handle world time databases");
484
485 /* Scan through the old database, handling each record we find
486 * according to the handler table above. Stop when we reach the
487 * index table or end of file.
488 */
489 *szCategories = '\0';
490 do
491 {
492 if (1 != fread(rechdr, 6, 1, hfOld))
493 break; /* Can't read, we're done */
494 if (rechdr[0] == 31)
495 break; /* Index table, we're done */
496
497 /* Read the record and process it. */
498 reclen = rechdr[3] * 256 + rechdr[2] - 6;
499 if (reclen != 0 && 1 != fread(record, reclen, 1, hfOld))
500 die("read error in middle of record");
501 (*RecordHandlers[rechdr[0]])(rechdr, record, reclen);
502 } while (1);
503
504 /* Did we find any fields? any categories? */
505 if (cFields == 0)
506 die("no FIELDDEF records found in database");
507 if (!*szCategories)
508 strcpy(szCategories, ";;");
509 }
510
511 /* This routine reads a line of input and breaks it into fields. */
512 void readLine()
513 {
514 int field = 0; /* Field number being read */
515 int matchQuote = FALSE; /* Inside quotation marks */
516 int fDone = FALSE; /* Finished reading */
517 uchar ch;
518 uchar * pch = record;
519
520 /* Wipe out any old data. */
521 memset(lineFields, 0, sizeof(lineFields));
522
523 /* Forgive the goto...
524 * We want to skip blank and comment lines.
525 */
526 nextline:
527 while ((ch = fgetc(hfIn)) == '\r' || ch == '\n')
528 if (ch == '\n')
529 lineNum++;
530 if (ch == ';')
531 {
532 while ((ch = fgetc(hfIn)) != '\n' && !feof(hfIn))
533 ;
534 lineNum++;
535 goto nextline;
536 }
537
538 do /* For all characters in input */
539 {
540 if (feof(hfIn)) /* End of file? */
541 {
542 lineFields[0] = NULL;
543 return;
544 }
545
546 switch (ch)
547 {
548 default: /* Normally, copy chars into buffer */
549 *pch++ = ch;
550 break;
551
552 case '"': /* Quote marks toggle the flag */
553 matchQuote = ! matchQuote;
554 break;
555
556 case ',': /* Commas skip to next field unless */
557 if (matchQuote) /* they are inside quotes */
558 *pch++ = ch;
559 else
560 {
561 *pch = '\0';
562 lineFields[field++] = (uchar *)strdup(record);
563 pch = record;
564 }
565 break;
566
567 case '\\': /* Backslashes handled specially */
568 ch = fgetc(hfIn);
569 switch (ch) /* Look at next character */
570 {
571 case '\r': /* Skip carriage returns */
572 fgetc(hfIn);
573 case '\n': /* Line feeds are ignored */
574 lineNum++;
575 break;
576
577 case 'n': /* \n is a newline */
578 *pch++ = '\n';
579 break;
580
581 case 'r': /* \r is a carriage return */
582 *pch++ = '\r';
583 break;
584
585 case 'x': /* \xnn is a hexadecimal char */
586 {
587 uchar ch2;
588 ch = fgetc(hfIn) - '0';
589 ch2 = fgetc(hfIn) - '0';
590 if (ch > 9)
591 ch -= 'A' - '0' + 10;
592 if (ch > 9)
593 ch -= 'a' - 'A';
594 if (ch2 > 9)
595 ch2 -= 'A' - '0' + 10;
596 if (ch2 > 9)
597 ch2 -= 'a' - 'A';
598 *pch++ = ch * 16 + ch2;
599 break;
600 }
601
602 case '0': case '1': case '2': case '3':
603 case '4': case '5': case '6': case '7':
604 /* \nnn is an octal char */
605 {
606 uchar chstr_1, chstr_2;
607 chstr_1 = fgetc(hfIn);
608 chstr_2 = fgetc(hfIn);
609 *pch++ = 64 * (ch - '0') +
610 8 * (chstr_1 - '0') +
611 (chstr_2 - '0');
612 break;
613 }
614
615 default: /* Anything else is copied to buffer */
616 *pch++ = ch;
617 }
618 break;
619
620 case '\r': /* Skip carriage returns */
621 fgetc(hfIn);
622 case '\n': /* Newlines end the record */
623 lineNum++;
624 if (matchQuote)
625 {
626 *pch++ = '\r';
627 *pch++ = '\n';
628 }
629 else
630 {
631 *pch = '\0';
632 lineFields[field++] = (uchar *)strdup(record);
633 pch = record;
634 fDone = TRUE;
635 }
636 break;
637 }
638 ch = fgetc(hfIn);
639 } while (!fDone);
640 ungetc(ch, hfIn); /* Put back the last character */
641 lineFields[field] = NULL; /* Mark end of the record */
642 }
643
644 /* This routine compares two field names and returns TRUE if they match. */
645 int matchField(uchar * name1,
646 uchar * name2)
647 {
648 char namebuf1[21],
649 namebuf2[21],
650 * pch;
651
652 /* Copy the field names into local buffers */
653 strncpy(namebuf1, (char *)name1, 20);
654 strncpy(namebuf2, (char *)name2, 20);
655 namebuf1[20] = namebuf2[20] = '\0';
656
657 /* Remove &s from the field names */
658 while (pch = strchr(namebuf1, '&'))
659 memmove(pch, pch+1, strlen(pch));
660 while (pch = strchr(namebuf2, '&'))
661 memmove(pch, pch+1, strlen(pch));
662
663 /* Return a case-insensitive comparison of the result. */
664 return (!stricmp(namebuf1, namebuf2));
665 }
666
667 /* This routine reads the line of field names and compares it with the
668 * field definitions in the database.
669 */
670 void readFields()
671 {
672 int i;
673
674 /* First open the input file. */
675 if (!pszInput)
676 hfIn = stdin;
677 else
678 if (!(hfIn = fopen(pszInput, "rt")) &&
679 !(hfIn = fopen(pszInput, "r")))
680 die("can't open input file");
681 memset(mapFields, -1, sizeof(mapFields));
682
683 /* Read the line of field names */
684 readLine();
685 while (lineFields[cInFields]) /* For each field name listed */
686 {
687 for (i = 0; i < cFields; i++)
688 /* For each field in the database */
689 if (matchField(lineFields[cInFields], fields[i]+7))
690 { /* Do they match? */
691 mapFields[cInFields] = i;
692 break;
693 }
694 if (mapFields[cInFields] == -1)
695 { /* Didn't find a match for this field name */
696 fprintf(stderr, "gdbload(%d): can't match field '%s'\n",
697 lineNum, lineFields[cInFields]);
698 exit(1);
699 }
700 if (fields[mapFields[cInFields]][4] & 0x80)
701 { /* Does this field contain data? */
702 fprintf(stderr, "gdbload(%d): field '%s' has no data\n",
703 lineNum, lineFields[cInFields]);
704 exit(1);
705 }
706 if (fields[mapFields[cInFields]][0] == 16)
707 { /* Is this field app-specific? */
708 fprintf(stderr, "gdbload(%d): field '%s' is not "
709 "handled by gdbload\n", lineNum,
710 lineFields[cInFields]);
711 exit(1);
712 }
713 free(lineFields[cInFields++]);
714 }
715 if (cInFields == 0) /* Did we find any fields? */
716 die("no field mappings found");
717 }
718
719 /* This routine adds a note to the database. The record number of the
720 * created note is returned.
721 */
722 int addNote(uchar * line,
723 uchar * fielddef)
724 {
725 uchar rechdr[6]; /* Record header buffer */
726 int reclen = strlen((char *)line);
727 /* Size of note */
728
729 /* Fill in record header */
730 rechdr[0] = 9;
731 rechdr[1] = 2;
732 rechdr[2] = (reclen+6) % 0x100;
733 rechdr[3] = (reclen+6) / 0x100;
734 rechdr[4] = hiNote % 0x100;
735 rechdr[5] = hiNote / 0x100;
736 hiNote++;
737
738 /* Write index entry and record. */
739 addIndexEntry(rechdr, ftell(hfNew));
740 fwrite(rechdr, 6, 1, hfNew);
741 fwrite(line, reclen, 1, hfNew);
742
743 /* Return note record number. */
744 return hiNote-1;
745 }
746
747 /* This routine adds the categories in a data record to the list of
748 * categories used in the database.
749 */
750 void addCategories(uchar * line)
751 {
752 static char buf[256]; /* Buffer for category name */
753 char * end;
754
755 if (!*line) /* Any categories in this record? */
756 return;
757 do
758 { /* For each category in the record */
759 end = strchr((char *)line, ';');
760 if (end)
761 *end = '\0';
762 sprintf(buf, ";%s;", line); /* Copy to buffer */
763 if (!strstr(szCategories, buf))
764 /* Is it already on the list? */
765 if (strlen(szCategories) + strlen(buf) - 2 > 256)
766 /* Would it overflow the list? */
767 die("too many categories");
768 else
769 if (!strcmp(szCategories, ";;"))
770 /* Is there anything on the list? */
771 strcpy(szCategories, buf);
772 else
773 strcat(szCategories, buf+1);
774 if (end)
775 {
776 *end = ';';
777 line = (uchar *)end+1;
778 }
779 } while (end && *line);
780 }
781
782 /* This routine adds a field of a record to the record buffer. */
783 void DoField(uchar * line,
784 uchar * fielddef,
785 int note,
786 int * endrec)
787 {
788 int offset = fielddef[2] + fielddef[3] * 256;
789
790 switch (*fielddef)
791 {
792 case 6: /* Category field */
793 addCategories(line); /* Add to categ list */
794 /* FALL-THROUGH */
795 case 2: /* String fields of various types */
796 case 3:
797 case 4:
798 case 5:
799 case 13:
800 case 15:
801 if (!*line) /* Is there any text? */
802 /* No, point to the null byte at the beginning of
803 * the relative data.
804 */
805 record[offset] = relStart % 0x100,
806 record[offset+1] = relStart / 0x100;
807 else
808 {
809 /* Yes, so point to the end of the relative data
810 * area, and copy the string there.
811 */
812 record[offset] = *endrec % 0x100;
813 record[offset+1] = *endrec / 0x100;
814 strcpy((char *)record+*endrec, (char *)line);
815 *endrec += strlen((char *)line) + 1;
816 }
817 break;
818
819 case 10: /* Note field */
820 if (!*line) /* Is there any text? */
821 /* No, so put a -1 for the note record number */
822 record[offset] = -1,
823 record[offset+1] = -1;
824 else
825 /* Yes, so put in the note record number */
826 record[offset] = note % 0x100,
827 record[offset+1] = note / 0x100;
828 break;
829
830 case 9: /* Radio button */
831 if (*line && strcmp((char *)line, "0"))
832 /* If anything other than 0 is in the field, set
833 * the radio button value.
834 */
835 record[offset] = fielddef[5];
836 break;
837
838 case 8: /* Date field */
839 {
840 long date = atol((char *)line);
841 int year = date / 10000;
842 int month = date / 100 % 100;
843 int day = date % 100;
844
845 if (!*line) /* Is there any text? */
846 {
847 /* No, put in a null date. */
848 record[offset] =
849 record[offset+1] =
850 record[offset+2] = -1;
851 break;
852 }
853
854 /* Check the date for validity. */
855 if (year < 1900 || year > 2099 ||
856 month < 1 || month > 12 ||
857 day < 1 || day > 31)
858 {
859 fprintf(stderr, "gdbdump(%d): %s is not a valid date\n",
860 lineNum, line);
861 exit(1);
862 }
863
864 /* Put in the date. */
865 record[offset] = year - 1900;
866 record[offset+1] = month - 1;
867 record[offset+2] = day - 1;
868 break;
869 }
870
871 case 7: /* Time */
872 {
873 int time = atoi((char *)line);
874 int hour = time / 100;
875 int minutes = time % 100;
876
877 if (!*line) /* Is there any text? */
878 {
879 /* No, so put in a null time */
880 record[offset] = 0;
881 record[offset+1] = 0x80;
882 break;
883 }
884
885 /* Check the time for validity */
886 if (hour < 0 || hour > 23 ||
887 minutes < 0 || minutes > 59)
888 {
889 fprintf(stderr, "gdbdump(%d): %s is not a valid time\n",
890 lineNum, line);
891 exit(1);
892 }
893
894 /* Put in the time */
895 time = hour * 60 + minutes;
896 record[offset] = time % 0x100;
897 record[offset+1] = time / 0x100;
898 break;
899 }
900
901 case 0: /* Bit in a byte (checkbox) */
902 case 1: /* Bit in a word (checkbox) */
903 {
904 int word = record[offset] + record[offset+1] * 256;
905 int mask = fielddef[5] + fielddef[6] * 256;
906
907 /* Is there anything other than 0 in the text? */
908 if (*line && strcmp((char *)line, "0"))
909 /* Yes, set the bit. */
910 word |= mask;
911 else
912 /* No, clear it. */
913 word &= ~mask;
914 record[offset] = word % 0x100;
915 record[offset+1] = word / 0x100;
916 break;
917 }
918 }
919 }
920
921 /* This routine adds the input records to the database. */
922 void addRecords()
923 {
924 uchar rechdr[6]; /* Record header buffer */
925 int endrec; /* Offset of first unused by in rec */
926 int note; /* Record # of associated note */
927 int i;
928
929 do
930 { /* For all records */
931 readLine();
932 if (!lineFields[0]) /* End of file? */
933 return;
934
935 /* Check for correct number of fields on line. */
936 if (!lineFields[cInFields-1])
937 die("not enough fields specified");
938 if (cInFields < MAXFIELDS && lineFields[cFields])
939 die("too many fields specified");
940
941 /* Does the record have a note? */
942 for (i = 0; i < cInFields; i++)
943 if (fields[mapFields[i]][0] == 10 &&
944 lineFields[i][0])
945 /* Yes. Add it to database. */
946 note = addNote(lineFields[i],
947 fields[mapFields[i]]);
948
949 /* Now do the record */
950 endrec = relStart + 1;
951 memset(record, 0, relStart+1);
952 for (i = 0; i < cInFields; i++)
953 {
954 DoField(lineFields[i], fields[mapFields[i]], note,
955 &endrec);
956 free(lineFields[i]);
957 }
958
959 /* Generate the data record header */
960 rechdr[0] = 11;
961 rechdr[1] = 2;
962 rechdr[2] = (endrec + 6) % 0x100;
963 rechdr[3] = (endrec + 6) / 0x100;
964 rechdr[4] = hiData % 0x100;
965 rechdr[5] = hiData / 0x100;
966 hiData++;
967
968 /* Write the index entry and the record. */
969 addIndexEntry(rechdr, ftell(hfNew));
970 fwrite(rechdr, 6, 1, hfNew);
971 fwrite(record, endrec, 1, hfNew);
972 } while(1);
973 }
974
975 /* This routine writes out the database header, the categories record,
976 * and the index table.
977 */
978 void writeHeader()
979 {
980 uchar rechdr[6]; /* Record header buffer */
981 int reclen; /* Record length */
982 long index; /* Offset of index in file */
983
984 /* Write out categories record. */
985 rechdr[0] = 5;
986 rechdr[1] = 2;
987 reclen = strlen(szCategories) - 2 /* extra semicolons */
988 + 1 /* null terminator */
989 + 6 /* record header */ ;
990 rechdr[2] = reclen % 0x100;
991 rechdr[3] = reclen / 0x100;
992 rechdr[4] = 0;
993 rechdr[5] = 0;
994 addIndexEntry(rechdr, ftell(hfNew));
995 fwrite(rechdr, 6, 1, hfNew);
996 fwrite(szCategories + 1, strlen (szCategories) - 2, 1, hfNew);
997 fwrite(rechdr+5, 1, 1, hfNew);
998
999 /* Update the database header with the location of the index table. */
1000 index = ftell(hfNew);
1001 dbhdr[18] = index % 0x100;
1002 dbhdr[19] = index / 0x100 % 0x100;
1003 dbhdr[20] = index / 0x10000 % 0x100;
1004 dbhdr[21] = index / 0x1000000;
1005 writeIndexEntries();
1006
1007 /* Update the database header and write it out. */
1008 dbhdr[13] |= 0x02; /* Database has been modified */
1009 dbhdr[16] = cRecords % 0x100;
1010 dbhdr[17] = cRecords / 0x100;
1011 fseek(hfNew, 0, SEEK_SET);
1012 fwrite(dbhdr, 29, 1, hfNew);
1013 }
1014
1015 /* Main program. */
1016 main(int argc,
1017 char * argv[])
1018 {
1019 parseArgs(argc, argv);
1020 makeFileNames();
1021 copyDatabase();
1022 readFields();
1023 addRecords();
1024 writeHeader();
1025 fclose(hfIn);
1026 fclose(hfOld);
1027 fclose(hfNew);
1028 unlink(szOld);
1029 rename(pszDatabase, szOld);
1030 rename(szNew, pszDatabase);
1031 if (!fBackup)
1032 unlink(szOld);
1033 exit(0);
1034 }