Package manager

Description

Ce code a été mis au point par Heliopraetor pour le compte de NaHelWorks (je poste le code sur sa demande).

Il nous sert d'archiveur. L'avantage de ce code est qu'il est "maison". Il n'y a aucune compression, et ce n'est pas le but. Le but recherché est juste de regrouper plusieurs fichiers dans un seul. Ce qui est fait à la perfection par mon ami.

Il utilise la classe CResourceManager, faite maison aussi (et complétement retravaillée depuis WaterBall).

Cette source est utile pour maîtriser la programmation windows win32 par l'api, avec la gestion des listbox, et également l'accès binaire à des fichiers par les opérateurs classiques...

Source / Exemple :


//main.cpp
/*

		Package Manager 1.0		Par		NaHelWorks (Heliopraetor)

		Ouvre les fichiers package TPP, format d'archive spécialement développé pour
		les besoins de l'équipe.

		www.nahelworks.free.fr

  • /
#include <windows.h> #include <assert.h> #include <sys/stat.h> #include <stdio.h> #include <shlobj.h> #include "resource.h" #include "CResourceManager.h" bool FileExist(const char* _File) { struct stat Info; return !stat(_File, &Info ); } /********************************************************************************
  • *
  • *
  • Variables globales. *
  • *
  • *
                                                                                                                                                                • /
HWND g_hDlg; HINSTANCE g_hInstance; CResourceManager g_RM; char g_PackageName[MAX_PATH]; HWND g_hList; /********************************************************************************
  • *
  • *
  • Fonction d'actualisation de l'application. *
  • *
  • *
                                                                                                                                                                • /
void PgmUpdate() { //List g_RM._UpdateWndList(g_hList,g_PackageName); //Nombre de fichiers char l_sFile[256]; sprintf(l_sFile,"%d",SendMessage(g_hList,LB_GETCOUNT,NULL,NULL)); SetWindowText(GetDlgItem(g_hDlg,IDC_NBEDIT),l_sFile); //Taille du package float size=g_RM._Size(g_PackageName); sprintf(l_sFile,"%.2f ko",size/1024.0f); SetWindowText(GetDlgItem(g_hDlg,IDC_SIZEEDIT),l_sFile); } /********************************************************************************
  • *
  • *
  • Fonction de fermeture du package. *
  • *
  • *
                                                                                                                                                                • /
void ClosePackage() { g_PackageName[0]='\0'; g_RM._EmptyList(g_hList); SetWindowText(g_hDlg,"Package Manager"); SetWindowText(GetDlgItem(g_hDlg,IDC_NBEDIT),NULL); SetWindowText(GetDlgItem(g_hDlg,IDC_SIZEEDIT),NULL); } /********************************************************************************
  • *
  • *
  • Fonction de chargement d'un package par boîte windows. *
  • *
  • *
                                                                                                                                                                • /
void OpenPackage() { OPENFILENAME ofn; char l_sFile[MAX_PATH]; ZeroMemory(l_sFile,MAX_PATH); ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = g_hDlg; ofn.lpstrFilter = "Fichiers TPP (*.tpp)\0*.tpp\0\0"; ofn.lpstrFile = l_sFile; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "tpp"; ofn.Flags = OFN_EXPLORER|OFN_PATHMUSTEXIST; ofn.lpstrTitle = "Sélection d'un fichier package"; if(!GetOpenFileName(&ofn)) return; if( l_sFile == NULL ) return; strcpy(g_PackageName,l_sFile); sprintf(l_sFile,"Package Manager - %s",g_PackageName); SetWindowText(g_hDlg,l_sFile); PgmUpdate(); } /********************************************************************************
  • *
  • *
  • Fonction de création d'un package par boîte windows. *
  • *
  • *
                                                                                                                                                                • /
void CreatePackage() { OPENFILENAME ofn; char l_sFile[MAX_PATH]; ZeroMemory(l_sFile,MAX_PATH); ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = g_hDlg; ofn.lpstrFilter = "Fichier TPP (*.tpp)\0*.tpp\0\0"; ofn.lpstrFile = l_sFile; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "tpp"; ofn.Flags = OFN_EXPLORER|OFN_PATHMUSTEXIST; ofn.lpstrTitle = "Création d'un fichier package"; if(!GetSaveFileName(&ofn)) return; if( l_sFile == NULL ) return; strcpy(g_PackageName,l_sFile); sprintf(l_sFile,"Package Manager - %s",g_PackageName); SetWindowText(g_hDlg,l_sFile); g_RM._OpenPackage(g_PackageName,true); PgmUpdate(); } /********************************************************************************
  • *
  • *
  • Fonction d'ajout d'un fichier par boîte windows. *
  • *
  • *
                                                                                                                                                                • /
void AddFiles() { //Création d'une boîte d'ouverture de fichiers OPENFILENAME ofn; char l_sFile[MAX_PATH]; ZeroMemory(l_sFile,MAX_PATH); ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = g_hDlg; ofn.lpstrFilter = "Tout fichier (*.*)\0*.*\0\0"; ofn.lpstrFile = l_sFile; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = ""; ofn.Flags = OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_ALLOWMULTISELECT; ofn.lpstrTitle = "Sélection de fichiers à ajouter au package"; //Ouverture de la boîte if(!GetOpenFileName(&ofn)) return; if( l_sFile == NULL ) return; //Traitement selon le(s) fichier(s) sélectionné(s) char dir[MAX_PATH]; if ( ofn.nFileOffset < strlen(l_sFile) ) //si un seul { char* pos=NULL; for (int offset=0 ; *(l_sFile+offset)!='\0' ; offset++) { if (*(l_sFile+offset)=='\\') pos=(l_sFile+offset+1); } g_RM._AddFileToPackage(g_PackageName,l_sFile,pos); } else //si plusieurs { sprintf(dir,"%s\\",l_sFile); char cpltfile[MAX_PATH]; while ( l_sFile[ofn.nFileOffset] != '\0' ) { sprintf(cpltfile,"%s%s",dir,l_sFile+ofn.nFileOffset); g_RM._AddFileToPackage(g_PackageName,cpltfile,l_sFile+ofn.nFileOffset); ofn.nFileOffset += (strlen(l_sFile+ofn.nFileOffset) + 1); } } PgmUpdate(); } /********************************************************************************
  • *
  • *
  • Fonction de suppression de fichiers dans l'archive. *
  • *
  • *
                                                                                                                                                                • /
void DeleteFiles() { //Obtention du nombre de fichiers sélectionnés int nb = SendMessage(g_hList,LB_GETSELCOUNT,NULL,NULL); if (!nb) return; //Récupération des indexs de fichiers récupérés int* buffer = new int[nb]; SendMessage(g_hList,LB_GETSELITEMS,nb,(LPARAM)buffer); //Suppression des fichiers sélectionnés for (int i=nb-1 ; i>-1 ; i--) { SendMessage(g_hList,LB_DELETESTRING,buffer[i],NULL); g_RM._DeleteFile(buffer[i],g_PackageName); } //Actualisation et fin PgmUpdate(); delete [] buffer; } /********************************************************************************
  • *
  • *
  • Fonction d'extraction de fichiers de l'archive. *
  • *
  • *
                                                                                                                                                                • /
void ExtractFiles(bool all) { char Dir[MAX_PATH]; ZeroMemory(Dir,MAX_PATH); BROWSEINFO bi; ZeroMemory(&bi, sizeof(bi)); bi.hwndOwner = g_hDlg; bi.pidlRoot = NULL; bi.pszDisplayName = Dir; bi.lpszTitle = "Sélection du dossier où extraire (remplacement automatique)"; bi.ulFlags = BIF_DONTGOBELOWDOMAIN|BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = NULL; int iImage = NULL; LPITEMIDLIST pidl = SHBrowseForFolder(&bi); if (!pidl) return; if (Dir[0]=='\0') return; SHGetPathFromIDList(pidl,Dir); if (all) { g_RM._ExtractPackage(g_PackageName,Dir); } else { //Obtention du nombre de fichiers sélectionnés int nb = SendMessage(g_hList,LB_GETSELCOUNT,NULL,NULL); if (!nb) return; //Récupération des indexs de fichiers récupérés int* buffer = new int[nb]; SendMessage(g_hList,LB_GETSELITEMS,nb,(LPARAM)buffer); //Extraction des fichiers sélectionnés for (int i=nb-1 ; i>-1 ; i--) { g_RM._ExtractFile(buffer[i],g_PackageName,Dir); } delete [] buffer; } } /********************************************************************************
  • *
  • *
  • Fonction de centrage d'une fenêtre et de sa mise au premier-plan. *
  • *
  • *
                                                                                                                                                                • /
bool CenterDlg(HWND hWnd) { RECT l_Dim; GetWindowRect(hWnd,&l_Dim); //Center the window SetWindowPos(hWnd,HWND_TOPMOST,(GetSystemMetrics(SM_CXSCREEN)-(l_Dim.right - l_Dim.left))/2, (GetSystemMetrics(SM_CYSCREEN)-(l_Dim.bottom - l_Dim.top))/2, l_Dim.right - l_Dim.left, l_Dim.bottom - l_Dim.top, SWP_SHOWWINDOW); //Center the window SetWindowPos(hWnd,HWND_NOTOPMOST,(GetSystemMetrics(SM_CXSCREEN)-(l_Dim.right - l_Dim.left))/2, (GetSystemMetrics(SM_CYSCREEN)-(l_Dim.bottom - l_Dim.top))/2, l_Dim.right - l_Dim.left, l_Dim.bottom - l_Dim.top, SWP_SHOWWINDOW); return true; } /********************************************************************************
  • *
  • *
  • Fonction de traitement des messages de la fenêtre principale. *
  • *
  • *
                                                                                                                                                                • /
LRESULT CALLBACK DlgProc(HWND DlgProc, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CLOSE: case WM_QUIT: case WM_DESTROY: PostQuitMessage(0); break; case WM_INITDIALOG: { ShowWindow(g_hDlg, SW_SHOW); SetForegroundWindow(g_hDlg); CenterDlg(DlgProc); break; } case WM_COMMAND: { switch( HIWORD(wParam) ) { case BN_CLICKED: { bool extractall=false; switch( LOWORD(wParam) ) { case IDCLOSE: PostQuitMessage(0); break; case ID_OPENARCH: OpenPackage(); break; case ID_CLOSEARCH: if (*g_PackageName) ClosePackage(); break; case ID_CREATEARCH: if (*g_PackageName) ClosePackage(); OpenPackage(); break; case ID_ADD: if (*g_PackageName) AddFiles(); break; case ID_DELETE: if (*g_PackageName) DeleteFiles(); break; case ID_EXTRACTARCH: extractall=true; case ID_EXTRACT: if (*g_PackageName) ExtractFiles(extractall); break; } } } } } return false; } /********************************************************************************
  • *
  • *
  • Fonction WinMain. *
  • *
  • *
                                                                                                                                                                • /
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { //Lancement du SplashScreen g_hInstance=hInstance; //Création de la fenêtre g_hDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAIN), HWND_TOP, (DLGPROC) DlgProc); if(g_hDlg == NULL) { MessageBox(NULL, "Création de la fenêtre échouée.", "Erreur", MB_ICONEXCLAMATION|MB_OK); return 0; } g_hList=GetDlgItem(g_hDlg,IDC_LIST); //Boucle de message MSG Msg; while( GetMessage(&Msg,NULL,0,0) > 0 ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } //Fin du programme DestroyWindow(g_hDlg); return Msg.wParam; }

Conclusion :


Ne figure ici que main.cpp : tout le reste est dans le zip.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.