| | 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 | |