| 1 | /** | 
|---|
| 2 |  * | 
|---|
| 3 |  */ | 
|---|
| 4 |  | 
|---|
| 5 | /* WIN32 stuff */ | 
|---|
| 6 | #include <windows.h> | 
|---|
| 7 | #include <stdio.h> | 
|---|
| 8 | #include <tchar.h> | 
|---|
| 9 |  | 
|---|
| 10 | // TODO: reference additional headers your program requires here | 
|---|
| 11 | #include <process.h> | 
|---|
| 12 | #include <strsafe.h> | 
|---|
| 13 | #include <shlwapi.h> | 
|---|
| 14 | #include <io.h> | 
|---|
| 15 |  | 
|---|
| 16 | #include <atlbase.h> | 
|---|
| 17 |  | 
|---|
| 18 | #include "tools.h" | 
|---|
| 19 |  | 
|---|
| 20 | void _addpath ( TCHAR * path ) | 
|---|
| 21 | { | 
|---|
| 22 |         TCHAR * pv; | 
|---|
| 23 |         size_t  pv_len,var_size; | 
|---|
| 24 |         errno_t res; | 
|---|
| 25 |  | 
|---|
| 26 |         /* Determine the length of the current path. */ | 
|---|
| 27 |         _tgetenv_s ( &var_size, NULL, 0, TEXT("PATH") ); | 
|---|
| 28 |         pv_len = var_size + _tcslen ( path ) + 2; | 
|---|
| 29 |  | 
|---|
| 30 |         /* Allocate memory for the path string from the environment | 
|---|
| 31 |            plus the added path. */ | 
|---|
| 32 |         pv = (TCHAR *) malloc ( pv_len * sizeof(TCHAR) ); | 
|---|
| 33 |         if ( pv == NULL ) | 
|---|
| 34 |         { | 
|---|
| 35 |                 perror ( "Failed to allocate memory for path" ); | 
|---|
| 36 |                 exit ( -1 ); | 
|---|
| 37 |         } | 
|---|
| 38 |  | 
|---|
| 39 |         /* Get the variable. */ | 
|---|
| 40 |         res = _tgetenv_s ( &var_size, pv, pv_len, TEXT("PATH") ); | 
|---|
| 41 |         if ( res != 0 ) | 
|---|
| 42 |         { | 
|---|
| 43 |                 perror ( "Cannot read environment variable PATH" ); | 
|---|
| 44 |                 exit ( -1 ); | 
|---|
| 45 |         } | 
|---|
| 46 |  | 
|---|
| 47 |         /* Add the new path string. */ | 
|---|
| 48 |         var_size = _tcslen ( pv ); | 
|---|
| 49 |         StringCbCat ( pv, pv_len*sizeof(TCHAR), TEXT(";") ); | 
|---|
| 50 |         var_size = _tcslen ( pv ); | 
|---|
| 51 |         StringCbCat ( pv, pv_len*sizeof(TCHAR), path ); | 
|---|
| 52 |         var_size = _tcslen ( pv ); | 
|---|
| 53 |  | 
|---|
| 54 |         /* Put the modified string back to the environment of this | 
|---|
| 55 |            process. */ | 
|---|
| 56 |         res = _tputenv_s( TEXT("PATH"), pv ); | 
|---|
| 57 |         if ( res != 0 ) | 
|---|
| 58 |         { | 
|---|
| 59 |                 perror ( "Cannot store new PATH" ); | 
|---|
| 60 |                 exit ( -1 ); | 
|---|
| 61 |         } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | TCHAR * _tstrtrim ( TCHAR * str ) | 
|---|
| 65 | { | 
|---|
| 66 |    TCHAR * os = str; | 
|---|
| 67 |    TCHAR * oe = str + _tcslen ( str ) - 1; | 
|---|
| 68 |  | 
|---|
| 69 |    while ( _istspace ( *os )) os++; | 
|---|
| 70 |    while ( _istspace ( *oe )) oe--; | 
|---|
| 71 |    *++oe = 0; | 
|---|
| 72 |  | 
|---|
| 73 |    return os; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | errno_t replace_in_scenario ( | 
|---|
| 77 |         const TCHAR * sce_name, | 
|---|
| 78 |         const TCHAR * new_name, | 
|---|
| 79 |         const TCHAR * section, | 
|---|
| 80 |         const TCHAR ** patterns, | 
|---|
| 81 |         int num_patterns, | 
|---|
| 82 |         const TCHAR * replacement | 
|---|
| 83 |         ) | 
|---|
| 84 | { | 
|---|
| 85 |         TCHAR   tline[MAX_LINE]; | 
|---|
| 86 |         TCHAR   trml[MAX_LINE]; | 
|---|
| 87 |     TCHAR   tmp_name[MAX_PATH]; | 
|---|
| 88 |         FILE *  tmp_stream; | 
|---|
| 89 |         FILE *  sce_stream; | 
|---|
| 90 |         errno_t res; | 
|---|
| 91 |  | 
|---|
| 92 |         /* Create filename of the temporary scenario. */ | 
|---|
| 93 |         _ttmpnam_s ( tmp_name ); | 
|---|
| 94 |  | 
|---|
| 95 |     /* Open the temporary file for writting. */ | 
|---|
| 96 |     res = _tfopen_s ( &tmp_stream, tmp_name, TEXT("w") ); | 
|---|
| 97 |         if ( res != 0 ) | 
|---|
| 98 |         { | 
|---|
| 99 |                 perror ( "Cannot create temporary file" ); | 
|---|
| 100 |                 return res; | 
|---|
| 101 |         } | 
|---|
| 102 |  | 
|---|
| 103 |     /* Open the scenario file for reading */ | 
|---|
| 104 |     res = _tfopen_s ( &sce_stream, sce_name, TEXT("r") ); | 
|---|
| 105 |     if ( res != 0 ) | 
|---|
| 106 |         { | 
|---|
| 107 |                 perror ( "Cannot open scenario for reading" ); | 
|---|
| 108 |                 return res; | 
|---|
| 109 |         } | 
|---|
| 110 |  | 
|---|
| 111 |     /* Copy the scenario file to the temporary location line by line, | 
|---|
| 112 |        changing the lines containing some of strings in `patterns` | 
|---|
| 113 |            to `replacement`, but only in section name `section`. */ | 
|---|
| 114 |  | 
|---|
| 115 |     bool is_sct = false; | 
|---|
| 116 |     bool is_upd = false; | 
|---|
| 117 |  | 
|---|
| 118 |         while ( !feof ( sce_stream )) | 
|---|
| 119 |         { | 
|---|
| 120 |         /* Read a single line from the scenario file. */ | 
|---|
| 121 |         if ( _fgetts ( tline, MAX_LINE, sce_stream ) == NULL ) | 
|---|
| 122 |                 { | 
|---|
| 123 |                         /* Error reading from source file. */ | 
|---|
| 124 |                         if ( !feof ( sce_stream )) perror ( "Error reading scenario" ); | 
|---|
| 125 |                         break; | 
|---|
| 126 |                 } | 
|---|
| 127 |  | 
|---|
| 128 |                 /* Is the `tline` empty? */ | 
|---|
| 129 |                 StringCbCopy ( trml, MAX_LINE*sizeof(TCHAR), tline ); | 
|---|
| 130 |         TCHAR * ttrml = _tstrtrim ( trml ); | 
|---|
| 131 |         if ( _tcslen ( ttrml ) == 0 ) continue; | 
|---|
| 132 |  | 
|---|
| 133 |         /* Check the beginning of the specified section. */ | 
|---|
| 134 |         if ( !is_sct ) | 
|---|
| 135 |                 { | 
|---|
| 136 |             is_sct = ( _tcsstr ( tline, section ) != NULL ); | 
|---|
| 137 |                 } | 
|---|
| 138 |                 else | 
|---|
| 139 |                 { | 
|---|
| 140 |                         /* We are inside the section, try to replace the text | 
|---|
| 141 |                            that should be replaced. */ | 
|---|
| 142 |                         bool is_pat = false; | 
|---|
| 143 |                         for (int i=0;i<num_patterns;i++) | 
|---|
| 144 |                         { | 
|---|
| 145 |                                 is_pat = ( _tcsstr ( tline, patterns[i] ) != NULL ); | 
|---|
| 146 |                                 if ( is_pat ) break; | 
|---|
| 147 |                         } | 
|---|
| 148 |             if ( is_pat ) | 
|---|
| 149 |                         { | 
|---|
| 150 |                 _sntprintf_s ( tline, MAX_LINE, MAX_LINE-2, TEXT("%s\n"), replacement ); | 
|---|
| 151 |                 is_upd = true; | 
|---|
| 152 |                         } | 
|---|
| 153 |                 } | 
|---|
| 154 |         _fputts ( tline, tmp_stream ); | 
|---|
| 155 |         } | 
|---|
| 156 |  | 
|---|
| 157 |     /* Construct the section if none is present. */ | 
|---|
| 158 |     if ( ! is_sct ) | 
|---|
| 159 |         { | 
|---|
| 160 |         _ftprintf ( tmp_stream, TEXT("%s\n"), section ); | 
|---|
| 161 |         } | 
|---|
| 162 |  | 
|---|
| 163 |     /* If the replacement string was not found in the scenario file, | 
|---|
| 164 |            inject it. */ | 
|---|
| 165 |     if ( ! is_upd ) | 
|---|
| 166 |         { | 
|---|
| 167 |         _ftprintf ( tmp_stream, TEXT("%s\n"), replacement ); | 
|---|
| 168 |         } | 
|---|
| 169 |  | 
|---|
| 170 |     fclose ( tmp_stream ); | 
|---|
| 171 |     fclose ( sce_stream ); | 
|---|
| 172 |  | 
|---|
| 173 |     /* Do not rewrite the original scenario file, but move the temporary | 
|---|
| 174 |        file to the same directory under a new name that has been | 
|---|
| 175 |            specified as a parameter. */ | 
|---|
| 176 |     if ( ! _taccess ( new_name, 00 )) | 
|---|
| 177 |         { | 
|---|
| 178 |                 /* File exists, delete it. */ | 
|---|
| 179 |         DeleteFile ( new_name ); | 
|---|
| 180 |         } | 
|---|
| 181 |     MoveFile ( tmp_name, new_name ); | 
|---|
| 182 |  | 
|---|
| 183 |         return 0; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | errno_t replace_stoptime ( | 
|---|
| 187 |         const TCHAR * net_path, | 
|---|
| 188 |         const TCHAR * stoptime | 
|---|
| 189 |         ) | 
|---|
| 190 | { | 
|---|
| 191 |         TCHAR   tline[MAX_LINE]; | 
|---|
| 192 |     TCHAR   tmp_name[MAX_PATH]; | 
|---|
| 193 |     TCHAR   run_name[MAX_EXE_PATH]; | 
|---|
| 194 |         FILE *  tmp_stream; | 
|---|
| 195 |         FILE *  run_stream; | 
|---|
| 196 |         errno_t res; | 
|---|
| 197 |         int     line_no; | 
|---|
| 198 |  | 
|---|
| 199 |         /* Create filename of the temporary file with replaced stoptime. */ | 
|---|
| 200 |         _ttmpnam_s ( tmp_name ); | 
|---|
| 201 |  | 
|---|
| 202 |     /* Open the temporary file for writting. */ | 
|---|
| 203 |     res = _tfopen_s ( &tmp_stream, tmp_name, TEXT("w") ); | 
|---|
| 204 |         if ( res != 0 ) | 
|---|
| 205 |         { | 
|---|
| 206 |                 perror ( "Cannot create temporary file" ); | 
|---|
| 207 |                 return res; | 
|---|
| 208 |         } | 
|---|
| 209 |  | 
|---|
| 210 |         /* Create the name of the time file. */ | 
|---|
| 211 |         StringCbCopy ( run_name, MAX_EXE_PATH, net_path ); | 
|---|
| 212 |         StringCbCat  ( run_name, MAX_EXE_PATH, TEXT("\\AIMSUN\\RunTime") ); | 
|---|
| 213 |  | 
|---|
| 214 |         /* Open the time file for reading */ | 
|---|
| 215 |     res = _tfopen_s ( &run_stream, run_name, TEXT("r") ); | 
|---|
| 216 |     if ( res != 0 ) | 
|---|
| 217 |         { | 
|---|
| 218 |                 perror ( "Cannot open scenario for reading" ); | 
|---|
| 219 |                 return res; | 
|---|
| 220 |         } | 
|---|
| 221 |  | 
|---|
| 222 |     /* Copy the time file to the temporary location line by line, | 
|---|
| 223 |        changing the third line containing stop time. */ | 
|---|
| 224 |         line_no = 0; | 
|---|
| 225 |         while ( !feof ( run_stream )) | 
|---|
| 226 |         { | 
|---|
| 227 |         /* Read a single line from the scenario file. */ | 
|---|
| 228 |         if ( _fgetts ( tline, MAX_LINE, run_stream ) == NULL ) | 
|---|
| 229 |                 { | 
|---|
| 230 |                         /* Error reading from source file. */ | 
|---|
| 231 |                         if ( !feof ( run_stream )) perror ( "Error reading scenario" ); | 
|---|
| 232 |                         break; | 
|---|
| 233 |                 } | 
|---|
| 234 |  | 
|---|
| 235 |                 /* Increase the line counter. */ | 
|---|
| 236 |                 line_no++; | 
|---|
| 237 |  | 
|---|
| 238 |                 /* Replace the third line. */ | 
|---|
| 239 |                 if ( line_no == 3 ) | 
|---|
| 240 |                 { | 
|---|
| 241 |                         _sntprintf_s ( tline, MAX_LINE, MAX_LINE-2, TEXT("%s\n"), stoptime ); | 
|---|
| 242 |                 } | 
|---|
| 243 |  | 
|---|
| 244 |         _fputts ( tline, tmp_stream ); | 
|---|
| 245 |         } | 
|---|
| 246 |  | 
|---|
| 247 |     fclose ( tmp_stream ); | 
|---|
| 248 |     fclose ( run_stream ); | 
|---|
| 249 |  | 
|---|
| 250 |     /* Rewrite the original RunTime file. */ | 
|---|
| 251 |     if ( ! _taccess ( run_name, 00 )) | 
|---|
| 252 |         { | 
|---|
| 253 |                 /* File exists, delete it. */ | 
|---|
| 254 |         DeleteFile ( run_name ); | 
|---|
| 255 |         } | 
|---|
| 256 |     MoveFile ( tmp_name, run_name ); | 
|---|
| 257 |  | 
|---|
| 258 |         return 0; | 
|---|
| 259 |  | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | int _search_index ( const int *list, int numElements, int value ) | 
|---|
| 263 | { | 
|---|
| 264 |         int i; | 
|---|
| 265 |         for ( i = 0 ; i < numElements ; i++ ) | 
|---|
| 266 |         { | 
|---|
| 267 |                 if ( *list == value ) break; | 
|---|
| 268 |                 list++; | 
|---|
| 269 |         } | 
|---|
| 270 |         if ( i >= numElements ) i = -1; | 
|---|
| 271 |         return i; | 
|---|
| 272 |  | 
|---|
| 273 | } | 
|---|