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