The Sage Continues: File Handling



  • Some useful functions anyone should have in their toolbox:

    bool FileAdmin::CreateFolder(const std::string & folder)
    {
     char *pLkUp, *pCreaDir, *pFolder, ptr, szHlp[_MAX_DIR];
     int nLen;
     
     pFolder=(TCHAR
    )malloc((folder.length()+1)sizeof(TCHAR));
     folder.copy(pFolder,folder.length());
     pFolder[folder.length()]=0;
     pCreaDir=(TCHAR
    )malloc((folder.length()+1)*sizeof(TCHAR));
     *pCreaDir=0;
     pLkUp=pFolder;

     if(*pLkUp=='\\' && *(pLkUp+1)=='\\')
     {
       char *ptr2;
       if(ptr=strchr(pLkUp+2,'\\'))
       {
         if(ptr2=strchr(ptr+1,'\\'))
         {
           nLen=ptr2-pLkUp;
           strncpy(szHlp,pLkUp,nLen);
           szHlp[nLen]=0;
           strcat(pCreaDir, szHlp);
         }
         else
           strcpy(pCreaDir,pLkUp);   
       }
       if(!CheckFolder(std::string(pCreaDir)))
       {
         free(pCreaDir);
         free(pFolder);
        return (false);
       }
       if(ptr2==NULL)
       {
         free(pCreaDir);
         free(pFolder);
         return (true);
       }
       strcat(pCreaDir,"\\");
       pLkUp=ptr2+1;
     }

     while(ptr=strchr(pLkUp,'\\'))
     {
       nLen=ptr-pLkUp+1;
       strncpy(szHlp,pLkUp,nLen);
       szHlp[nLen]=0;
       strcat(pCreaDir, szHlp);
       // Check if target directory exists
       if(!CheckFolder(std::string(pCreaDir)))
         if(!CreateDirectory(pCreaDir,NULL))
         {
           free(pCreaDir);
          return (false);
         }
        pLkUp=ptr+1;
     }
     
     if (pLkUp)
     {
        strcat(pCreaDir, pLkUp);
        // Check if target directory exists
        if(!CheckFolder(std::string(pCreaDir)))
          if(!CreateDirectory(pCreaDir,NULL))
          {
            free(pCreaDir);
           return(0);
          }
     }
     free(pCreaDir);
     free(pFolder);
     return(true);
    }

    int FileAdmin::CheckFolder(const std::string & folder)
    {
     TCHAR szCurDir[_MAX_PATH];
     
     folder.copy(szCurDir,folder.length());
     szCurDir[folder.length()]=0;
     GetCurrentDirectory(sizeof(szCurDir),szCurDir);
     int rtn=SetCurrentDirectory(folder.c_str());
     SetCurrentDirectory(szCurDir);
     return (rtn); 
    }

     



  •  Given this is C++, have you considered using std::vector<TCHAR> to remove all that code doing manual memory management?  It would halve the size of the first function as well as making it hugely more readable.



  • @MET said:

    Given this is C++, have you considered using std::vector<TCHAR> to remove all that code doing manual memory management?  It would halve the size of the first function as well as making it hugely more readable.
     

    You're right, that is TRWTF. 



  • Or the fact that it won't compile if you compile with UNICODE enabled.  What's the point of using the "universal" TCHAR, if you're going to mix it with char* and std::string.  Someone's either been reading the MSDN docs too literally and taking it verbatim, or not reading it enough to realize what TCHAR actually is.

    We won't mention the obvious thing that is std::string::c_str() either :P 

     

    This does remind me of my first job out of school.  C programmer hacking their way through C++.  We had lots of WTFy code like that too. 



  • @GuntherVB said:


     folder.copy(szCurDir,folder.length());
     szCurDir[folder.length()]=0;
     GetCurrentDirectory(sizeof(szCurDir),szCurDir);
     int rtn=SetCurrentDirectory(folder.c_str());
     SetCurrentDirectory(szCurDir);
     return (rtn); 
    }

     

    Holy jeebus! Here's a coupla bucks, go buy that guy some whitespace!

     



  • @DaveK said:

    go buy that guy some whitespace!


    Agreed - that's something that annoys me even more than Hungarian or brace style wars.



  • @mfah said:

    @DaveK said:

    go buy that guy some whitespace!


    Agreed - that's something that annoys me even more than Hungarian or brace style wars.

    I bet the author of the original code types his emails as a single paragraph as well.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.