From dd984538bfffc1e81ce19f6f4cd73e524e13fe53 Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Sat, 4 Oct 2008 21:56:02 +0000 Subject: [PATCH] Removed some old DOS code, unified (for gcc) the handling of file paths. Probably fixed some linux issues there. Fixed the filename search by typing in Save/load. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@196 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- Makefile.dep | 6 +++--- boutons.c | 51 ++++++++++++++++++++++----------------------------- divers.c | 39 --------------------------------------- divers.h | 46 ---------------------------------------------- init.c | 38 +++++++++++++++++--------------------- io.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- io.h | 11 +++++++++++ linux.c | 16 ---------------- linux.h | 3 --- main.c | 40 +++++++++++----------------------------- 10 files changed, 108 insertions(+), 187 deletions(-) diff --git a/Makefile.dep b/Makefile.dep index 81925932..c9b0f211 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -1,6 +1,6 @@ main.o: main.c const.h struct.h global.h loadsave.h graph.h divers.h \ init.h boutons.h moteur.h pages.h files.h sdlscreen.h erreurs.h \ - readini.h saveini.h linux.h + readini.h saveini.h linux.h io.h init.o: init.c const.h struct.h global.h loadsave.h graph.h boutons.h \ palette.h aide.h operatio.h divers.h erreurs.h clavier.h io.h graph.o: graph.c sdlscreen.h struct.h const.h graph.h divers.h moteur.h \ @@ -13,7 +13,7 @@ special.o: special.c const.h struct.h global.h loadsave.h graph.h \ moteur.h boutons.o: boutons.c const.h struct.h global.h loadsave.h divers.h \ graph.h moteur.h readline.h files.h init.h boutons.h operatio.h pages.h \ - erreurs.h readini.h saveini.h shade.h + erreurs.h readini.h saveini.h shade.h io.h palette.o: palette.c const.h struct.h global.h loadsave.h divers.h \ graph.h moteur.h readline.h boutons.h pages.h aide.o: aide.c const.h struct.h global.h loadsave.h divers.h graph.h \ @@ -39,4 +39,4 @@ saveini.o: saveini.c const.h global.h struct.h loadsave.h readini.h \ shade.o: shade.c global.h struct.h const.h loadsave.h graph.h moteur.h \ divers.h readline.h clavier.o: clavier.c global.h struct.h const.h loadsave.h -io.o: io.c struct.h const.h +io.o: io.c struct.h const.h io.h diff --git a/boutons.c b/boutons.c index ff53aaa9..f243b12f 100644 --- a/boutons.c +++ b/boutons.c @@ -23,6 +23,7 @@ #include "readini.h" #include "saveini.h" #include "shade.h" +#include "io.h" #ifdef __WATCOMC__ #include @@ -2103,7 +2104,7 @@ void Print_repertoire_courant(void) // Ensuite, on cherche un endroit à partir duquel on pourrait loger tout // le reste de la chaine (Ouaaaaaah!!! Vachement fort le mec!!) for (Indice++;IndiceType+1)) ) { - // On copie le nom de la liste en cours de traitement dans Nom_courant - // tout en le remettant sous forme normale. - for (Pointeur1=Element_courant->NomComplet,Pointeur2=Nom_courant;*Pointeur1;Pointeur1++) - if (*Pointeur1!=' ') - *(Pointeur2++)=*Pointeur1; - *Pointeur2=0; // On compare et si c'est mieux, on stocke dans Meilleur_nom - for (Compteur=0; tolower(Nom_courant[Compteur])==tolower(Nom[Compteur]); Compteur++); + for (Compteur=0; tolower(Element_courant->NomComplet[Compteur])==tolower(Nom[Compteur]); Compteur++); if (Compteur>Lettres_identiques) { Lettres_identiques=Compteur; - strcpy(FFF_Meilleur_nom,Nom_courant); + strcpy(FFF_Meilleur_nom,Element_courant->NomComplet); Pointeur_Meilleur_nom=Element_courant->NomComplet; } } @@ -2390,16 +2382,20 @@ byte Bouton_Load_ou_Save(byte Load, byte Image) } // On prend bien soin de passer dans le répertoire courant (le bon qui faut! Oui madame!) - chdir(Principal_Repertoire_courant); - /*_dos_setdrive(Principal_Repertoire_courant[0]-64,&Bidon);*/ - Determiner_repertoire_courant(); - + if (Load) + { + chdir(Principal_Repertoire_courant); + Determiner_repertoire_courant(); + } + else + { + chdir(Principal_Repertoire_fichier); + Determiner_repertoire_courant(); + } + // Affichage des premiers fichiers visibles: Relire_liste_fichiers(Principal_Format,Principal_File_list_Position,Principal_File_list_Decalage,Scroller_de_fichiers); - // Je n'efface pas cette partie parce que l'idée n'était pas mauvaise mais - // ça chie un maximum alors autant ne pas trop compliquer les choses... - /* if (!Load) { // On initialise le nom de fichier à celui en cours et non pas celui sous @@ -2408,7 +2404,6 @@ byte Bouton_Load_ou_Save(byte Load, byte Image) // On affiche le nouveau nom de fichier Print_Nom_fichier_dans_selecteur(); } - */ Pixel_de_chargement=Pixel_Chargement_dans_preview; Nouvelle_preview=1; @@ -2679,11 +2674,10 @@ byte Bouton_Load_ou_Save(byte Load, byte Image) Temp=strlen(Fichier_recherche); if (Touche_ANSI>= ' ' && Touche_ANSI < 255 && Temp<50) { - Fichier_recherche[Temp]=toupper(Touche_ANSI); - Fichier_recherche[Temp+1]=0; + Fichier_recherche[Temp]=Touche_ANSI; + Fichier_recherche[Temp+1]='\0'; Fichier_le_plus_ressemblant=Nom_correspondant_le_mieux_a(Fichier_recherche); - if ( (Fichier_le_plus_ressemblant) - && (!memcmp(Fichier_recherche,FFF_Meilleur_nom,Temp+1)) ) + if ( (Fichier_le_plus_ressemblant) ) { Temp=Principal_File_list_Position+Principal_File_list_Decalage; Effacer_curseur(); @@ -2694,7 +2688,7 @@ byte Bouton_Load_ou_Save(byte Load, byte Image) Nouvelle_preview=1; } else - Fichier_recherche[Temp]=0; + *Fichier_recherche=0; } } else @@ -2715,10 +2709,9 @@ byte Bouton_Load_ou_Save(byte Load, byte Image) strcpy(Repertoire_precedent,Nom_formate("..")); else { - for (Temp=strlen(Principal_Repertoire_courant); - (Temp>0) && (Principal_Repertoire_courant[Temp-1]!='\\'); - Temp--); - strcpy(Repertoire_precedent,Nom_formate(Principal_Repertoire_courant+Temp)); + strcpy(Repertoire_precedent, + Nom_formate(Position_dernier_slash(Principal_Repertoire_courant)) + ); } // On doit rentrer dans le répertoire: diff --git a/divers.c b/divers.c index efe466c5..0e77c665 100644 --- a/divers.c +++ b/divers.c @@ -578,45 +578,6 @@ byte Lit_pixel_dans_ecran_backup (word X,word Y) return *(Ecran_backup + X + Principal_Largeur_image * Y); } -byte Type_de_lecteur_de_disquette(byte Numero_de_lecteur) -// Numero_de_lecteur compris entre 0 et 3 (4 lecteurs de disquettes) -// -// Résultat = 0 : Pas de lecteur -// 1 : Lecteur 360 Ko -// 2 : Lecteur 1.2 Mo -// 3 : Lecteur 720 Ko -// 4 : Lecteur 1.4 Mo -// 5 : Lecteur 2.8 Mo (??? pas sur ???) -// 6 : Lecteur 2.8 Mo -{ - UNIMPLEMENTED - return 0; -} - -byte Disk_map(byte Numero_de_lecteur) -{ - UNIMPLEMENTED - return 0; -} - -byte Disque_dur_present(byte Numero_de_disque) -{ - UNIMPLEMENTED - return 0; -} - -byte Lecteur_CDROM_present(byte Numero_de_lecteur) -{ - UNIMPLEMENTED - return 0; -} - -long Freespace(byte Numero_de_lecteur) -{ - UNIMPLEMENTED - return 0; -} - // Les images ILBM sont stockés en bitplanes donc on doit trifouiller les bits pour // en faire du chunky diff --git a/divers.h b/divers.h index 8bffd21a..c0582c93 100644 --- a/divers.h +++ b/divers.h @@ -35,52 +35,6 @@ byte Lit_pixel_dans_ecran_backup (word X,word Y); byte Lit_pixel_dans_ecran_feedback (word X,word Y); byte Lit_pixel_dans_brosse (word X,word Y); -long Freespace(byte Numero_de_lecteur); -// Numero_de_lecteur = 0 : Disque courant -// 1 : lecteur A: -// 2 : lecteur B: -// 3 : lecteur C: -// ... : ... -// -// Résultat = -1 si lecteur invalide, taille disponible sinon - -byte Type_de_lecteur_de_disquette(byte Numero_de_lecteur); -// Numero_de_lecteur compris entre 0 et 3 (4 lecteurs de disquettes) -// -// Résultat = 0 : Pas de lecteur -// 1 : Lecteur 360 Ko -// 2 : Lecteur 1.2 Mo -// 3 : Lecteur 720 Ko -// 4 : Lecteur 1.4 Mo -// 5 : Lecteur 2.8 Mo (??? pas sur ???) -// 6 : Lecteur 2.8 Mo - -byte Disk_map(byte Numero_de_lecteur); -// Renseigne sur la lettre logique d'un lecteur -// (utile pour tester si un lecteur de disquette est A: ou B: aux yeux du DOS) -// -// Entrée: Octet = n° du lecteur (1=A, 2=B ...) -// -// Sortie: Octet = 0FFh : Pas de lecteur (???) -// sinon: numéro représenatan la lettre logique du lecteur -// (commence à 1) - -byte Disque_dur_present(byte Numero_de_disque); -// Numero_de_disque = 0:C, 1:D, 2:E, ... -// -// Résultat = 0 : Pas de disque dur présent -// 1 : Disque dur présent - -byte Lecteur_CDROM_present(byte Numero_de_lecteur); -// Numero_de_lecteur = 0 : lecteur A: -// 1 : lecteur B: -// 2 : lecteur C: -// ... : ... -// -// Résultat = 0 : Pas de lecteur CD-ROM présent -// 1 : Lecteur CD-ROM présent - - void Ellipse_Calculer_limites(short Rayon_horizontal,short Rayon_vertical); // Calcule les valeurs suivantes en fonction des deux paramètres: // diff --git a/init.c b/init.c index a0cb8398..209bc0a8 100644 --- a/init.c +++ b/init.c @@ -31,13 +31,7 @@ // Chercher le répertoire contenant GFX2.EXE void Chercher_repertoire_du_programme(char * Chaine) { - #ifdef __WATCOMC__ - GetCurrentDirectory(255,Repertoire_du_programme); - strcat(Repertoire_du_programme,"\\"); - #else - puts("Chercher_repertoire_du_programme: implémentation incomplète"); - Repertoire_du_programme[0]=0; //On va travailler dans le dossier courant ... - #endif + Extraire_chemin(Repertoire_du_programme, Chaine); } @@ -202,21 +196,23 @@ void Charger_DAT(void) strcpy(Nom_du_fichier,Repertoire_du_programme); strcat(Nom_du_fichier,"gfx2.dat"); - + if(stat(Nom_du_fichier,&Informations_Fichier)) - switch errno - { - case EACCES: puts("La permission de parcours est refusée pour un des répertoires contenu dans le chemin path."); break; - case EBADF: puts("filedes est un mauvais descripteur."); break; - case EFAULT: puts("Un pointeur se trouve en dehors de l'espace d'adressage."); break; - case ENAMETOOLONG: puts("Nom de fichier trop long."); break; - case ENOENT: puts("Un composant du chemin path n'existe pas, ou il s'agit d'une chaîne vide."); break; - case ENOMEM: puts("Pas assez de mémoire pour le noyau."); break; - case ENOTDIR: puts("Un composant du chemin d'accès n'est pas un répertoire."); break; - #ifdef __linux__ - case ELOOP: puts("Trop de liens symboliques rencontrés dans le chemin d'accès."); break; - #endif - } + { + switch errno + { + case EACCES: puts("La permission de parcours est refusée pour un des répertoires contenu dans le chemin path."); break; + case EBADF: puts("filedes est un mauvais descripteur."); break; + case EFAULT: puts("Un pointeur se trouve en dehors de l'espace d'adressage."); break; + case ENAMETOOLONG: puts("Nom de fichier trop long."); break; + case ENOENT: puts("Un composant du chemin path n'existe pas, ou il s'agit d'une chaîne vide."); break; + case ENOMEM: puts("Pas assez de mémoire pour le noyau."); break; + case ENOTDIR: puts("Un composant du chemin d'accès n'est pas un répertoire."); break; + #ifdef __linux__ + case ELOOP: puts("Trop de liens symboliques rencontrés dans le chemin d'accès."); break; + #endif + } + } Taille_fichier=Informations_Fichier.st_size; if (Taille_fichier +#include #include "struct.h" +#include "io.h" word endian_magic16(word x) { @@ -13,7 +15,7 @@ word endian_magic16(word x) return SDL_Swap16(x); #endif } -word endian_magic32(word x) +dword endian_magic32(dword x) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN return x; @@ -128,3 +130,44 @@ int write_dword_be(FILE *Fichier, dword Mot) #endif return fwrite(&Mot, 1, sizeof(dword), Fichier) == sizeof(dword); } + +// Détermine la position du dernier '/' ou '\\' dans une chaine, +// typiquement pour séparer le nom de fichier d'un chemin. +// Attention, sous Windows, il faut s'attendre aux deux car +// par exemple un programme lancé sous GDB aura comme argv[0]: +// d:\Data\C\GFX2\grafx2/grafx2.exe +char * Position_dernier_slash(const char * Chaine) +{ + const char * Position = NULL; + for (; *Chaine != '\0'; Chaine++) + if (*Chaine == SEPARATEUR_CHEMIN[0] +#ifdef __WIN32__ + || *Chaine == '/' +#endif + ) + Position = Chaine; + return (char *)Position; +} +// Récupère la partie "nom de fichier seul" d'un chemin +void Extraire_nom_fichier(char *Destination, const char *Source) +{ + const char * Position = Position_dernier_slash(Source); + + if (Position) + strcpy(Destination,Position+1); + else + strcpy(Destination,Source); +} +// Récupère la partie "répertoire+/" d'un chemin. +void Extraire_chemin(char *Destination, const char *Source) +{ + char * Position; + + strcpy(Destination,Source); + Position = Position_dernier_slash(Destination); + if (Position) + *(Position+1) = '\0'; + else + strcat(Destination, SEPARATEUR_CHEMIN); +} + diff --git a/io.h b/io.h index f2cb61d8..74c90e6f 100644 --- a/io.h +++ b/io.h @@ -17,3 +17,14 @@ int read_word_be(FILE *Fichier, word *Dest); int write_word_be(FILE *Fichier, word Mot); int read_dword_be(FILE *Fichier, dword *Dest); int write_dword_be(FILE *Fichier, dword Mot); + +void Extraire_nom_fichier(char *Destination, const char *Source); +void Extraire_chemin(char *Destination, const char *Source); + +char * Position_dernier_slash(const char * Chaine); + +#ifdef __linux__ + #define SEPARATEUR_CHEMIN "/" +#else + #define SEPARATEUR_CHEMIN "\\" +#endif diff --git a/linux.c b/linux.c index 3cf0284f..e4a37e35 100644 --- a/linux.c +++ b/linux.c @@ -5,22 +5,6 @@ #include #include -void _splitpath(char* Buffer, char* Chemin, char* Nom_Fichier) -{ - int i=0, Position_Du_Dernier_Slash=0; - do - { - Chemin[i]=Buffer[i]; - if (Buffer[i]=='/') - Position_Du_Dernier_Slash=i; - i++; - }while (Buffer[i]!=0); - - i=Position_Du_Dernier_Slash+1; - Chemin[i]=0; - strcpy(Nom_Fichier,Buffer+i); -} - int filelength(int fichier) { struct stat infos_fichier; diff --git a/linux.h b/linux.h index b1eb77fc..b4dc1994 100644 --- a/linux.h +++ b/linux.h @@ -3,9 +3,6 @@ #ifdef __linux__ -void _splitpath(char* Buffer, char* Chemin, char* Nom_Fichier); -/* Sépare dans la chaîne passée dans Buffer le chemin d'accès du nom de fichier */ - struct find_t { unsigned char attrib; char name[256]; diff --git a/main.c b/main.c index 372bfb99..db81cc19 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ #include "readini.h" #include "saveini.h" #include "linux.h" +#include "io.h" #ifndef __linux__ #include @@ -215,34 +216,17 @@ void Analyse_de_la_ligne_de_commande(int argc,char * argv[]) // On récupère le chemin complet du paramètre // Et on découpe ce chemin en répertoire(path) + fichier(.ext) #ifdef __linux__ - Buffer=realpath(argv[1],NULL); - _splitpath(Buffer,Principal_Repertoire_fichier,Principal_Nom_fichier); + Buffer=realpath(argv[1],NULL); #else - Buffer = malloc(TAILLE_CHEMIN_FICHIER); - _fullpath(Buffer,argv[1],TAILLE_CHEMIN_FICHIER); - { - // Découpage du nom canonique de fichier en chemin + nom. - // On croirait que c'est du classique pour la LibC, mais non... - int i; - int PosDernierSeparateur = 0; - for (i=0; Buffer[i]!='\0'; i++) - { - Principal_Repertoire_fichier[i] = Buffer[i]; - if (Buffer[i]=='\\') - PosDernierSeparateur = i; - } - Principal_Repertoire_fichier[PosDernierSeparateur]='\0'; - for (i=0; Buffer[PosDernierSeparateur + i + 1]!='\0'; i++) - { - Principal_Nom_fichier[i] = Buffer[PosDernierSeparateur + i + 1]; - } - Principal_Nom_fichier[i]='\0'; - - } - free(Buffer); + Buffer = malloc(TAILLE_CHEMIN_FICHIER); + _fullpath(Buffer,argv[1],TAILLE_CHEMIN_FICHIER); #endif - - // chdir(Principal_Repertoire_fichier); + Extraire_chemin(Principal_Repertoire_fichier, Buffer); + Extraire_nom_fichier(Principal_Nom_fichier, Buffer); + #ifndef __linux__ + free(Buffer); + #endif + chdir(Principal_Repertoire_fichier); } else { @@ -309,6 +293,7 @@ void Initialisation_du_programme(int argc,char * argv[]) // On détermine dès le départ où se trouve le fichier: Chercher_repertoire_du_programme(argv[0]); + // On détecte les lecteurs qui sont accessibles: Rechercher_drives(); // On détermine le répertoire courant: @@ -630,9 +615,6 @@ void Fermeture_du_programme(void) // On prend bien soin de passer dans le répertoire initial: if (chdir(Repertoire_initial)!=-1) { - /* A revoir ... - _dos_setdrive(Repertoire_initial[0]-64,&Bidon); - */ // On sauvegarde les données dans le .CFG et dans le .INI if (Config.Auto_save) {