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