Matt23488

Directory stuff

Aug 3rd, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. //Checks if a directory exists.
  2. bool path_exists(char *directory)
  3. {
  4.     DWORD returnvalue;
  5.     returnvalue = GetFileAttributes(directory);
  6.     if(returnvalue == ((DWORD)-1))
  7.     {
  8.         return false;
  9.     }
  10.     return true;
  11. }
  12.  
  13. //Checks if path refers to a letter drive instead of a sub-directory.
  14. bool is_letter_drive(char *path)
  15. {
  16.     char i;
  17.     char* temp_path;// = new char[256];
  18.     for(i = 'A'; i <= 'Z'; i++)
  19.     {
  20.         temp_path = g_strdup_printf ("%c:", i);//sprintf(temp_path, "%c:", i);
  21.         if(strcmp(path, temp_path) == 0)
  22.         {
  23.             g_free (temp_path);//Not needed without Gtk
  24.             break;
  25.         }
  26.         g_free (temp_path);//Not needed without Gtk
  27.         temp_path = g_strdup_printf ("%c:", tolower(i));//sprintf(temp_path, "%c:", tolower(i));
  28.         if(strcmp(path, temp_path) == 0)
  29.         {
  30.             g_free (temp_path);//Not needed without Gtk
  31.             break;
  32.         }
  33.         g_free (temp_path);//Not needed without Gtk
  34.     }
  35.     if(i <= 'Z')
  36.     {
  37.         return true;
  38.     }
  39.     return false;
  40. }
  41.  
  42. //Attempts to create a directory based on path. Returns false if unsuccessful. Supports recursive directories.
  43. bool create_directory(char *path)
  44. {
  45.     int i = 0;
  46.     char *sub = new char[strlen(path) + 1];
  47.     for(i; i < strlen(path); i++)
  48.     {
  49.         if(path[i] == '\\')
  50.         {
  51.             sub[i] = '\0';
  52.             if(!is_letter_drive(sub))
  53.             {
  54.                 if(!path_exists(sub) && !CreateDirectory(sub, NULL))
  55.                 {
  56.                     return false;
  57.                 }
  58.             }
  59.         }
  60.         sub[i] = path[i];
  61.     }
  62.     return true;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment