Journal droid_rage's Journal: Why "Learn C++ in 21 days!" Doesn't work
OMG, like, I was totally looking through _Production code_, when I found this little gem. I'm not even a particularly good coder, but please. This is a piece of production code! If this isn't the worst code I've ever read, I'm not sure what is. Somebody fell asleep in class when they were learning about recursion and functions.
----------------------
// INFCopy.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WIN32_FIND_DATA FileName;
HANDLE hFind[20]; //Handle for Directory Find Data. Each array segment is one subdir
CHAR szDestinationDir[MAX_PATH]; // c:\windows\inf
CHAR szSourceDir[MAX_PATH]; // Set to c:\windows\options\cabs\windows\inf
CHAR szCopyFile[MAX_PATH];
CHAR szCompareFile[MAX_PATH];
CHAR szRootDest[MAX_PATH];
CHAR szNewDir[MAX_PATH];
CHAR SubDirsSource[20][MAX_PATH]; //This keeps track of all upper level dirs in Source
CHAR SubDirsDest[20][MAX_PATH]; //This keeps track of all upper level dirs in Destination
UINT DirLevel; // Array counter for SubDirs and hFind functions
strcpy(szSourceDir,"c:\\windows\\options\\cabs\\windows\\inf");
strcpy(szRootDest,"c:\\windows\\inf");
strcpy(szDestinationDir,"c:\\windows\\inf");
SetCurrentDirectory(szSourceDir);
DirLevel = 0;
sprintf(SubDirsSource[DirLevel],szSourceDir);
sprintf(SubDirsDest[DirLevel],szDestinationDir);
hFind[DirLevel] = FindFirstFile("*.*", &FileName);
while(DirLevel >= 0) // If DirLevel is at 0, program should break, but just in case...
{
if(FileName.dwFileAttributes == 16) // If this is a directory?
{
if(((strcmp(FileName.cFileName, ".")) == 0)||((strcmp(FileName.cFileName,"..")) == 0))
{
if(!FindNextFile(hFind[DirLevel], &FileName)) break; //break if it's . or ..
else continue;
}
DirLevel++;
SetCurrentDirectory(FileName.cFileName);
sprintf(SubDirsSource[DirLevel], FileName.cFileName);
sprintf(szNewDir,"%s\\%s\\", szDestinationDir,FileName.cFileName);
CreateDirectory(szNewDir,0); //Creating the Dir is just so much easier
printf("Copying %s\n",FileName.cFileName); //Hey man, what're ya doing?
sprintf(SubDirsDest[DirLevel], FileName.cFileName);
sprintf(szDestinationDir,"%s", szNewDir);
if((hFind[DirLevel] = FindFirstFile("*.*", &FileName)) == INVALID_HANDLE_VALUE)
{
//If there are no files in the directory...
FindClose(hFind[DirLevel]); //Close the find handle
DirLevel--; //backtrack to previous array
SetCurrentDirectory(SubDirsSource[DirLevel]); //set the directory to previous array dir
sprintf(szDestinationDir, "%s",SubDirsDest[DirLevel]); //set destination " "
FindNextFile(hFind[DirLevel], &FileName); //Find the next file in previous dir
}
else continue;
}//This ends the dir copy function if statement.
else
{
/* If it's not a Dir, then it's a file, and we like copying files, don't we Bobby?*/
sprintf(szCopyFile,"%s\\%s", szDestinationDir, FileName.cFileName); //sets szCopyFile to correct Dir and file name
CopyFile(FileName.cFileName, szCopyFile, false);
printf("Copying %s\n",FileName.cFileName);
sprintf(szCompareFile,"%s",FileName.cFileName);
FindNextFile(hFind[DirLevel], &FileName);
if(strcmp(FileName.cFileName, szCompareFile) == 0)
{
if(DirLevel == 0) break;
FindClose(hFind[DirLevel]);
DirLevel--;
SetCurrentDirectory(SubDirsSource[DirLevel]);
sprintf(szDestinationDir, "%s\\",SubDirsDest[DirLevel]);
FindNextFile(hFind[DirLevel], &FileName);
}
}
} //This ends Topmost While statement
printf("Finished copying files");
return 0;
}
----------------------
// INFCopy.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WIN32_FIND_DATA FileName;
HANDLE hFind[20];
CHAR szDestinationDir[MAX_PATH];
CHAR szSourceDir[MAX_PATH];
CHAR szCopyFile[MAX_PATH];
CHAR szCompareFile[MAX_PATH];
CHAR szRootDest[MAX_PATH];
CHAR szNewDir[MAX_PATH];
CHAR SubDirsSource[20][MAX_PATH];
CHAR SubDirsDest[20][MAX_PATH];
UINT DirLevel;
strcpy(szSourceDir,"c:\\windows\\options\\cabs\\windows\\inf");
strcpy(szRootDest,"c:\\windows\\inf");
strcpy(szDestinationDir,"c:\\windows\\inf");
SetCurrentDirectory(szSourceDir);
DirLevel = 0;
sprintf(SubDirsSource[DirLevel],szSourceDir);
sprintf(SubDirsDest[DirLevel],szDestinationDir);
hFind[DirLevel] = FindFirstFile("*.*", &FileName);
while(DirLevel >= 0)
{
if(FileName.dwFileAttributes == 16)
{
if(((strcmp(FileName.cFileName, ".")) == 0)||((strcmp(FileName.cFileName,"..")) == 0))
{
if(!FindNextFile(hFind[DirLevel], &FileName)) break;
else continue;
}
DirLevel++;
SetCurrentDirectory(FileName.cFileName);
sprintf(SubDirsSource[DirLevel], FileName.cFileName);
sprintf(szNewDir,"%s\\%s\\", szDestinationDir,FileName.cFileName);
CreateDirectory(szNewDir,0);
printf("Copying %s\n",FileName.cFileName);
sprintf(SubDirsDest[DirLevel], FileName.cFileName);
sprintf(szDestinationDir,"%s", szNewDir);
if((hFind[DirLevel] = FindFirstFile("*.*", &FileName)) == INVALID_HANDLE_VALUE)
{
FindClose(hFind[DirLevel]);
DirLevel--;
SetCurrentDirectory(SubDirsSource[DirLevel]);
sprintf(szDestinationDir, "%s",SubDirsDest[DirLevel]);
FindNextFile(hFind[DirLevel], &FileName);
}
else continue;
}//This ends the dir copy function if statement.
else
{
sprintf(szCopyFile,"%s\\%s", szDestinationDir, FileName.cFileName);
CopyFile(FileName.cFileName, szCopyFile, false);
printf("Copying %s\n",FileName.cFileName);
sprintf(szCompareFile,"%s",FileName.cFileName);
FindNextFile(hFind[DirLevel], &FileName);
if(strcmp(FileName.cFileName, szCompareFile) == 0)
{
if(DirLevel == 0) break;
FindClose(hFind[DirLevel]);
DirLevel--;
SetCurrentDirectory(SubDirsSource[DirLevel]);
sprintf(szDestinationDir, "%s\\",SubDirsDest[DirLevel]);
FindNextFile(hFind[DirLevel], &FileName);
}
}
}
printf("Finished copying files");
return 0;
}
Why "Learn C++ in 21 days!" Doesn't work More Login
Why "Learn C++ in 21 days!" Doesn't work
Slashdot Top Deals