ARO : ANTIREADONLY -> MODIFIER LES FICHIERS EN LECTURE SEULE EN FICHIER "NORM
cs_gaya
Messages postés47Date d'inscriptionjeudi 8 août 2002StatutMembreDernière intervention10 août 2002
-
8 août 2002 à 03:28
Utilisateur anonyme -
14 déc. 2004 à 19:27
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.
Mais bon ça fait quelques années que j'ai posté cette source donc depuis je me suis amélioré et je fait maintenant dans l'OpenSource, fini windows ;)
En tous les cas, bravo pour ton code !
cs_zorlac
Messages postés17Date d'inscriptionlundi 6 janvier 2003StatutMembreDernière intervention14 août 2005 14 déc. 2004 à 15:39
pas mal pour un jeune, essaie de prendre l'habitude d'initialiser tes structure par un memset ou zeromemory pour être sûr qu'il va allouer sur le stack la taille nécessaire, dans mon exemple ici en bas j'ai aussi oublié de le faire ;-)
SYSTEMTIME CUpdateMaker::GetSystTimeFile(std::string filename)
{
WIN32_FILE_ATTRIBUTE_DATA FileInformation;
memset(&FileInformation, 0, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
FILETIME lastwrite;
memset(&lastwrite, 0, sizeof(FILETIME));
SYSTEMTIME Systlastwrite;
GetFileAttributesEx(filename.c_str(), GetFileExInfoStandard, &FileInformation);
lastwrite = FileInformation.ftLastWriteTime;
//needed otherwise utc => localtime!
FileTimeToLocalFileTime(&lastwrite, &lastwrite);
FileTimeToSystemTime(&lastwrite, &Systlastwrite);
return Systlastwrite;
}
int CUpdateMaker::CompareWORD(WORD value1, WORD value2)
{
if(value1 > value2)
return 1;
else if(value1 < value2)
return -1;
else
return 0;
}
int CUpdateMaker::CompareSystemTime(SYSTEMTIME time1, SYSTEMTIME time2)
{
//1:newer, -1:older, 0:same
//compare year
int result = CompareWORD(time1.wYear, time2.wYear);
if( result !=0)
return result;
//compare month
result = CompareWORD(time1.wMonth, time2.wMonth);
if( result !=0)
return result;
//compare day
result = CompareWORD(time1.wDay, time2.wDay);
if( result !=0)
return result;
//compare hour
result = CompareWORD(time1.wHour, time2.wHour);
if( result !=0)
return result;
//compare minute
result = CompareWORD(time1.wMinute, time2.wMinute);
if( result !=0)
return result;
//compare second
result = CompareWORD(time1.wSecond, time2.wSecond);
if( result !=0)
return result;
int CUpdateMaker::ManageAll(void)
{
for(int i=0; i < m_PathInputArray.size() ; i++)
{
RecursiveBuild(m_PathInputArray[i].value, m_PathOutputArray[i].value );
}
return 0;
}
int CUpdateMaker::RecursiveBuild(std ::string AttributesIn, std::string AttributesOut)
{
int success;
AttributesIn = CheckSlashEndPath(AttributesIn);
AttributesOut = CheckSlashEndPath(AttributesOut);
CreateRecursiveDirectory(AttributesOut);
WIN32_FIND_DATA l_FindData;
std::string SourcePath = AttributesIn + "*.*";
std::string DestinationPath = AttributesOut + "*.*";
std::string in, out;
//it passed by . and ..!
HANDLE l_hFindFile=::FindFirstFile(SourcePath.c_str(), &l_FindData);
if (INVALID_HANDLE_VALUE==l_hFindFile)
{
WriteLog("ERROR : We cannot find any files\n");
}
Hum... Il me semble qu'il scanne aussi les sous-dossiers, non ?
Je cite "ATTENTION ! Il scanne aussi les sous - dossiers !".
Sinon pour le FindNextFile() qui renvoit une valeur je suis d'accord avec toi, mais bon quesque tu veut... Je suis un débutant, moi ^^
TheLudo2013
Messages postés77Date d'inscriptionmardi 12 novembre 2002StatutMembreDernière intervention18 octobre 2007 12 nov. 2002 à 02:47
Dommage que ta fonction qui recherche les fichiers ne cherche pas dans tous les sous répertoire. De plus je trouve bourin de comparer le nom du fichier précedent avec celui trouvé (sois-disant pour voir quand il y en a plus) : a tu oublie que FindNextFile() avait une valeur de retour(lol).
14 déc. 2004 à 19:27
Hé bin, c'est impressionnant ;)
Mais bon ça fait quelques années que j'ai posté cette source donc depuis je me suis amélioré et je fait maintenant dans l'OpenSource, fini windows ;)
En tous les cas, bravo pour ton code !
14 déc. 2004 à 15:39
SYSTEMTIME CUpdateMaker::GetSystTimeFile(std::string filename)
{
WIN32_FILE_ATTRIBUTE_DATA FileInformation;
memset(&FileInformation, 0, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
FILETIME lastwrite;
memset(&lastwrite, 0, sizeof(FILETIME));
SYSTEMTIME Systlastwrite;
GetFileAttributesEx(filename.c_str(), GetFileExInfoStandard, &FileInformation);
lastwrite = FileInformation.ftLastWriteTime;
//needed otherwise utc => localtime!
FileTimeToLocalFileTime(&lastwrite, &lastwrite);
FileTimeToSystemTime(&lastwrite, &Systlastwrite);
return Systlastwrite;
}
int CUpdateMaker::CompareWORD(WORD value1, WORD value2)
{
if(value1 > value2)
return 1;
else if(value1 < value2)
return -1;
else
return 0;
}
int CUpdateMaker::CompareSystemTime(SYSTEMTIME time1, SYSTEMTIME time2)
{
//1:newer, -1:older, 0:same
//compare year
int result = CompareWORD(time1.wYear, time2.wYear);
if( result !=0)
return result;
//compare month
result = CompareWORD(time1.wMonth, time2.wMonth);
if( result !=0)
return result;
//compare day
result = CompareWORD(time1.wDay, time2.wDay);
if( result !=0)
return result;
//compare hour
result = CompareWORD(time1.wHour, time2.wHour);
if( result !=0)
return result;
//compare minute
result = CompareWORD(time1.wMinute, time2.wMinute);
if( result !=0)
return result;
//compare second
result = CompareWORD(time1.wSecond, time2.wSecond);
if( result !=0)
return result;
//compare millisecondes
//result = CompareWORD(time1.wMilliseconds, time2.wMilliseconds);
//if( result !=0)
// return result;
return 0;
}
int CUpdateMaker::ManageAll(void)
{
for(int i=0; i < m_PathInputArray.size() ; i++)
{
RecursiveBuild(m_PathInputArray[i].value, m_PathOutputArray[i].value );
}
return 0;
}
int CUpdateMaker::RecursiveBuild(std ::string AttributesIn, std::string AttributesOut)
{
int success;
AttributesIn = CheckSlashEndPath(AttributesIn);
AttributesOut = CheckSlashEndPath(AttributesOut);
CreateRecursiveDirectory(AttributesOut);
WIN32_FIND_DATA l_FindData;
std::string SourcePath = AttributesIn + "*.*";
std::string DestinationPath = AttributesOut + "*.*";
std::string in, out;
//it passed by . and ..!
HANDLE l_hFindFile=::FindFirstFile(SourcePath.c_str(), &l_FindData);
if (INVALID_HANDLE_VALUE==l_hFindFile)
{
WriteLog("ERROR : We cannot find any files\n");
}
while(l_hFindFile!=INVALID_HANDLE_VALUE)
{
std::string EntryName(l_FindData.cFileName);
in = AttributesIn + EntryName;
out = AttributesOut + EntryName;
if(EntryName != "." && EntryName != ".." && FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(in.c_str()))
RecursiveBuild(in, out);
if(EntryName != "." && EntryName != ".." && FILE_ATTRIBUTE_DIRECTORY != GetFileAttributes(in.c_str()))
{
success = CopyFileToOutput(in, out);
if(success == -1)
return -1;
}
if(0==::FindNextFile(l_hFindFile, &l_FindData))
break;
}
::FindClose(l_hFindFile);
return 0;
}
int CUpdateMaker::CopyFileToOutput(std::string AllInPath, std::string AllOutPath)
{
int success;
DWORD resError;
std::string outlog;
if(_access(AllOutPath.c_str(), 0) == -1)
{
success = CopyFile(AllInPath.c_str(), AllOutPath.c_str(), FALSE);
if(success == 0)
{
WriteLog("ERROR: Cannot copy file to destination\n");
return -1;
}
outlog = "File copied in : " + AllOutPath + "\n";
WriteLog(outlog.c_str());
}
else
{
int result = CompareSystemTime(GetSystTimeFile(AllInPath), GetSystTimeFile(AllOutPath));
if(result == 1)
{
success = SetFileAttributes(AllOutPath.c_str(), FILE_ATTRIBUTE_NORMAL);
if(success == 0)
{
WriteLog("ERROR: Cannot set file attributes\n");
return -1;
}
success = DeleteFile(AllOutPath.c_str());
if(success == 0)
{
resError = GetLastError();
WriteLog("ERROR: Cannot delete file \n");
return -1;
}
success = CopyFile(AllInPath.c_str(), AllOutPath.c_str(), FALSE);
if(success == 0)
{
WriteLog("ERROR: Cannot copy file to destination\n");
return -1;
}
outlog = "File copied in : " + AllOutPath + "\n";
WriteLog(outlog.c_str());
}
}
return 0;
}
12 nov. 2002 à 07:12
Je cite "ATTENTION ! Il scanne aussi les sous - dossiers !".
Sinon pour le FindNextFile() qui renvoit une valeur je suis d'accord avec toi, mais bon quesque tu veut... Je suis un débutant, moi ^^
12 nov. 2002 à 02:47
8 août 2002 à 14:55
a+
8 août 2002 à 03:28
Dommage que la largeur d'affichage du site en réduise la lisibilité.
Bonne continuation JMGR, @+