Waitrud Weber’s blog

things and reminders for memories

Analyzer: windows-make: ensurance of times: we use m_replace in main function and realised the allocation error.

1. Read string from a file.

 
.\array_counter.cpp  Tue Mar 16 10:01:45 2021
...
124 :char* m_replace ( char* char_string,
125 :	char* from_string, char* to_string ) {
126 :	char c1, c2, c11;
127 :	int index_max = 0;
128 :	char* result;
129 :
130 :	err_msg_001("char* m_replace ( char* char_string, char* from_string, char* to_string ) starts.\r\n");
131 :
132 :	int count = array_count( char_string );
133 :	int a_f = array_count( from_string );
134 :	int a_t = array_count( to_string );
135 :	int a_c = 0;
136 :	index_max = sizeof (char)*( count + a_t - 1 );
137 :	char* char_string_2 = 
138 :	  (char *) malloc ( index_max );
139 :
140 :	c2 = 0;
141 :	int cnt_replace = 0;
142 :	for ( int i = 0; i<count; i++ ) {
143 :		c1 = *( char_string + i ) ;
144 :		a_c = 0;
145 :		for ( int j=0; j= 0) {
154 :			// match
155 :			int to = i + a_f;
156 :			for( int k=0; k<a_t; k++ ) {
157 :				err_msg_001("replace string k=|%d|%c|\r\n", k, *( to_string + k ));
158 :				int put_index_001 = + i + k + cnt_replace*(a_t- a_f );
159 :				if ( index_max <= put_index_001 ) {
160 :					index_max *= 2;
161 :					char_string_2 = (char*) realloc ( char_string_2, sizeof(char) * index_max );
162 :				}
163 :				*( char_string_2 + put_index_001 ) = 
164 :				  *( to_string + k );
165 :			}
166 :			i = to - 1;
167 :			cnt_replace++;
168 :		} else {
169 :			int put_index_002 = i + cnt_replace*(a_t- a_f );
170 :			if ( index_max <= put_index_002 ) {
171 :				index_max *= 2;
172 :				char_string_2 = (char*) realloc ( char_string_2, sizeof(char) * index_max );
173 :			}
174 :			*( char_string_2 + put_index_002 ) = c1;
175 :		}
176 :	}
177 :	*( char_string_2 + count + cnt_replace*( a_t - a_f ) ) = '\0';
178 :	result = copyof(char_string_2);
179 :
180 :	int countofresult = array_count(result);
181 :
182 :	err_msg_001("char* m_replace ( char* char_string, char* from_string, \
183 :char* to_string ) ends.|%s|count|%d|cnt_replace|%d| countofresult|%d|\r\nresult|%s|\r\n",
184 : result, count, cnt_replace, countofresult, result);
185 :
186 ://	exit(-1);
187 :	return result;
188 :}
   
 
replace.cpp  Tue Mar 16 09:49:48 2021
...
198 :int replace_csv_001 (char*** csv, char* form_file ) {
199 :	char str_num[10];
200 :	char* string;
201 :	printf("replace_csv starts.\r\n");
202 :	sleep(10);
203 :
204 :	string = read_all(form_file);
205 :
206 :	for( int i =0; i<3; i++ ) {
207 :		for( int j =0; j<3; j++ ) {
208 :			sprintf( str_num, "$%d%d", i, j );
209 :			if ( csv[i][j] != nullptr )
210 :				string = m_replace ( string, str_num, csv[i][j] );
211 :
212 :			printf("replace: %d %d |%s|%s|:\r\n|%s|\r\n", i, j, str_num, csv[i][j], string);
213 :			sleep(1);
214 :		}
215 :	}
   
  write_block starts.
cstring:
|<table>
<tr><td>1 </td><td> axes </td><td> amazon </td><td> </td></tr>
<tr><td>2 </td><td> drill </td><td> amazon </td><td> </td></tr>
<tr><td>3 </td><td> hummer </td><td> amazon </td><td> </td></tr>
<tr><td>$30 </td><td>$31 </td><td>$32 </td><td> </td></tr>
<tr><td>$40 </td><td>$41 </td><td>$42 </td><td> </td></tr>
<tr><td>$50 </td><td>$51 </td><td>$52 </td><td> </td></tr>
<tr><td>$60 </td><td>$61 </td><td>$62 </td><td> </td></tr>
<tr><td>$70 </td><td>$71 </td><td>$72 </td><td> </td></tr>
<tr><td>$80 </td><td>$81 </td><td>$82 </td><td> </td></tr>
<tr><td>$90 </td><td>$91 </td><td>$92 </td><td> </td></tr>
</table>
|
write_block ends.
replace_csv ends.
   
 

*

We could replace the all string from a csv file in from file and write the stream to the file.
We realised the correct restart index which was wrong before but that should have been workedin all practice.

 

   
  *
If something wrong happened, we realised the not effective mistaken but worked.