Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. char userpath[4096];
  2. char logfilename[4096];
  3.  
  4. // OS specific (Windows)
  5. int FolderExists(const char * file)
  6. {
  7.     DWORD returnvalue;
  8.     WCHAR temp[512];
  9.    
  10.     MultiByteToWideChar(0, 0, file, 5, temp, 50);
  11.    
  12.     LPCWSTR widefile = temp;
  13.  
  14.     returnvalue = GetFileAttributesW(widefile);
  15.  
  16.     if (returnvalue == ((DWORD) - 1))
  17.         return FALSE;
  18.     else
  19.         return TRUE;
  20. }
  21.  
  22. // OS specific (Windows)
  23. void MakeADirectory(char * dir)
  24. {
  25.     CreateDirectoryA(dir, NULL);
  26.  
  27.     /*printf("Now creating: ");
  28.     printf(dir);
  29.     printf("\n");*/
  30.    
  31.     return;
  32. }
  33.  
  34. void InitLog()
  35. {
  36.     // Find the user's profile directory
  37.     char * tempfilename;
  38.     FILE *fp;
  39.    
  40.     // OS Specific (Windows)
  41.     // ---
  42.     size_t len;
  43.     _dupenv_s(&tempfilename, &len, "USERPROFILE");
  44.  
  45.     if (!tempfilename)
  46.     {
  47.         printf("Cannot find %USERPROFILE% environment variable. Now exiting.\n");
  48.         exit(1);
  49.     }
  50.     // ---
  51.    
  52.     strcpy_s (logfilename, 4096, tempfilename);
  53.  
  54.     free(tempfilename);
  55.    
  56.     // Make sure the whole path to the log file exists
  57.     strcat_s (logfilename, 4096, "\\My Documents");
  58.  
  59.     if (!FolderExists(logfilename))
  60.     {
  61.         // My Documents is missing!
  62.         MakeADirectory(logfilename);
  63.     }
  64.  
  65.     strcat_s (logfilename, 4096, "\\My Games");
  66.    
  67.     if (!FolderExists(logfilename))
  68.     {
  69.         // My Documents\My Games is missing!
  70.         MakeADirectory(logfilename);
  71.     }
  72.    
  73.     strcat_s (logfilename, 4096, "\\Dusk Software");
  74.    
  75.     if (!FolderExists(logfilename))
  76.     {
  77.         // My Documents\My Games\Dusk Software is missing!
  78.         MakeADirectory(logfilename);
  79.     }
  80.  
  81.     strcpy_s (userpath, 4096, logfilename);
  82.  
  83.     strcat_s (logfilename, 4096, "\\LOG-fydos-adventure-game.txt");
  84.  
  85.     // See if the log file exists
  86.  
  87.     fopen_s(&fp, logfilename,"r");
  88.  
  89.     if (fp)
  90.     {
  91.         // Log file already exists
  92.  
  93.         // TODO: Check file size of log, trim it down if it is too big!
  94.     }
  95.     else
  96.     {
  97.         // Log file doesn't exist yet
  98.         if (fopen_s(&fp, logfilename, "a") != 0)
  99.         {
  100.             printf("Cannot open log file. Be sure that this game has write rights. Now exiting.\n");
  101.             exit(1);
  102.         }
  103.     }
  104.  
  105.     fclose(fp);
  106.  
  107.     return;
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement