home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 345_01 / tlcresw.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  7KB  |  252 lines

  1. /* TLCRESW.C - "The Last Cross-referencer" - C Reserved word routines    */
  2. /*    Last Modified:    07/02/89                                            */
  3.  
  4. /*
  5. ---------------------------------------------------------------------
  6. Copyright (c) 1987-1989, Eduard Schwan Programs [esp] - All rights reserved
  7. TLC (The Last C-Cross-Referencer) and TLP (same, but for Pascal) are
  8. Cross-Reference Generators crafted and shot into the Public Domain by
  9. Eduard Schwan.  The source code and executable program may be freely
  10. distributed as long as the copyright/author notices remain intact, and
  11. it is not used in part or whole as the basis of a commercial product.
  12. Any comments, bug-fixes, or enhancements are welcome.
  13. Also, if you find TLC and it's source code useful, a contribution of
  14. $20 (check/money order) is encouraged!  Hopefully we will all see more
  15. source code distributed!
  16.     Eduard Schwan, 1112 Oceanic Drive, Encinitas, Calif. 92024
  17. ---------------------------------------------------------------------
  18. */
  19.  
  20. /*
  21. HEADER:        The Last Cross-Referencer;
  22. TITLE:        TLC/TLP - The Last Cross-Referencer;
  23. VERSION:    1.01;
  24.  
  25. DESCRIPTION: "TLC/TLP.
  26.             C Reserved word routines";
  27.  
  28. KEYWORDS:    Utility, Cross-reference, C, Pascal, Apple, Macintosh, APW, Aztec;
  29. SYSTEM:        Macintosh MPW, v3.0;
  30. FILENAME:    TLCRESW.C;
  31. WARNINGS:    "Has not yet been ported to MS-DOS.
  32.             Shareware, $20 Check/Money Order suggested.";
  33.  
  34. SEE-ALSO:    README.TLC,TLCHELP.DOC,TLPHELP.DOC;
  35. AUTHORS:    Eduard Schwan;
  36. COMPILERS:    AZTEC C65 v3.2b, APPLEIIGS APW C v1.0, APPLE MACINTOSH MPW C v3.0;
  37. */
  38.  
  39.  
  40. /*------------------------ include files -------------------------*/
  41.  
  42. #include    <stdio.h>
  43. #include    <errno.h>
  44. #include    "tlc.h"
  45.  
  46.  
  47. /*--------------------- external declarations --------------------*/
  48.  
  49. #include    "tlc.ext"
  50.  
  51. extern    char*    fgets();
  52. extern    VOID    init_scanner();
  53. extern    int     get_token();
  54. extern    FILE    *open_text_file();
  55. extern    int     close_text_file();
  56.  
  57.  
  58. /*------------------------ static variables -----------------------*/
  59.  
  60. static    pos_int line_num;
  61.  
  62.  
  63. /*=====================[ out_of_mem ]======================*/
  64.  
  65. static VOID out_of_mem(resword)
  66. char    *resword;
  67.  
  68.     { /* out_of_mem */
  69.     fprintf(stderr,
  70.         "out of memory adding '%s' on line %d of ResWordFile '%s'\n",
  71.         resword, line_num, file_rec.resw_fname);
  72.     exit(BAD_EXIT);
  73.     } /* out_of_mem */
  74.  
  75.  
  76. /*====================[ is_resword ]=======================*/
  77.  
  78. int is_resword(resword)
  79. char *    resword;
  80.  
  81.     { /* is_resword() */
  82.     int                 found;
  83.     int                 direction;
  84.     resw_list_type *    resw_ptr;
  85.  
  86.     found = FALSE;
  87.     resw_ptr = resw_rec.resw_list;
  88.     while (!found && resw_ptr!=NULL)
  89.      {
  90.         direction = strncmp(resword, resw_ptr->resword,
  91.                              parm_rec.symbol_size);
  92.         if (direction<0)
  93.             direction=-1;
  94.         else if (direction>0)
  95.             direction=1;
  96. /*
  97. debug(printf("is_resw(%s:%d):'%s'=%d\n",resword,parm_rec.symbol_size,resw_ptr->resword,direction);)
  98. */
  99.         switch (direction)
  100.             {
  101.             case  0: /* found! */
  102.                     found = TRUE;/*DEBUG:return token_type in rec later*/
  103.                     resw_ptr->occurrances++;
  104.                     break;
  105.             case -1: /* too high, go left */
  106.                     resw_ptr = resw_ptr->left;
  107.                     break;
  108.             case  1: /* too low, go right */
  109.             default:
  110.                     resw_ptr = resw_ptr->right;
  111.                     break;
  112.             } /*switch*/
  113.         } /*while*/
  114.  
  115.     return(found);
  116.  
  117.     } /* is_resword() */
  118.  
  119.  
  120.  
  121. /*====================[ add_resword ]======================*/
  122.  
  123. static resw_list_type * add_resword(resword, list_node)
  124. char            *resword;
  125. resw_list_type    *list_node;
  126.  
  127.     { /* add_resword */
  128.  
  129.     if (list_node == NULL)
  130.         { /* end of branch, add new node */
  131.         list_node = (resw_list_type*) malloc(sizeof(resw_list_type));
  132.         if (list_node == NULL)
  133.             { /* couldn't create record from heap, error exit */
  134.             out_of_mem(resword);
  135.             }
  136.         /*
  137.         initialize new node fields
  138.         */
  139.         list_node->left     = NULL;
  140.         list_node->right    = NULL;
  141.         list_node->resword    = malloc(strlen(resword)+1);
  142.         list_node->occurrances = 0;
  143.         if (list_node->resword == NULL)
  144.             { /* couldn't create record from heap, error exit */
  145.             out_of_mem(resword);
  146.             }
  147.         strcpy(list_node->resword, resword);
  148.         } /* end of branch */
  149.     else
  150.         { /* traverse left or right sub-tree of this node */
  151.         if (strcmp(resword, list_node->resword) < 0)
  152.             { /* resword lower, go left */
  153.             list_node->left = add_resword(resword, list_node->left);
  154.             }
  155.         else
  156.             { /* resword higher, go right */
  157.             list_node->right = add_resword(resword, list_node->right);
  158.             }
  159.         } /* else traverse */
  160.     /*
  161.     return this node to the caller
  162.     */
  163.     return (list_node);
  164.     } /* add_resword */
  165.  
  166.  
  167. /*=================[ handle_resword_file ]===================*/
  168.  
  169. VOID handle_resw_file()
  170.  
  171.     { /* handle_resw_file() */
  172.     int     error;
  173.     boolean end_of_file;
  174.     FILE    *fp;
  175.  
  176.     if (verbose)
  177.         {
  178.         fprintf(stderr, "Reading ResWordsFile '%s'\n",
  179.                 file_rec.resw_fname);
  180.         fflush(stderr);
  181.         }
  182.  
  183.     fp = open_text_file(file_rec.resw_fname, "r", &error);
  184.     if (error)
  185.         {
  186.         fprintf(stderr, "Error #%d/$%x opening ResWordsFile '%s'\n",
  187.                 error, error, file_rec.resw_fname);
  188.     fflush(stderr);
  189.         exit(BAD_EXIT);
  190.         }
  191.  
  192.     init_scanner(TRUE, TRUE, TRUE); /* CaseSens=T UseUndersc=T DelQuotes=T */
  193.     line_num = 0;
  194.     end_of_file = feof(fp);
  195.     while (!end_of_file && !error)
  196.         { /* do each line in the file */
  197.         errno = 0;
  198.         end_of_file = (fgets(curr_line, LINE_SIZE, fp) == NULL);
  199.         line_num++;
  200.         error = errno;
  201.         if (end_of_file)
  202.             error = 0;    /* some O.S.s will set errno too! */
  203. /*
  204. debug(printf("resw line #%d:%s",line_num,curr_line);)
  205. */
  206.         while (!end_of_file && !error)
  207.             { /* get words on a line */
  208.             error = get_token(curr_line);
  209.             if (!error)
  210.                 {
  211.                 if (token.tok_type == TOK_ID)
  212.                     {
  213. /*
  214. debug(printf(" add resword '%s'\n",token.tok_string);)
  215. */
  216.                     resw_rec.resw_list =
  217.                         add_resword(token.tok_string, resw_rec.resw_list);
  218.                     resw_rec.num_reswords++;
  219.                     }
  220.                 else
  221.                     {
  222.                     fprintf(stderr,
  223.                     "illegal char '%c' in ResWordFile '%s' line %d col %d\n",
  224.                     token.tok_string[0], file_rec.resw_fname,
  225.                     line_num, token.tok_column);
  226.                     fflush(stderr);
  227.                     exit(BAD_EXIT);
  228.                     }
  229.                 } /* if !error */
  230.             } /*while in line*/
  231.  
  232.             /*
  233.             if a new line is needed, not really an error
  234.             */
  235.             if (error == TERR_EOLN)
  236.                 error = 0;
  237.         } /*while !eof*/
  238.  
  239.     /*
  240.     handle any fatal errors
  241.     */
  242.     if (error)
  243.         {
  244.         fprintf(stderr,
  245.             "error # %d/$%x reading line %d of ResWordFile '%s'\n",
  246.             error, error, line_num, file_rec.resw_fname);
  247.         fflush(stderr);
  248.         exit(BAD_EXIT);
  249.         }
  250.     error = close_text_file(fp, file_rec.resw_fname);
  251.     } /* handle_resw_file() */
  252.