Tesseract  3.02
tesseract-ocr/ccutil/nwmain.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        nwmain.h
00003  * Description: Tool to declare main, making windows invisible.
00004  * Author:                                      Ray Smith
00005  * Created:                                     Fri Sep 07 13:27:50 MDT 1995
00006  *
00007  * (C) Copyright 1995, Hewlett-Packard Co.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef RUNMAIN_H
00021 #define RUNMAIN_H
00022 
00023 #include          "host.h"
00024 #include          "params.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