View Single Post
Old 23-01-2013, 11:36   #10
Vincenzo1968
Bannato
 
Iscritto dal: Mar 2008
Cittā: Villabate(PA)
Messaggi: 2515
Uno spezzone del codice di AnonimoVeneziano per il contest 19(punto A)

Codice:
#if !_WIN32
bool load_files(const char* directory) {

  DIR* RESTRICT dir_obj = opendir(directory);
  struct dirent* RESTRICT dir_struct;
  char * RESTRICT * RESTRICT files_names;
  const uint16_t path_size = strlen(directory);

  int count = 0;

  if (dir_obj == NULL)
    return TRUE;

  while ((dir_struct = readdir(dir_obj)) != NULL) {
    //printf("File name: %s\n", dir_struct->d_name);
    if (!IS_SPECIAL_DIRECTORY(dir_struct->d_name))
      ++file_nums;
  }

  rewinddir(dir_obj);

  files_names = malloc(file_nums*sizeof(char*));  

  while ((dir_struct = readdir(dir_obj)) != NULL) {
    if (!IS_SPECIAL_DIRECTORY(dir_struct->d_name)) {
      int name_size = strlen(dir_struct->d_name);
      int size = path_size + name_size + 1;
 
      files_names[count] = malloc(sizeof(char)*size);
      strncpy(files_names[count], directory, path_size);
      strncpy(files_names[count]+path_size, dir_struct->d_name, name_size);
      //printf("%s\n", files_names[count]);
      ++count;
    }
  }

  closedir(dir_obj);

  //printf("%d\n", file_nums);
  //printf("Size of dir: %d\n", path_size);
  
  // Read files
  read_files(files_names, file_nums);

  for (int i = 0; i < file_nums; ++i)
    free((void*)files_names[i]);

  free((void**)files_names);
 
  return FALSE;
}
#else

bool load_files(const char* directory) {

  WIN32_FIND_DATA find_data;
  HANDLE hFind;
  char * RESTRICT * RESTRICT files_names;
  const uint16_t path_size = strlen(directory)-1;

  int count = 0;
  hFind = FindFirstFile(directory, &find_data);
  if ( hFind == INVALID_HANDLE_VALUE)
    return TRUE;

  do  {
    //printf("File name: %s\n", dir_struct->d_name);
	if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        ++file_nums;
  } while (FindNextFile(hFind, &find_data));

  FindClose(hFind);
  hFind = FindFirstFile(directory, &find_data);

  files_names = 
	  (char* RESTRICT * RESTRICT) malloc(file_nums*sizeof(char*));  

  do {
    if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
      int name_size = strlen(find_data.cFileName);
      int size = path_size + name_size + 1;
      files_names[count] = 
		  (char* RESTRICT)malloc(sizeof(char)*size);
      strncpy(files_names[count], directory, path_size);
	  strncpy(files_names[count]+path_size, find_data.cFileName, name_size+1);
      //printf("%s\n", files_names[count]);
      ++count;
    }
  } while (FindNextFile(hFind, &find_data));

  FindClose(hFind);

  //printf("%d\n", file_nums);
  //printf("Size of dir: %d\n", path_size);
  
  // Read files
  read_files(files_names, file_nums);

  for (int i = 0; i < file_nums; ++i)
    free((void*)files_names[i]);

  free((void**)files_names);
 
  return FALSE;
}

#endif
Vincenzo1968 č offline   Rispondi citando il messaggio o parte di esso