home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / text / hyper / hsc_source.lha / hsc / source / hscprj / readprj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-19  |  11.6 KB  |  447 lines

  1. /*
  2.  * hscprj/readprj.c
  3.  *
  4.  * project managment input-routines for hsc
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 10-Sep-1996
  23.  * created: 10-Sep-1996
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <time.h>
  31.  
  32. #include "hsclib/ldebug.h"
  33. #include "hscprj/pdebug.h"
  34. #include "hscprj/pdefs.h"
  35.  
  36. #include "ugly/utypes.h"
  37. #include "ugly/dllist.h"
  38. #include "ugly/expstr.h"
  39. #include "ugly/umemory.h"
  40. #include "ugly/infile.h"
  41. #include "ugly/ustring.h"
  42.  
  43. #include "hscprj/document.h"
  44. #include "hscprj/project.h"
  45.  
  46. /*
  47.  * hsc_msg_project_corrupt: display error message about
  48.  * corrupt project file
  49.  */
  50. static VOID hsc_msg_project_corrupt(HSCPRJ * hp, STRPTR descr)
  51. {
  52.     hp->fatal = TRUE;
  53.     if (hp->CB_msg_corrupt_pf)
  54.         (*(hp->CB_msg_corrupt_pf)) (hp, descr);
  55. }
  56.  
  57. /* convert hex-digit to int */
  58. static int x2int(char c)
  59. {
  60.     if ((c >= '0') && (c <= '9'))
  61.         return (c - '0');
  62.     if ((c >= 'A') && (c <= 'F'))
  63.         return (c - 'A' + 10);
  64.     if ((c >= 'a') && (c <= 'f'))
  65.         return (c - 'a' + 10);
  66.     return (EOF);
  67. }
  68.  
  69. /*
  70.  * read_long: read (unsigned) long integer value
  71.  */
  72. static ULONG read_ulong(HSCPRJ * hp)
  73. {
  74.     INFILE *inpf = hp->inpf;
  75.     ULONG num = 0;
  76.     int ch;
  77.     int digit = EOF;
  78.  
  79.     do
  80.     {
  81.         ch = infgetc(inpf);
  82.         if (ch != ' ')
  83.         {
  84.             digit = x2int(ch);
  85.             if (digit == EOF)
  86.                 num = 0;
  87.             else
  88.                 num = (num << 4) + digit;
  89.         }
  90.         if (digit == EOF)
  91.             hsc_msg_project_corrupt(hp, "hex digit expected");
  92.     }
  93.     while ((digit != EOF) && (ch != ' '));
  94.  
  95.     if (digit == EOF)
  96.         num = 0;
  97.  
  98.     return (num);
  99. }
  100.  
  101. /*
  102.  * read_string:
  103.  *
  104.  * read string length, alloc mem and read string into it
  105.  */
  106. static STRPTR read_string(HSCPRJ * hp)
  107. {
  108.     STRPTR dest = NULL;
  109.  
  110.     ULONG len = read_ulong(hp);
  111.  
  112.     if (len)
  113.     {
  114.         ULONG i;
  115.         int ch = 'x';           /* dummy init */
  116.  
  117.         /* alloc mem */
  118.         dest = umalloc((size_t) (len + 1));
  119.         dest[len] = '\0';
  120.  
  121.         for (i = 0; ((i < len) && (ch != EOF)); i++)
  122.         {
  123.             ch = infgetc(hp->inpf);
  124.             if (ch != EOF)
  125.                 dest[i] = ch;
  126.             else
  127.                 hsc_msg_project_corrupt(hp, "string expected");
  128.         }
  129.         if (ch != EOF)
  130.             dest[len] = 0;
  131.         else
  132.         {
  133.             ufree(dest);
  134.             dest = NULL;
  135.         }
  136.     }
  137.     return (dest);
  138. }
  139.  
  140. /*
  141.  * read_caller: read file position
  142.  */
  143. static CALLER *read_caller(HSCPRJ * hp)
  144. {
  145.     CALLER *caller = NULL;
  146.     STRPTR callerid = infgetw(hp->inpf);
  147.  
  148.     if (callerid && !upstrcmp(callerid, ID_CALLER_STR))
  149.     {
  150.         int ch = infgetc(hp->inpf);     /* skip blank */
  151.  
  152.         if (ch == ' ')
  153.         {
  154.             STRPTR fname = read_string(hp);
  155.             caller = new_caller(fname, read_ulong(hp), read_ulong(hp));
  156.             ufreestr(fname);
  157.         }
  158.         else
  159.         {
  160.             hsc_msg_project_corrupt(hp, "blank expected");
  161.         }
  162.     }
  163.     else if (callerid && !strcmp(callerid, "\n"))
  164.     {
  165.         /* skip empty caller */
  166.         inungetcw(hp->inpf);
  167.         D(fprintf(stderr, DHP "skip EMPTY CALLER\n"));
  168.     }
  169.     else
  170.     {
  171.         hsc_msg_project_corrupt(hp, ID_CALLER_STR " expected");
  172.     }
  173.     return (caller);
  174. }
  175.  
  176. /*
  177.  * read_lf: read linefeed
  178.  */
  179. static BOOL read_lf(HSCPRJ * hp)
  180. {
  181.     int ch = infgetc(hp->inpf);
  182.     BOOL ok = TRUE;
  183.  
  184.     if (ch != '\n')
  185.     {
  186.         hsc_msg_project_corrupt(hp, "linefeed expected");
  187.         ok = FALSE;
  188.     }
  189.     return (ok);
  190. }
  191.  
  192. /*
  193.  * read command
  194.  *
  195.  * returns next command or NULL if eof reached
  196.  */
  197. static STRPTR read_command(HSCPRJ * hp)
  198. {
  199.     STRPTR command;
  200.  
  201.     do
  202.     {
  203.         command = infgetw(hp->inpf);
  204.     }
  205.     while (command && !strcmp(command, "\n"));
  206.  
  207.     if (command)
  208.     {
  209.         /* skip blanks */
  210.         infskip_ws(hp->inpf);
  211.         DP(fprintf(stderr, DHP "command `%s'\n", command));
  212.     }
  213.     else
  214.     {
  215.         DP(fprintf(stderr, DHP "command EOF\n"));
  216.     }
  217.  
  218.     return (command);
  219. }
  220.  
  221. /*
  222.  * read header
  223.  */
  224. static BOOL read_header(HSCPRJ * hp)
  225. {
  226.     STRARR fileid[1 + sizeof(FILEID_HSCPRJ)];
  227.     BOOL ok = FALSE;
  228.     STRPTR cmd = NULL;
  229.     size_t i;
  230.  
  231.     /* read fileid */
  232.     for (i = 0; i < strlen(FILEID_HSCPRJ); i++)
  233.     {
  234.         int ch = infgetc(hp->inpf);
  235.  
  236.         fileid[i] = (UBYTE) ch;
  237.     }
  238.     fileid[i] = '\0';
  239.  
  240.     DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  241.  
  242.     /* check fileid */
  243.     if (!strcmp(fileid, FILEID_HSCPRJ))
  244.     {
  245.         DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  246.         ok = read_lf(hp);
  247.     }
  248.     else
  249.     {
  250.         hsc_msg_project_corrupt(hp, "wrong file-id");
  251.     }
  252.  
  253.     if (ok)
  254.     {
  255.  
  256.         ok = FALSE;
  257.  
  258.         /* read version */
  259.         cmd = read_command(hp);
  260.  
  261.         /* check version */
  262.         if (cmd && !strcmp(cmd, LINE_VERSION_STR))
  263.         {
  264.             ULONG version = read_ulong(hp);
  265.  
  266.             DP(fprintf(stderr, DHP "version: %lu\n", version));
  267.  
  268.             if (version && (version <= 2))
  269.             {
  270.                 ok = read_lf(hp);
  271.             }
  272.             else
  273.             {
  274.                 hsc_msg_project_corrupt(hp, "wrong version");
  275.             }
  276.  
  277.         }
  278.         else
  279.         {
  280.             hsc_msg_project_corrupt(hp, "unknown version");
  281.         }
  282.  
  283.     }
  284.     return (ok);
  285. }
  286.  
  287. /*
  288.  * hsc_project_read_file
  289.  *
  290.  * read project file
  291.  *
  292.  * params: hp....HSCPRJ created using new_project()
  293.  *         inpf..input file opened using infopen();
  294.  *               this has to be opened and closed by
  295.  *               you outside this function.
  296.  */
  297. BOOL hsc_project_read_file(HSCPRJ * hp, INFILE * inpf)
  298. {
  299.     BOOL ok = FALSE;
  300.  
  301.     if (inpf)
  302.     {
  303.         hp->inpf = inpf;
  304.  
  305.         DP(fprintf(stderr, DHP "read project-file from `%s'\n",
  306.                      infget_fname(inpf)));
  307.  
  308.         if (read_header(hp))
  309.         {
  310.             HSCDOC *document = NULL;
  311.             STRPTR cmd = NULL;
  312.  
  313.             do
  314.             {
  315.                 cmd = read_command(hp);
  316.                 if (cmd)
  317.                 {
  318.                     if (!strcmp(cmd, LINE_REM_STR))
  319.                     {
  320.                         /* skip comment */
  321.                         int ch;
  322.                         DP(fprintf(stderr, DHP "comment `"));
  323.                         do
  324.                         {
  325.                             ch = infgetc(inpf);
  326.                             DP(
  327.                                     {
  328.                                     if (ch != '\n')
  329.                                     fprintf(stderr, "%c", ch);
  330.                                     }
  331.                             );
  332.                         }
  333.                         while ((ch != EOF) && (ch != '\n'));
  334.                         DP(fprintf(stderr, "'\n"));
  335.                     }
  336.                     else if (!strcmp(cmd, LINE_DOCUMENT_STR))
  337.                     {
  338.                         /* begin new DOCUMENT */
  339.                         STRPTR docname = read_string(hp);
  340.                         if (docname)
  341.                         {
  342.                             document = new_document(docname);
  343.                             app_dlnode(hp->documents, (APTR) document);
  344.                             /* free mem allocated by read_string() */
  345.                             ufree(docname);
  346.                         }
  347.                     }
  348.                     else if (!strcmp(cmd, LINE_SOURCE_STR))
  349.                     {
  350.                         /* assign SOURCE */
  351.                         if (document)
  352.                         {
  353.                             STRPTR sourcename = read_string(hp);
  354.                             if (sourcename)
  355.                             {
  356.                                 reallocstr(&(document->sourcename),
  357.                                            sourcename);
  358.                                 /* free mem allocated by read_string() */
  359.                                 ufree(sourcename);
  360.                             }
  361.                         }
  362.                         else
  363.                             hsc_msg_project_corrupt
  364.                                 (hp, LINE_SOURCE_STR " without "
  365.                                  LINE_DOCUMENT_STR);
  366.                     }
  367.                     else if (!strcmp(cmd, LINE_TITLE_STR))
  368.                     {
  369.                         /* assign TITLE */
  370.                         if (document)
  371.                         {
  372.                             STRPTR titlename = read_string(hp);
  373.                             if (titlename)
  374.                             {
  375.                                 set_estr(document->title, titlename);
  376.                                 /* free mem allocated by read_string() */
  377.                                 ufree(titlename);
  378.                             }
  379.                         }
  380.                         else
  381.                             hsc_msg_project_corrupt
  382.                                 (hp, LINE_TITLE_STR " without "
  383.                                  LINE_DOCUMENT_STR);
  384.                     }
  385.                     else if (!strcmp(cmd, LINE_ID_STR))
  386.                     {
  387.                         /* append new ID */
  388.                         if (document)
  389.                         {
  390.                             STRPTR idname = read_string(hp);
  391.                             if (idname)
  392.                             {
  393.                                 HSCIDD *iddef =
  394.                                 app_iddef(document, idname);
  395.                                 iddef->caller = read_caller(hp);
  396.                                 /* free mem allocated by read_string() */
  397.                                 ufree(idname);
  398.                             }
  399.                         }
  400.                         else
  401.                             hsc_msg_project_corrupt
  402.                                 (hp, LINE_ID_STR " without "
  403.                                  LINE_DOCUMENT_STR);
  404.  
  405.                     }
  406.                     else if (!strcmp(cmd, LINE_INCLUDE_STR))
  407.                     {
  408.                         /* store new INCLUDE */
  409.                         if (document)
  410.                         {
  411.                             STRPTR incname = read_string(hp);
  412.                             if (incname)
  413.                             {
  414.                                 HSCINC *inc =
  415.                                 app_include(document, incname);
  416.                                 inc->caller = read_caller(hp);
  417.                                 /* free mem allocated by read_string() */
  418.                                 ufree(incname);
  419.                             }
  420.                         }
  421.                         else
  422.                             hsc_msg_project_corrupt
  423.                                 (hp, LINE_INCLUDE_STR " without "
  424.                                  LINE_DOCUMENT_STR);
  425.                     }
  426.                     else
  427.                     {
  428.                         /* unknown command */
  429.                         hsc_msg_project_corrupt(hp, "unknown tag");
  430.                     }
  431.                 }
  432.                 else
  433.                 {
  434.                     DP(fprintf(stderr, DHP "EOF\n"));
  435.                 }
  436.             }
  437.             while (cmd && !hp->fatal);
  438.  
  439.             ok = !hp->fatal;
  440.         }
  441.         hp->inpf = NULL;
  442.     }
  443.  
  444.     return (ok);
  445. }
  446.  
  447.