home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1990 / 06 / tricks / genfpt.c < prev    next >
C/C++ Source or Header  |  1990-03-08  |  10KB  |  451 lines

  1. /* genfpt.c
  2.  * Generieren von 'C'-Funktionen-Prototypen
  3.  * Version 1.0
  4.  * November 89
  5.  * Josef Konrad
  6.  * Comp. QC 2.0
  7.  * Aufruf: genfpt datei.c [datei.c ...]
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14.  
  15. #define     MAXLEN      255      /* Laenge einer Zeile */
  16. #define     FALSE       0
  17. #define     TRUE        1
  18.  
  19. #define READLINE if(!GetLine()) \
  20.     { printf("***Unerwartetes EOF***"); return; }
  21.  
  22. void main (int argc, char *argv[]);
  23. void SearchFunction (FILE *infile);
  24. int CheckFunction (void);
  25. void HandleParams (void);
  26. int GetPara (void);
  27. int GetTyp (void);
  28. int ReadLine (void);
  29. int GetLine (void);
  30. void SkipBraces (void);
  31. void SkipComment (void);
  32. void SkipQuot (void);
  33. void CompressStr (char *pString);
  34. int CheckIntTyp (char *pString);
  35.  
  36. char    cFunktDefBuf[MAXLEN],cLine[MAXLEN],cParaBuf[MAXLEN],
  37.           cFunktBuf[MAXLEN];
  38. char    cTmpBuf[MAXLEN];
  39. char    *pTmpBuf, *pStrPos;
  40. char    *pPara[100], *pActTyp;
  41. char    *pLine;
  42. FILE    *infile, *outfile;
  43.  
  44. /*--------------------------------------------------------*/
  45.  
  46. void main (int argc, char *argv[])
  47. {
  48.     char  *pPos;
  49.  
  50.     if (argc < 2)
  51.     {
  52.         puts ("Falsche Anzahl Argumente");
  53.         exit (1);
  54.     }
  55.     while (--argc)
  56.     {
  57.           if ((infile = fopen (*++argv, "rt")) == NULL)
  58.         {
  59.             perror (*argv);
  60.             exit (1);
  61.         }
  62.         pPos = strchr (*argv, '.');
  63.         if (!pPos)
  64.         {
  65.             puts ("Falscher Dateiname");
  66.             exit (1);
  67.         }
  68.         strncpy (cTmpBuf, *argv, pPos - *argv);
  69.         cTmpBuf[pPos - *argv] = '\0';
  70.         strcat (cTmpBuf, ".fpt");
  71.           if ((outfile = fopen ( cTmpBuf, "w")) == NULL)
  72.           {
  73.             perror (cTmpBuf);
  74.             exit (1);
  75.           }
  76.         printf("\nDatei: %s\n", *argv);
  77.         SearchFunction (infile);
  78.         fclose (infile);
  79.     }
  80. }
  81.  
  82. /*-----------------------------------------------------*/
  83. /* lesen aus Datei und auf Funktionsdefinition pruefen */
  84.  
  85. void SearchFunction (FILE *infile)
  86. {
  87.      while (!feof (infile))
  88.      {
  89.         if (!GetLine ())
  90.             break;
  91.         if (strchr (cLine, '{'))
  92.         /* Ueberlesen z.B. Strukturen */
  93.             if (!strchr (cLine, '}'))
  94.             {
  95.                 SkipBraces ();
  96.                 continue;
  97.             }
  98.             else
  99.           continue;   /* Einzeilige Strukturen etc. */
  100.           if (strchr (cLine,'(') && !strchr (cLine,';'))
  101.           if (CheckFunction ())
  102.           {
  103.           cParaBuf[0] = '\0';
  104.           if (!strchr (cFunktDefBuf, '{'))
  105.              /* Funkt. Rumpf */
  106.          {       /* Suche Beginn Funktionsrumpf */
  107.              READLINE;
  108.              while (!strchr (cLine, '{'))
  109.              {
  110.               strcat (cParaBuf, cLine);
  111.               /* Parameterdef. */
  112.               READLINE;
  113.              }
  114.           }
  115.           HandleParams ();
  116.           if (!strchr (cLine, '}'))
  117.               SkipBraces ();
  118.               /* Funktionsrumpf ueberlesen */
  119.            }
  120.      }  /* while */
  121. }
  122.  
  123. /*--------------------------------------------------------*/
  124. /* Pruefen, ob Funktionsdefinition */
  125.  
  126. CheckFunction (void)
  127. {
  128.      int     iParenCnt;
  129.  
  130.      if (cLine[0] == '#')   /* Preprozessor-Anweisung */
  131.         return (0);
  132.      iParenCnt = 1;
  133.      pLine = strchr (cLine, '(');
  134.      pLine++;
  135.      cFunktDefBuf[0] = '\0';
  136.      while (iParenCnt) /* Solange Klammern offen sind */
  137.      {
  138.         while (*pLine != '\n')
  139.         {
  140.             if (*pLine == '(')
  141.                 iParenCnt++;
  142.             else
  143.                 if (*pLine == ')')
  144.                     iParenCnt--;
  145.             pLine++;
  146.         }
  147.         strcat (cFunktDefBuf, cLine);
  148.         if (iParenCnt)
  149.             READLINE;
  150.      }
  151.      if (strchr (cLine, ';')) /* Noch ';' gefunden ? */
  152.         return 0;
  153.      return  1;    /* Funktionsdefinition */
  154. }
  155.  
  156. /*--------------------------------------------------------*/
  157. /* pruefen, ob Parametertypen in () angegeben sind, falls
  158.    nicht, werden sie erzeugt,  ebenso  wird 'void' bei ()
  159.     erzeugt und 'int', wenn kein
  160.     Typ fuer den Rueckgabewert angegeben ist */
  161.  
  162. void HandleParams (void)
  163. {
  164.     int   iParaCnt, Cnt, fFound;
  165.     char  *pPos1, *pPos2, fVoid;
  166.  
  167.     fVoid = TRUE;
  168.     pPos1 = pPos2 = strchr (cFunktDefBuf, '(');
  169.     while (*++pPos1 != ')')
  170.         if (!isspace (*pPos1++))
  171.         {
  172.          fVoid = FALSE;  /* Parameter in Klammern */
  173.             break;
  174.         }
  175.     if (fVoid)     /* Keine Parameter, kein 'void' */
  176.     {
  177.      strncpy (cFunktBuf,cFunktDefBuf,pPos2
  178.           - cFunktDefBuf + 1);
  179.      pPos1 = strchr (cFunktBuf, '(');
  180.      *++pPos1 = '\0';
  181.      strcat (cFunktBuf, "void);");
  182.      CompressStr (cFunktBuf);
  183.      if (CheckIntTyp (cFunktBuf))
  184.         {
  185.           strcpy (cFunktDefBuf, "int ");
  186.           strcat (cFunktDefBuf, cFunktBuf);
  187.           fprintf (outfile, "%s\n", cFunktDefBuf);
  188.           printf ("%s\n", cFunktDefBuf);
  189.          }
  190.         else
  191.          {
  192.            fprintf (outfile, "%s\n", cFunktBuf);
  193.            printf ("%s\n", cFunktBuf);
  194.          }
  195.         return;
  196.     }
  197.     if (cParaBuf[0] == '\0')
  198.                 /* Typen sind in () angegeben */
  199.     {
  200.         pPos1 = strrchr (cFunktDefBuf, ')');
  201.         if (pPos1)
  202.             *++pPos1 = '\0';
  203.         strcat (cFunktDefBuf, ";");
  204.         CompressStr (cFunktDefBuf);
  205.         if (CheckIntTyp (cFunktDefBuf))
  206.         {
  207.             strcpy (cFunktBuf, "int ");
  208.             strcat (cFunktBuf, cFunktDefBuf);
  209.             fprintf (outfile, "%s\n", cFunktBuf);
  210.             printf ("%s\n", cFunktBuf);
  211.         }
  212.         else
  213.         {
  214.             fprintf (outfile, "%s\n", cFunktDefBuf);
  215.             printf ("%s\n", cFunktDefBuf);
  216.         }
  217.         return;
  218.     }
  219.     strrev (cParaBuf);
  220.     pStrPos = strchr (cParaBuf, ';');
  221.             /* ersten ';' ueberspr.*/
  222.     pStrPos++;
  223.     cFunktBuf[0] = '\0';
  224.     do          /* Aufbauen Parameter und Typ Zeile */
  225.     {
  226.         iParaCnt = GetPara ();
  227.         fFound = GetTyp ();
  228.         for (Cnt = 0; Cnt < iParaCnt; Cnt++)
  229.         {
  230.             strcat (cFunktBuf, pPara[Cnt]);
  231.             strcat (cFunktBuf, " ");
  232.             strcat (cFunktBuf, pActTyp);
  233.             strcat (cFunktBuf, ",");
  234.             free (pPara[Cnt]);
  235.         }
  236.     }
  237.     while (fFound);
  238.     strrev (cFunktBuf);
  239.     *++pPos2 = '\0';
  240.                /* Funktionsdef. bis einschl. '(' */
  241.     strcat (cFunktDefBuf, &cFunktBuf[1]);
  242.                /* [0] steht ',' */
  243.     strcat (cFunktDefBuf, ");");
  244.     CompressStr (cFunktDefBuf);
  245.     if (CheckIntTyp (cFunktDefBuf))
  246.     {
  247.         strcpy (cFunktBuf, "int ");
  248.         strcat (cFunktBuf, cFunktDefBuf);
  249.         fprintf (outfile, "%s\n", cFunktBuf);
  250.         printf ("%s\n", cFunktBuf);
  251.     }
  252.     else
  253.     {
  254.         fprintf (outfile, "%s\n", cFunktDefBuf);
  255.         printf ("%s\n", cFunktDefBuf);
  256.     }
  257. }
  258.  
  259. /*--------------------------------------------------------*/
  260. /* Holt Parametergruppe aus der Parameterzeile
  261.     und setzt array von Zeigern auf die Parameter */
  262.  
  263. int GetPara (void)
  264. {
  265.     int   iParaCnt;
  266.  
  267.     iParaCnt = 0;
  268.     do
  269.     {
  270.         pTmpBuf = cTmpBuf;
  271.         while (isspace (*pStrPos) || *pStrPos == ',')
  272.            pStrPos++;
  273.         while (*pStrPos != ',' && !isspace (*pStrPos))
  274.            *pTmpBuf++ = *pStrPos++;
  275.         *pTmpBuf = '\0';
  276.         pPara[iParaCnt] = strdup (cTmpBuf);
  277.                                                     /* in Parameterliste */
  278.         iParaCnt++;
  279.         while (isspace (*pStrPos))
  280.             pStrPos++;
  281.     }
  282.     while (*pStrPos == ',');
  283.     return (iParaCnt);        /* Anzahl der Parameter */
  284. }
  285.  
  286. /*--------------------------------------------------------*/
  287. /* Holt Datentyp einer Parametergr.,setzt Zeiger auf  Typ */
  288.  
  289. int GetTyp (void)
  290. {
  291.   pTmpBuf = cTmpBuf;
  292.   while (*pStrPos != ';' && *pStrPos)    /* Ende Typ,String*/
  293.      if (*pStrPos != '\n')
  294.          *pTmpBuf++ = *pStrPos++;
  295.      else
  296.         pStrPos++;
  297.     *pTmpBuf = '\0';
  298.     pActTyp = cTmpBuf;            /* Zeiger setzen */
  299.     if (*pStrPos)     /* Noch nicht am String-Ende ? */
  300.     {
  301.         pStrPos++;     /* ';' ueberspringen */
  302.         return TRUE;
  303.     }
  304.     else
  305.  return FALSE;  /* Kennzeichnet String-Ende */
  306. }
  307.  
  308. /*--------------------------------------------------------*/
  309.  
  310. ReadLine (void)
  311. {
  312.  if (fgets (cTmpBuf, MAXLEN, infile) == NULL)
  313.     return FALSE;
  314.  pTmpBuf = cTmpBuf;
  315.  return TRUE;
  316. }
  317.  
  318. /*--------------------------------------------------------*/
  319. /* Einlesen einer Zeile, wobei Kommentare und Text innerhalb
  320.    von einfachen oder doppelten Anfuehrungszeichen
  321.    uebersprungen werden */
  322.  
  323. GetLine (void)
  324. {
  325.     do
  326.     {
  327.       if (!ReadLine())
  328.           return FALSE;
  329.       pLine = cLine;
  330.        do
  331.        {
  332.            if (*pTmpBuf == '\'' || *pTmpBuf == '"')
  333.            SkipQuot ();
  334.            else
  335.         if (*pTmpBuf == '/' && *(pTmpBuf+1) == '*')
  336.               SkipComment ();
  337.         else
  338.              *pLine++ = *pTmpBuf++;
  339.        }
  340.        while (*pTmpBuf != '\n'&& *pTmpBuf);
  341.        *pLine++ = *pTmpBuf++;
  342.        *pLine = '\0';
  343.        pLine = cLine;
  344.     }
  345.     while (cLine[0] == '\n'); /* keine leeren Zeilen */
  346.     return TRUE;
  347. }
  348.  
  349. /*-------------------------------------------------------*/
  350. /* Ueberlesen Code zwischen '{' und '}' */
  351.  
  352. void SkipBraces (void)
  353. {
  354.     int   iBraceCnt = 1;
  355.  
  356.     while (iBraceCnt)
  357.     {
  358.         if (!GetLine ()) return;
  359.         while (*pLine != '\n')
  360.         {
  361.             if (*pLine == '{')
  362.                 iBraceCnt++;
  363.             else
  364.                 if (*pLine == '}')
  365.                     iBraceCnt--;
  366.             pLine++;
  367.         }
  368.     }
  369. }
  370.  
  371. /*--------------------------------------------------------*/
  372.  
  373. void SkipComment (void)
  374. {
  375.     while (!(*pTmpBuf++ == '*' && *pTmpBuf == '/' ))
  376.         if (*pTmpBuf == '\n')
  377.             if (!ReadLine ()) return;
  378.     pTmpBuf++;
  379. }
  380.  
  381. /*--------------------------------------------------------*/
  382.  
  383. void SkipQuot (void)
  384. {
  385.     char  quotchar;
  386.  
  387.     quotchar = *pTmpBuf;
  388.     do
  389.     {
  390.      pTmpBuf++;
  391.      if (*pTmpBuf == '\n')
  392.          if (!ReadLine ()) return;
  393.          if (*pTmpBuf == '\\' && *(pTmpBuf-1) != '\\')
  394.          if (*++pTmpBuf == quotchar)
  395.                 pTmpBuf++;
  396.     }
  397.     while (*pTmpBuf != quotchar && *pTmpBuf);
  398.     pTmpBuf++;
  399. }
  400.  
  401. /*--------------------------------------------------------*/
  402. /* Entfernt aus String fuehrende und nachfolgende Leer-
  403.    zeichen und reduziert den Abstand zwischen Worten auf
  404.    jeweils 1 Leerzeichen.
  405.    Kopiert dann den komprimierten String nach pString     */
  406.  
  407. void CompressStr (char *pString)
  408. {
  409.     char  *pTmp;
  410.     pTmpBuf = cTmpBuf;
  411.     pTmp = pString;
  412.     while (*pString)
  413.     {
  414.         while (isspace (*pString) && *pString)
  415.             pString++;
  416.         while (!isspace (*pString) && *pString)
  417.             *pTmpBuf++ = *pString++;
  418.         *pTmpBuf++ = ' ';
  419.         while (isspace (*pString) && *pString)
  420.             pString++;
  421.     }
  422.     while (*pTmpBuf != ' ')
  423.         pTmpBuf--;      /* Letzte Blank entfernen */
  424.     *pTmpBuf = '\0';
  425.     strcpy (pTmp, cTmpBuf);
  426. }
  427.  
  428. /*--------------------------------------------------------*/
  429. /* Pruefen, ob Ergebnistyp angegeben ist */
  430.  
  431. CheckIntTyp (char *pString)
  432. {
  433.     char  *pTmp;
  434.  
  435.     pTmp = strchr (pString, '(');
  436.     if (!pTmp)
  437.         return FALSE;     /* Fuer Fehlerfall */
  438.     pTmp--;          /* Letztes Zeichen Funktionsname */
  439.     while (isspace (*pTmp))
  440.         pTmp--;
  441.     while (iscsym (*pTmp))
  442.         if (pTmp-- == pString)
  443.         return TRUE; /* Kein Typ angegeben -> int */
  444.     while (!iscsym (*pTmp))
  445.         if (pTmp-- == pString)
  446.             return TRUE;
  447.             /* Kein Typ angegeben --> int */
  448.     return FALSE;
  449. }
  450.  
  451.