Waitrud Weber’s blog

things and reminders for memories

WindowsAPI: aliment: convert every strings to char string(char*)

  1 :#ifndef _FILE_CONTROLER_H_
  2 :
  3 :#define _FILE_CONTROLER_H_
  4 :
  5 ://using namespace std;
  6 :
  7 :class FileControler
  8 :{
  9 : public:
 10 :  // void List_Files () ;
 11 :  //void List_Files(std::string strPath);
 12 :  void List_Files_001(char *strPath);
 13 :  void List_Files(char* lpcstr_path );
 14 :  void print_strings ();
 15 :  char** get_files ( char* str, int* file_num );
 16 :  int get_strings_number ();
 17 :
 18 : private:
 19 :  void Sub_List_Files () ;
 20 :  char* m_fullpath_001 ( char* strPath,  char* filename ) ;
 21 :  char* m_fullpath ( char* strPath, char* filename );
 22 :
 23 :  char* to_directory(char* lpcstr_path, char* filename );
 24 :  char** put_string ( char* str ) ;
 25 :  char** get_strings ();
 26 :  char* copyof ( char* str ) ;
 27 :  int is_attribute ( char* ffdc_filename, char* char_attr );
 28 :
 29 : public:
 30 :  FileControler ( );
 31 :
 32 : private:
 33 :  char** list_dummy_ary = nullptr;
 34 :  int list_dummy_ary_index = 0;
 35 :
 36 :};
 37 :
 38 :#endif

 

  1 :#include <windows.h>
  2 ://#include <iostream>
  3 ://#include <cstring>
  4 ://#include <string>
  5 :#include <strsafe.h>
  6 :
  7 :#include "aDebug.h"
  8 :#include "array_counter.h"
  9 :#include "parse.h"
 10 :// #include "php_analyzer.h"
 11 :#include "list_directory.h"
 12 :
 13 ://using namespace std;
 14 :
 15 :FileControler::FileControler() {
 16 : this->list_dummy_ary_index = 0;
 17 : this->list_dummy_ary = nullptr;
 18 :
 19 :}
 20 :
 21 ://
 22 :// it refers all to
 23 :// https://stackoverflow.com/questions/11362771/list-files-in-directory-c
 24 :// https://bituse.info/winapi/31
 25 ://
 26 :// Parameters
 27 ://
 28 :// lpcstr_path
 29 ://
 30 :// Directory name which is going to be found files.
 31 ://
 32 :// Return Value
 33 ://
 34 :// char**
 35 ://
 36 :// File list under the lpcstr_path, which has fullpath.
 37 ://
 38 :char** FileControler::get_files ( char* lpcstr_path, int* file_num ) {
 39 : WIN32_FIND_DATA ffd;
 40 : HANDLE hFind = INVALID_HANDLE_VALUE;
 41 : char* ffdc_filename;
 42 : int dummy_filenum = 0;
 43 : char* char_asterisk_path = "\0"; // .\abc\* if .\abc\*.php
 44 : char* char_attr = "\0";
 45 : int cnt = 0;
 46 : int cnt_char_asterisk_path = 0;
 47 :
 48 : DEBUG( (char *)"get_files", "lpcstr_path: %s file_num: %d\r\n", (char*)lpcstr_path, *file_num );
 49 :
 50 :    // Prepare string for use with FindFile functions.  First, copy the
 51 :    // string to a buffer, then append '\*' to the directory name.
 52 : cnt = array_count(lpcstr_path);
 53 : int search_length = m_last_with( lpcstr_path, ".");
 54 : if ( search_length <= 0 ) {
 55 :
 56 :  hFind = FindFirstFile( TEXT( lpcstr_path ), &ffd );
 57 :  char_asterisk_path = lpcstr_path;
 58 : } else {
 59 :
 60 :  // In this case, lpcstr_path is like ".\abc\*.php"
 61 :  char_asterisk_path = substring( lpcstr_path, 0, search_length );
 62 :  cnt_char_asterisk_path = array_count( char_asterisk_path );
 63 :  char_attr = substring( lpcstr_path, search_length, cnt - cnt_char_asterisk_path ); // max_count:cnt
 64 :  hFind = FindFirstFile( TEXT( char_asterisk_path ), &ffd );
 65 : }
 66 :
 67 : ffdc_filename = copyof( (char*)ffd.cFileName );
 68 : printf("char_attr: %s search_length: %d char_asterisk_path: %s lpcstr_path %s\r\n", char_attr, search_length, char_asterisk_path, lpcstr_path );
 69 :
 70 : int index = 0;
 71 :    // List all the files in the directory with some info about them.
 72 : do
 73 : {
 74 :  ffdc_filename = copyof( (char*)ffd.cFileName );
 75 :  //DEBUG( (char *)"get_files",  "ffdc_filename: %d: %s attribute %d\r\n", index, ffdc_filename, is_attribute ( ffdc_filename, char_attr ) );
 76 :  index++;
 77 :  char* char_directory = to_directory ( char_asterisk_path, ffdc_filename );
 78 :
 79 :  if ( char_directory == NULL || m_compare( ffdc_filename, (char*)".") == 1 || m_compare( ffdc_filename, (char*)"..") == 1 ) {
 80 :
 81 :   //DEBUG( (char *)"get_files",  "skip away : %s\r\n", ffdc_filename );
 82 :
 83 :  } else {
 84 :
 85 :   if ( is_attribute ( ffdc_filename, char_attr ) == 1 ) {
 86 :    this->put_string ( char_directory );
 87 :   }
 88 :
 89 :   if ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
 90 :    //char* listfiles = (char*) m_concat( (char*)char_directory, (char*)"\\*." );
 91 :    char* listfiles = (char*) m_concat( (char*)char_directory, (char*)"\\*" );
 92 :    listfiles = (char*) m_concat( (char*)listfiles, (char*)char_attr );
 93 :    DEBUG( (char *)"get_files", "listfiles %s\r\n", listfiles );
 94 :    this->get_files( (char*) listfiles, &dummy_filenum );
 95 :   }
 96 :
 97 :  }
 98 : }
 99 : while ( FindNextFile(hFind, &ffd ) != 0);
100 :
101 : FindClose(hFind);
102 :
103 : *file_num = get_strings_number () ;
104 :
105 : return get_strings();
106 :}
107 :
108 ://
109 ://
110 ://
111 ://
112 ://
113 :int FileControler::is_attribute ( char* ffdc_filename, char* char_attr ) {
114 :
115 : if ( char_attr == nullptr ) return -1;
116 :
117 : //printf ("%s | %s\r\n", ffdc_filename , char_attr );
118 :
119 : if ( m_compare( char_attr, "" ) == 1 ) return -1;
120 :
121 : //printf ("%s | %s\r\n", ffdc_filename , char_attr );
122 :
123 : int last = m_last_with ( ffdc_filename, char_attr );
124 :
125 : //printf( "last = %d\r\n", last );
126 :
127 : if ( last == -1 ) {
128 :  return -1;
129 : }
130 :
131 : return 1;
132 :}
133 :
134 ://
135 :// Return Value
136 ://
137 :// char* list_dummy_ary
138 ://
139 :// Not extern, excluded, int, number of strings which you put with put_string.
140 ://
141 :int FileControler::get_strings_number () {
142 : return list_dummy_ary_index;
143 :}
144 :
145 :
146 ://
147 :// Return Value
148 ://
149 :// char* list_dummy_ary
150 ://
151 :// Not extern, char string array.
152 ://
153 :char** FileControler::get_strings () {
154 : return list_dummy_ary;
155 :}
156 :
157 ://
158 :// Parameters
159 ://
160 :// char* str
161 ://
162 :// char string which is going to be copiied in memories.
163 ://
164 :// Return Value
165 ://
166 :// char* mem
167 ://
168 :// copied char string from str.
169 ://
170 :char* FileControler::copyof ( char* str ) {
171 : int ac = array_count(str);
172 : char* mem = (char*)malloc( sizeof(char) * (ac + 1) );
173 :
174 : for ( int i=0; i<ac; i++) {
175 :  mem[i] = str[i];
176 : }
177 :
178 : mem[ac] = '\0';
179 : return mem;
180 :}
181 :
182 :
183 ://
184 :// Return Value
185 ://
186 :// char* str
187 ://
188 :// Not extern, char string list_dummy_ary.
189 ://
190 :char** FileControler::put_string ( char* str ) {
191 :
192 : printf("index: %d str %s \r\n", list_dummy_ary_index,  str );
193 :
194 : if ( list_dummy_ary_index == 0 ) {
195 :  list_dummy_ary = (char **) malloc( sizeof(char*) );
196 :  list_dummy_ary_index = 1;
197 :  list_dummy_ary[ 0 ] = (char*) str;
198 : } else {
199 :  list_dummy_ary_index++;
200 :  list_dummy_ary = (char **) realloc( list_dummy_ary, sizeof(char*)* list_dummy_ary_index );
201 :  list_dummy_ary[ list_dummy_ary_index - 1 ] = (char*)str;
202 : }
203 : printf("index: %d str %s|%s \r\n", list_dummy_ary_index, list_dummy_ary[ list_dummy_ary_index - 1 ], str );
204 :
205 : return list_dummy_ary;
206 :}
207 :
208 ://
209 ://
210 ://
211 ://
212 ://
213 :void FileControler::print_strings () {
214 :
215 : for ( int i=0; i<list_dummy_ary_index; i++) {
216 :  printf("list_dummy_ary[%d]=%s\r\n", i, (char*)list_dummy_ary[i] );
217 : }
218 :
219 :}
220 :
221 ://
222 ://
223 ://
224 ://
225 ://
226 :char* FileControler::to_directory(char* lpcstr_path, char* filename )
227 :{
228 : int comp1 = m_compare ( filename, (char*)".");
229 : // printf( "comp1: %d\r\n", comp1 );
230 :
231 : int comp2 = m_compare ( filename, (char*)"..");
232 : // printf( "comp2: %d\r\n", comp2 );
233 :
234 : int index = m_last_with( (char *) lpcstr_path, "\\" );
235 : // printf( "index: %d\r\n", index );
236 :
237 : if ( (index > 0) && (comp1 != 1) && (comp2 != 1) ) {
238 :
239 :  char* string_directory = substring( (char *) lpcstr_path, 0, index + 1 );
240 :  //printf("string_directory1: %s\r\n", string_directory );
241 :  char* to_directory = m_concat( string_directory, filename );
242 :  // printf("to_directory: %s\r\n", to_directory );
243 :
244 :  return to_directory;
245 :
246 : }
247 :
248 : return NULL;
249 :}
250 :
251 ://
252 :// it refers all to
253 :// https://stackoverflow.com/questions/11362771/list-files-in-directory-c
254 :// https://bituse.info/winapi/31
255 ://
256 :// Parameters
257 ://
258 :// lpcstr_path
259 ://
260 :// Directory name which is going to be found files.
261 ://
262 :// Return Value
263 ://
264 :// void
265 ://
266 :void FileControler::List_Files(char* lpcstr_path )
267 :{
268 : WIN32_FIND_DATA ffd;
269 : HANDLE hFind = INVALID_HANDLE_VALUE;
270 : char* ffdc_filename;
271 :
272 :    // Prepare string for use with FindFile functions.  First, copy the
273 :    // string to a buffer, then append '\*' to the directory name.
274 : hFind = FindFirstFile( TEXT( lpcstr_path ), &ffd );
275 : ffdc_filename = TEXT( ffd.cFileName );
276 :
277 : printf ( "filename: %s\r\n", ffdc_filename );
278 : printf ( "lpcstr_path: %s\r\n", TEXT( lpcstr_path ) );
279 :
280 :    // List all the files in the directory with some info about them.
281 : do
282 : {
283 :  ffdc_filename = TEXT( ffd.cFileName );
284 :  if ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
285 :  {
286 :   //If it's a directory, nothing should happen. Just continue with the next file.
287 :   char* char_directory = to_directory ( lpcstr_path, ffdc_filename );
288 :   if ( char_directory == NULL ) {
289 :    printf( "skip away : %s\r\n", char_directory );
290 :   } else {
291 :    char* listfiles = (char*) m_concat( (char*)char_directory, (char*)"\\*" );
292 :    this->List_Files( (char*) listfiles );
293 :   }
294 :   free ( char_directory );
295 :  }
296 :  else
297 :  {
298 :   printf( "filename 2:%s\r\n", ffdc_filename );
299 :
300 :   if ( file_type ( (char*)ffdc_filename, (char*)"*.php") == 1 ) {
301 :    char* char_fullpath = m_fullpath ( lpcstr_path, ffdc_filename );
302 :
303 :    // exit(-1);
304 :    // parser starts 20190103
305 ://    printf ("put_string %s\r\n", char_fullpath );
306 ://    put_string( char_fullpath );
307 :    // exit(-1);
308 ://    int php_return = php_parse( char_fullpath );
309 :   }
310 :
311 :  }
312 :  free ( ffdc_filename );
313 : }
314 : while ( FindNextFile(hFind, &ffd ) != 0);
315 :
316 : free ( lpcstr_path );
317 :
318 : FindClose(hFind);
319 :}
320 :
321 :// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findnextfilea
322 :// BOOL FindNextFileA(
323 ://   HANDLE             hFindFile,
324 ://   LPWIN32_FIND_DATAA lpFindFileData
325 :// );
326 ://
327 :// Parameters
328 ://
329 :// hFindFile
330 ://
331 :// The search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.
332 ://
333 :// lpFindFileData
334 ://
335 :// A pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.
336 :
337 :// Return Value
338 ://
339 :// If the function succeeds, the return value is nonzero and the lpFindFileData parameter contains information about the next file or directory found.
340 ://
341 :// If the function fails, the return value is zero and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
342 ://
343 :// If the function fails because no more matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
344 ://
345 :// Remarks
346 ://
347 :// This function uses the same search filters that were used to create the search handle passed in the hFindFile parameter. For additional information, see FindFirstFile and FindFirstFileEx.
348 ://
349 :// The order in which the search returns the files, such as alphabetical order, is not guaranteed, and is dependent on the file system. If the data must be sorted, the application must do the ordering after obtaining all the results.
350 ://
351 :// Note  In rare cases or on a heavily loaded system, file attribute information on NTFS file systems may not becurrent at the time this function is called. To be assured of getting the current NTFS file system fileattributes, call the GetFileInformationByHandle function.
352 ://
353 :// 
354 :// The order in which this function returns the file names is dependent on the file system type. With the NTFSfile system and CDFS file systems, the names are usually returned in alphabetical order. With FAT file systems,the names are usually returned in the order the files were written to the disk, which may or may not be inalphabetical order. However, as stated previously, these behaviors are not guaranteed.
355 :// If the path points to a symbolic link, the WIN32_FIND_DATA buffer contains information about the symbolic link, not the target.
356 ://
357 :// In Windows 8 and Windows Server 2012, this function is supported by the following technologies.
358 ://
359 ://
360 ://
361 :// Technology
362 ://
363 :// Supported
364 ://
365 :// Server Message Block (SMB) 3.0 protocol  Yes 
366 :// SMB 3.0 Transparent Failover (TFO)  Yes 
367 :// SMB 3.0 with Scale-out File Shares (SO)  Yes 
368 :// Cluster Shared Volume File System (CsvFS)  Yes 
369 :// Resilient File System (ReFS)  Yes 
370 ://
371 :// Transacted Operations
372 :// If there is a transaction bound to the file enumeration handle, then the files that are returned are subject to transaction isolation rules.
373 :// Examples
374 ://
375 :// For an example, see Listing the Files in a Directory.
376 ://
377 ://
378 :// Requirements
379 ://
380 ://
381 ://
382 ://
383 ://
384 ://
385 ://
386 ://
387 :// Minimum supported client Windows XP [desktop apps | UWP apps]
388 :// Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
389 :// Target Platform Windows
390 :// Header fileapi.h (include Windows.h)
391 :// Library Kernel32.lib
392 :// DLL Kernel32.dll
393 ://
394 :// See Also
395 ://
396 :// File Management Functions
397 ://
398 :// FindClose
399 ://
400 :// FindFirstFile
401 ://
402 :// FindFirstFileEx
403 ://
404 :// GetFileAttributes
405 ://
406 :// SetFileAttributes
407 ://
408 :// Symbolic Links
409 ://
410 :// WIN32_FIND_DATA
411 ://
412 ://
413 :
414 :
415 :
416 :
417 :
418 :
419 ://
420 ://
421 ://
422 ://
423 ://
424 ://
425 :char* FileControler::m_fullpath_001 ( char *strPath,  char *filename ) {
426 :
427 : int index = m_last_with( (char *) strPath, "\\" );
428 :
429 : char* string_directory = substring( (char *) strPath, 0, index + 1 );
430 : printf("string_directory3: %s\r\n", string_directory );
431 : char* fullpath = m_concat( string_directory, (char *) filename );
432 : printf("filename fullpath = %s\r\n", fullpath );
433 :
434 : return fullpath;
435 :}
436 :
437 ://
438 ://
439 ://
440 ://
441 ://
442 ://
443 :char* FileControler::m_fullpath ( char* strPath, char* filename ) {
444 :
445 : int index = m_last_with( (char *) strPath, "\\" );
446 :
447 : char* string_directory = substring( (char *) strPath, 0, index + 1 );
448 : printf("string_directory3: %s\r\n", string_directory );
449 : char* fullpath = m_concat( (char *)string_directory, (char *) filename );
450 : printf("filename fullpath_c = %s\r\n", fullpath );
451 :
452 : return fullpath;
453 :}
454 :
455 ://
456 ://
457 ://
458 ://
459 ://
460 ://
461 ://
462 :void FileControler::Sub_List_Files ()
463 :{
464 :    WIN32_FIND_DATA ffd;
465 : HANDLE hFind = FindFirstFile(TEXT("C:\\Users\\Andre\\Dropbox\\Programmering privat\\Diablo III DPS Calculator\\Debug\\SavedProfiles\\*"), &ffd);
466 :
467 : if (INVALID_HANDLE_VALUE != hFind)
468 : {
469 :
470 :  do
471 :  {
472 :   //...
473 :   int a = 0;
474 :
475 :  } while(FindNextFile(hFind, &ffd) != 0);
476 :
477 :  FindClose( hFind );
478 :
479 : } else {
480 :
481 :  // Report failure.
482 :  int b = 0;
483 : }
484 :
485 :}
486 :
487 :