ccutil/nwmain.h

Go to the documentation of this file.
00001 
00020 #ifndef RUNMAIN_H
00021 #define RUNMAIN_H
00022 
00023 #include          "host.h"
00024 #include          "varable.h"
00025 #include          "notdll.h"     //must be last include
00026 
00027 #define DECLARE_MAIN(ARGC,ARGV)\
00028 STRING_VAR(init_config_file,"config","Config file to read on startup");\
00029 REALLY_DECLARE_MAIN(ARGC,ARGV)
00030 
00031 #define DECLARE_MAIN_CONFIG(ARGC,ARGV,NAME)\
00032 STRING_VAR(init_config_file,NAME,"Config file to read on startup");\
00033 REALLY_DECLARE_MAIN(ARGC,ARGV)
00034 
00035 #ifndef __UNIX__
00036 
00037 #define REALLY_DECLARE_MAIN(ARGC,ARGV)\
00038 \
00039 /**********************************************************************\
00040 * parse_args                                       \
00041 *                                               \
00042 * Turn a list of args into a new list of args with each separate\
00043 * whitespace spaced string being an arg.                 \
00044 **********************************************************************/\
00045 \
00046 INT32                parse_args(             /*refine arg list*/\
00047 INT32                argc,                /*no of input args*/\
00048 char                 *argv[],             /*input args*/\
00049 char                 *arglist[]              /*output args*/\
00050 )\
00051 {\
00052    INT32             argcount;               /*converted argc*/\
00053    char              *testchar;              /*char in option string*/\
00054    INT32             arg;                 /*current argument*/\
00055 \
00056    argcount=0;                               /*no of options*/\
00057    for (arg=0;arg<argc;arg++)\
00058    {\
00059       testchar=argv[arg];                       /*start of arg*/\
00060       do\
00061       {\
00062          while (*testchar\
00063          && (*testchar==' ' || *testchar=='"' || *testchar=='\n' || *testchar=='\t'))\
00064             testchar++;                      /*skip white space*/\
00065          if (*testchar)\
00066          {\
00067             arglist[argcount++]=testchar;       /*new arg*/\
00068             do\
00069             {\
00070                for (testchar++;*testchar\
00071                && *testchar!=' ' && *testchar!='"' && *testchar!='\n' && *testchar!='\t';\
00072                testchar++);                     /*skip to white space*/\
00073             }\
00074             while (*testchar=='"' && testchar[1]!=' ' && testchar[1]!='\0' && testchar[1]!='\n' && testchar[1]!='\t');\
00075             if (*testchar)\
00076                *testchar++='\0';             /*turn to separate args*/\
00077          }\
00078       }\
00079       while (*testchar);\
00080    }\
00081    return argcount;                          /*new number of args*/\
00082 }\
00083 \
00084 INT32                global_exit_code;\
00085 INT32                real_main(INT32,const char**);\
00086 \
00087 INT32                run_main(               /*the main thread*/\
00088 CWinApp*             theapp                  /*arguments*/\
00089 )\
00090 {\
00091    char              **argv;\
00092    char              *argsin[2];\
00093    INT32             argc;\
00094    INT32             exit_code;\
00095    \
00096    argsin[0]=strdup(theapp->m_pszExeName);\
00097    argsin[1]=strdup(theapp->m_lpCmdLine);\
00098 /*allocate memory for the args. There can never be more than half*/\
00099 /*the total number of characters in the arguments.*/\
00100    argv=(char**)malloc(((strlen(argsin[0])+strlen(argsin[1]))/2+1)*sizeof(char*));\
00101 \
00102 /*now construct argv as it should be for C.*/\
00103    argc=parse_args(2,argsin,argv);\
00104 \
00105 /*call main(argc,argv) here*/\
00106    exit_code=real_main(argc,(const char **)argv);\
00107 \
00108 \
00109 /*now get rid of the main app window*/\
00110    if (theapp!=NULL && theapp->m_pMainWnd!=NULL)\
00111       PostMessage(theapp->m_pMainWnd->m_hWnd,WM_QUIT,0,0);\
00112    free(argsin[0]);\
00113    free(argsin[1]);\
00114    free(argv);\
00115    global_exit_code=exit_code;\
00116    return exit_code;\
00117 }\
00118 \
00119 INT32                real_main(INT32 ARGC,const char* ARGV[])\
00120 
00121 #else
00122 
00123 #define REALLY_DECLARE_MAIN(ARGC,ARGV)\
00124 \
00125 /**********************************************************************\
00126 * parse_args\
00127 *\
00128 * Turn a list of args into a new list of args with each separate\
00129 * whitespace spaced string being an arg.\
00130 **********************************************************************/\
00131 \
00132 INT32                parse_args(             /*refine arg list*/\
00133 INT32                argc,                /*no of input args*/\
00134 char                 *argv[],             /*input args*/\
00135 char                 *arglist[]              /*output args*/\
00136 )\
00137 {\
00138    INT32             argcount;               /*converted argc*/\
00139    char              *testchar;              /*char in option string*/\
00140    INT32             arg;                 /*current argument*/\
00141 \
00142    argcount=0;                               /*no of options*/\
00143    for (arg=0;arg<argc;arg++)\
00144    {\
00145       testchar=argv[arg];                       /*start of arg*/\
00146       do\
00147       {\
00148          while (*testchar\
00149          && (*testchar==' ' || *testchar=='"' || *testchar=='\n' || *testchar=='\t'))\
00150             testchar++;                      /*skip white space*/\
00151          if (*testchar)\
00152          {\
00153             arglist[argcount++]=testchar;       /*new arg*/\
00154             do\
00155             {\
00156                for (testchar++;*testchar\
00157                && *testchar!=' ' && *testchar!='"' && *testchar!='\n' && *testchar!='\t';\
00158                testchar++);                     /*skip to white space*/\
00159             }\
00160             while (*testchar=='"' && testchar[1]!=' ' && testchar[1]!='\0' && testchar[1]!='\n' && testchar[1]!='\t');\
00161             if (*testchar)\
00162                *testchar++='\0';             /*turn to separate args*/\
00163          }\
00164       }\
00165       while (*testchar);\
00166    }\
00167    return argcount;                          /*new number of args*/\
00168 }\
00169 \
00170 INT32                main(INT32 ARGC,const char* ARGV[])\
00171 
00172 #endif
00173 
00174 #else
00175 #error "NOT allowed to include nwmain.h or runmain.h twice!!"
00176 #endif

Generated on Wed Feb 28 19:49:09 2007 for Tesseract by  doxygen 1.5.1