Утилиты
реализации: C++, количество: 5
реализации(исходники)
+добавить
Разного рода небольшие полезные утилиты, использованные для каких-либо небольших задач.
Реализации: vb(1), C++(5), C#(2), sql(1), perl(1) +добавить реализацию
заполните необходимые поля!
1) zip-архивы с помощью ZLib, code #544[автор:-]
создание zip-архива с помощью библиотеки ZLib, C++, code #544
ссылка
+
рейтинг: 7/3,5.05(588), управление:
рейтинг: 7/3,5.05(588), управление:
// ПРИМЕР >> #include "stdafx.h" #include "SimpleZipArchive.h" int main(int argc, char* argv[]) { CSimpleZipArchive zip("d:\\_myzip.zip"); zip.AddFile("d:\\file1.txt"); zip.AddFile("d:\\file2.doc"); zip.AddFile("d:\\file3.xls"); zip.RemoveFile("d:\\file2.doc"); zip.ZipIt(); return 0; } // КЛАСС >> #if !defined(_SIMPLEZIPARCHIVE_H_INCLUDED_) #define _SIMPLEZIPARCHIVE_H_INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #pragma comment(lib, ".\\zlib\\zlib.lib") #include ".\zlib\zip.h" #include <sys/stat.h> #include <time.h> #include <map> #include <string> using namespace std; #define READ_BUFFER_SIZE 65535 //Спасибо тов. Bell-у за наше счастливое детство :) struct CaseInsensitiveLess : public binary_function<string, string, bool> { bool operator()(const string &s1, const string &s2) const { return stricmp(s1.c_str(), s2.c_str()) < 0; } }; typedef map<string, bool, CaseInsensitiveLess> STRCI_MAP; class CSimpleZipArchive { public: CSimpleZipArchive(const char* _zipName = NULL) { SetZipFileName(_zipName); } virtual ~CSimpleZipArchive() {} void SetZipFileName(const char* _zipName) { if (_zipName && 0 != *_zipName) m_ZipFileName = _zipName; } const char* GetZipFileName() const { return m_ZipFileName.c_str(); } bool RemoveFile(const char* _fileName) { if (!_fileName || 0 == *_fileName) return false; STRCI_MAP::iterator findIter = m_Files.find(string(_fileName)); if (findIter != m_Files.end()) { m_Files.erase(findIter); return true; } else return false; } bool AddFile(const char* _fileName) { if (!_fileName || 0 == *_fileName) return false; pair<STRCI_MAP::iterator, bool> result = m_Files.insert(STRCI_MAP::value_type(string(_fileName), true)); return result.second; } bool ZipIt() const { bool result = false; if ( 0 == m_Files.size()) return result; STRCI_MAP::const_iterator Iter = m_Files.begin(); zipFile zipfile = zipOpen(m_ZipFileName.c_str(), 0); if (zipfile != NULL) { while (Iter != m_Files.end()) { result = false; const char* srcFullFileName = Iter->first.c_str(); const char* p = strrchr(srcFullFileName, '\\'); const char* srcFileName = (NULL == p) ? srcFullFileName : p + 1; char read_buf[READ_BUFFER_SIZE]; FILE* file = fopen(srcFullFileName, "rb"); if (file != NULL) { zip_fileinfo zfileinfo = {0}; struct _stat file_stat = {0}; _fstat(_fileno(file), &file_stat); struct tm* file_time = localtime(&file_stat.st_mtime); tm_zip* zip_time = &zfileinfo.tmz_date; memcpy(zip_time, file_time, sizeof(tm_zip)); int compression = Z_BEST_COMPRESSION; if ( ZIP_OK == zipOpenNewFileInZip(zipfile, srcFileName, &zfileinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, compression)) { while(!feof(file)) { result = false; size_t count = fread( read_buf, sizeof(char), READ_BUFFER_SIZE, file ); if(!ferror(file)) { if ( ZIP_OK == zipWriteInFileInZip(zipfile, read_buf, count)) { result = true; continue; } else break; }else break; } result = result && (Z_OK == zipCloseFileInZip(zipfile)); } result = result && (0 == fclose(file)); } (result) ? Iter++ : Iter = m_Files.end(); } result = result && (Z_OK == zipClose(zipfile, NULL)); } return result; } private: STRCI_MAP m_Files; string m_ZipFileName; }; #endif // !defined(_SIMPLEZIPARCHIVE_H_INCLUDED_)
Понадобилось создавать zip-архивы с помощью библиотеки <a href="http://www.gzip.org/zlib/" class="link">ZLib</a>. Решил поделиться результатами своих изысканий, может кому и сгодится. Код приводится без всяких претензий на полноту и оригинальность, так... чтоб было с чего начать. Извиняюсь за "расширенную" индикацию ошибок в лице return false , если понадобится, дополнить труда не составит.
http://rsdn.ru/forum/Message.aspx?mid=220804&only=1
http://rsdn.ru/forum/Message.aspx?mid=220804&only=1
3) Определение сборки, code #555[автор:-]
Определение сборки. Как узнать какая сборка - управляемая или нет?, C++, code #555
ссылка
+
рейтинг: 7/3,5.04(652), управление:
рейтинг: 7/3,5.04(652), управление:
// СПОСОБ 1 >> // mcppfaq_isManaged.cpp // compile with: /clr // arguments: mcppfaq_delnative.dll #using <mscorlib.dll> using namespace System; using namespace System::IO; static bool isManaged(String __gc* sFilename) { try { Byte Data __gc[] = new Byte __gc[4096]; FileInfo __gc* file = new FileInfo(sFilename); Stream __gc* fin = file -> Open(FileMode::Open, FileAccess::Read); Int32 iRead = fin -> Read(Data, 0, 4096); fin -> Close(); // Verify this is a executable/dll if ((Data[1] << 8 | Data[0]) != 0x5a4d) return false; // This will get the address for the WinNT header Int32 iWinNTHdr = Data[63]<<24 | Data[62]<<16 | Data[61] << 8 | Data[60]; // Verify this is an NT address if ((Data[iWinNTHdr+3] << 24 | Data[iWinNTHdr+2] << 16 | Data[iWinNTHdr+1] << 8 | Data[iWinNTHdr]) != 0x00004550) return false; Int32 iLightningAddr = iWinNTHdr + 24 + 208; Int32 iSum=0; Int32 iTop = iLightningAddr + 8; for (int i = iLightningAddr; i < iTop; i++) iSum|=Data[i]; if (iSum == 0) return false; else return true; } catch(Exception __gc* e) { throw (e); } } int main(int argc, char * argv[]) { System::Console::WriteLine(isManaged(argv[1])); } // СПОСОБ 2 >> static bool isManaged( String^ sFilename ) { try { array<Byte>^ Data = gcnew array<Byte>(4096); FileInfo^ file = gcnew FileInfo(sFilename); if( !file->Exists ) { return false; } Stream^ fin = file->Open( FileMode::Open, FileAccess::Read ); __int32 iRead = fin->Read(Data, 0, 4096); fin->Close(); // Verify this is a executable/dll if( (Data[1] << 8 | Data[0]) != 0x5a4d ) { return false; } // This will get the address for the WinNT header __int32 iWinNTHdr = Data[63]<<24 | Data[62]<<16 | Data[61] << 8 | Data[60]; // Verify this is an NT address if ((Data[iWinNTHdr+3] << 24 | Data[iWinNTHdr+2] << 16 | Data[iWinNTHdr+1] << 8 | Data[iWinNTHdr]) != 0x00004550) { return false; } __int32 iLightningAddr = iWinNTHdr + 24 + 208; __int32 iSum=0; __int32 iTop = iLightningAddr + 8; for (int i = iLightningAddr; i < iTop; i++) { iSum|=Data[i]; } return (iSum == 0 ? false : true); }catch(Exception^ e) { throw (e); } return false; }
источник: http://rsdn.ru/Forum/Message.aspx?mid=508204#508204
5) Дизассемблер длин инструкций (x86), code #579[автор:-]



