/** * */ /* WIN32 stuff */ #include #include #include // TODO: reference additional headers your program requires here #include #include #include #include #include #include "tools.h" void _addpath ( TCHAR * path ) { TCHAR * pv; size_t pv_len,var_size; errno_t res; /* Determine the length of the current path. */ _tgetenv_s ( &var_size, NULL, 0, TEXT("PATH") ); pv_len = var_size + _tcslen ( path ) + 2; /* Allocate memory for the path string from the environment plus the added path. */ pv = (TCHAR *) malloc ( pv_len * sizeof(TCHAR) ); if ( pv == NULL ) { perror ( "Failed to allocate memory for path" ); exit ( -1 ); } /* Get the variable. */ res = _tgetenv_s ( &var_size, pv, pv_len, TEXT("PATH") ); if ( res != 0 ) { perror ( "Cannot read environment variable PATH" ); exit ( -1 ); } /* Add the new path string. */ var_size = _tcslen ( pv ); StringCbCat ( pv, pv_len*sizeof(TCHAR), TEXT(";") ); var_size = _tcslen ( pv ); StringCbCat ( pv, pv_len*sizeof(TCHAR), path ); var_size = _tcslen ( pv ); /* Put the modified string back to the environment of this process. */ res = _tputenv_s( TEXT("PATH"), pv ); if ( res != 0 ) { perror ( "Cannot store new PATH" ); exit ( -1 ); } } TCHAR * _tstrtrim ( TCHAR * str ) { TCHAR * os = str; TCHAR * oe = str + _tcslen ( str ) - 1; while ( _istspace ( *os )) os++; while ( _istspace ( *oe )) oe--; *++oe = 0; return os; } errno_t replace_in_scenario ( const TCHAR * sce_name, const TCHAR * new_name, const TCHAR * section, const TCHAR ** patterns, int num_patterns, const TCHAR * replacement ) { TCHAR tline[MAX_LINE]; TCHAR trml[MAX_LINE]; TCHAR tmp_name[MAX_PATH]; FILE * tmp_stream; FILE * sce_stream; errno_t res; /* Create filename of the temporary scenario. */ _ttmpnam_s ( tmp_name ); /* Open the temporary file for writting. */ res = _tfopen_s ( &tmp_stream, tmp_name, TEXT("w") ); if ( res != 0 ) { perror ( "Cannot create temporary file" ); return res; } /* Open the scenario file for reading */ res = _tfopen_s ( &sce_stream, sce_name, TEXT("r") ); if ( res != 0 ) { perror ( "Cannot open scenario for reading" ); return res; } /* Copy the scenario file to the temporary location line by line, changing the lines containing some of strings in `patterns` to `replacement`, but only in section name `section`. */ bool is_sct = false; bool is_upd = false; while ( !feof ( sce_stream )) { /* Read a single line from the scenario file. */ if ( _fgetts ( tline, MAX_LINE, sce_stream ) == NULL ) { /* Error reading from source file. */ if ( !feof ( sce_stream )) perror ( "Error reading scenario" ); break; } /* Is the `tline` empty? */ StringCbCopy ( trml, MAX_LINE*sizeof(TCHAR), tline ); TCHAR * ttrml = _tstrtrim ( trml ); if ( _tcslen ( ttrml ) == 0 ) continue; /* Check the beginning of the specified section. */ if ( !is_sct ) { is_sct = ( _tcsstr ( tline, section ) != NULL ); } else { /* We are inside the section, try to replace the text that should be replaced. */ bool is_pat = false; for (int i=0;i= numElements ) i = -1; return i; }