Leptonica 1.68
C Image Processing Library

xtractprotos.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * xtractprotos.c
00018  *
00019  *   This program accepts a list of C files on the command line
00020  *   and outputs the C prototypes to stdout.  It uses cpp to
00021  *   handle the preprocessor macros, and then parses the cpp output.
00022  *   In use, it is convenient to redirect stdout to a file.
00023  *
00024  *   An optional 'prestring' can be prepended to each declaration.
00025  *   Without this string, use:
00026  *      xtractprotos [list of C files]
00027  *   With this string, use:
00028  *      xtractprotos -prestring=[string] [list of C files]
00029  *
00030  *   Update the version number when making a new version.
00031  *
00032  *   For simple C prototype extraction, xtractprotos has essentially
00033  *   the same functionality as Adam Bryant's cextract, but the latter
00034  *   has not been officially supported for over 10 years, has been
00035  *   patched numerous times, and currently doesn't work with
00036  *   sys/sysmacros.h for 64 bit architecture.
00037  *
00038  *   This is used to extract all prototypes in liblept, in the
00039  *   file leptprotos.h.  The function that does all the work is
00040  *   parseForProtos(), which takes as input the output from cpp.
00041  *   To avoid including the very large leptprotos.h in the input
00042  *   from each file, cpp runs here with -DNO_PROTOS.
00043  */
00044 
00045 #include <string.h>
00046 #include "allheaders.h"
00047 
00048     /* MS VC++ can't handle array initialization in C with static consts */
00049 #define  L_BUF_SIZE   512
00050 
00051     /* Cygwin needs any extension, or it will append ".exe" to the filename! */
00052 static const char *tempfile = "/tmp/temp_cpp_output.txt";
00053 static const char *version = "1.4";
00054 
00055 
00056 main(int    argc,
00057      char **argv)
00058 {
00059 char        *filein, *str, *prestring;
00060 const char  *spacestr = " ";
00061 char         buf[L_BUF_SIZE];
00062 l_int32      i, firstfile, len, ret;
00063 SARRAY      *sa;
00064 static char  mainName[] = "xtractprotos";
00065 
00066         /* Output extern C head */
00067     sa = sarrayCreate(0);
00068     sarrayAddString(sa, (char *)"/*", 1);
00069     snprintf(buf, L_BUF_SIZE,
00070              " *  This file was autogen'd by xtractprotos, v. %s", version);
00071     sarrayAddString(sa, buf, 1);
00072     sarrayAddString(sa, (char *)" */", 1);
00073     sarrayAddString(sa, (char *)"#ifdef __cplusplus", 1);
00074     sarrayAddString(sa, (char *)"extern \"C\" {", 1);
00075     sarrayAddString(sa, (char *)"#endif  /* __cplusplus */\n", 1);
00076     str = sarrayToString(sa, 1);
00077     fprintf(stdout, "%s", str);
00078     sarrayDestroy(&sa);
00079     lept_free(str);
00080 
00081         /* Prepend 'prestring' if requested */
00082     firstfile = 1;
00083     prestring = NULL;
00084     if (argv[1][0] == '-') {
00085         firstfile = 2;
00086         if (sscanf(argv[1], "-prestring=%s", buf) != 1)
00087             L_WARNING("Failure to parse prestring; omitting!", mainName);
00088         else {
00089             if ((len = strlen(buf)) > L_BUF_SIZE - 3)
00090                 L_WARNING("prestring too large; omitting!", mainName);
00091             else {
00092                 buf[len] = ' ';
00093                 buf[len + 1] = '\0';
00094                 prestring = stringNew(buf);
00095             }
00096         }
00097     }
00098         
00099     for (i = firstfile; i < argc; i++) {
00100         filein = argv[i];
00101         len = strlen(filein);
00102         if (filein[len - 1] == 'h')
00103             continue;
00104         snprintf(buf, L_BUF_SIZE, "cpp -ansi -DNO_PROTOS %s %s",
00105                  filein, tempfile);
00106         ret = system(buf);
00107         if (ret) {
00108             fprintf(stderr, "cpp failure for %s; continuing\n", filein);
00109             continue;
00110         }
00111 
00112         if ((str = parseForProtos(tempfile, prestring)) == NULL) {
00113             fprintf(stderr, "parse failure for %s; continuing\n", filein);
00114             continue;
00115         }
00116         if (strlen(str) > 1)  /* strlen(str) == 1 is a file without protos */
00117             fprintf(stdout, "%s", str);
00118         lept_free(str);
00119     }
00120 
00121         /* Output extern C tail */
00122     sa = sarrayCreate(0);
00123     sarrayAddString(sa, (char *)"\n#ifdef __cplusplus", 1);
00124     sarrayAddString(sa, (char *)"}", 1);
00125     sarrayAddString(sa, (char *)"#endif  /* __cplusplus */", 1);
00126     str = sarrayToString(sa, 1);
00127     fprintf(stdout, "%s", str);
00128     sarrayDestroy(&sa);
00129     lept_free(str);
00130     if (prestring)
00131         lept_free(prestring);
00132 
00133     remove(tempfile);
00134     return 0;
00135 }
00136 
00137 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines