home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************
- * Fonctions de gestion de fichiers de paramètres (.inf) *
- * *
- * Fichier source des fonctions. *
- * *
- * (C) 1996 - Philippe Castella *
- * *
- *******************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
- #include "InitFile.h"
-
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*
- * Déclaration des fonctions priéves au module. *
- *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
- static int ReadLine (FILE *ficIni, char *ligne);
- static void ReadFile (FILE *fileHandle, InitFile **strIni);
- static InfSection *AddSection (InitFile **strIni, char *section);
- static void AddVar (InitFile **strIni, char *section, char *var, char *val);
- static void SaveFile (InitFile *strIni);
- static void GetValeur (InitFile *strIni, char *section, char *var, char *buf);
-
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*
- * Définition des fonctions de la librairie. *
- *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
-
- /*-----------------------------------------------------------------*
- * InfCreate : fonction de création/ouverture d'un fichier .inf *
- * *
- * Entrée : nom du fichier .inf *
- * Sortie : un pointeur sur la nouvelle structure créée *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- InitFile *InfCreate (char *nomFic)
- {
- InitFile *strIni;
- FILE *fileHandle;
-
- /* Allocation de la mémoire nécessaire à la structure */
- strIni = (InitFile *)malloc (sizeof (InitFile));
- if (strIni)
- {
- /* Ouverture du fichier */
- fileHandle = fopen (nomFic, "r");
-
- /* Mise jour de la structure */
- strIni->FileName = (char *)malloc (strlen(nomFic) + 1);
- if (! strIni->FileName)
- { /* Si Pb, on libère la mémoire et on s'en va */
- free(strIni);
- return (InitFile *)0;
- }
- strcpy (strIni->FileName, nomFic);
- strIni->FirstSection = (InfSection *)0;
-
- /* Si fichier existe, on lit son contenu */
- if (fileHandle)
- { /* Lecture ligne à ligne du fichier = remplir la structure */
- ReadFile (fileHandle, &strIni);
-
- /* Fermeture du fichier */
- fclose (fileHandle);
- }
- }
- else
- strIni = (InitFile *)0;
-
- return strIni;
- }
-
- /*-----------------------------------------------------------------*
- * InfWriteBool : Ajout d'une variable de type booléen. *
- * InfWriteInteger : Ajout d'une variable de type entier. *
- * InfWriteString : Ajout d'une variable de type chaîne de car. *
- * *
- * Entrée : nom de la section *
- * nom de la variable *
- * valeur de la variable *
- * I/O : L'adresse du pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- void InfWriteBool (InitFile **strIni, char *section, char *var, short val)
- {
- if (val)
- AddVar (strIni, section, var, "1");
- else
- AddVar (strIni, section, var, "0");
- }
-
- void InfWriteInteger (InitFile **strIni, char *section, char *var, long val)
- {
- char buf[10];
-
- ltoa (val, buf, 10);
- AddVar (strIni, section, var, buf);
- }
-
- void InfWriteString (InitFile **strIni, char *section, char *var, char *val)
- {
- AddVar (strIni, section, var, val);
- }
-
- /*-----------------------------------------------------------------*
- * InfReadBool : Lecture d'une variable de type booléen. *
- * InfReadInteger : Lecture d'une variable de type entier. *
- * InfReadString : Lecture d'une variable de type chaîne de car. *
- * *
- * Entrée : Le pointeur sur la structure de données. *
- * nom de la section *
- * nom de la variable *
- * valeur par défaut de la variable si non trouvée *
- * I/O : valeur de la variable *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- void InfReadBool (InitFile *strIni, char *section, char *var, short def, short *val)
- {
- char buf[129];
-
- /* Récupération de la valeur de la variable */
- GetValeur (strIni, section, var, buf);
-
- /* Si rien trouvé -> retourner val par défaut, sinon la valeur */
- if (! *buf)
- *val = def;
- else
- *val = (short)atoi (buf);
- }
-
- void InfReadInteger (InitFile *strIni, char *section, char *var, long def, long *val)
- {
- char buf[129];
-
- /* Récupération de la valeur de la variable */
- GetValeur (strIni, section, var, buf);
-
- /* Si rien trouvé -> retourner val par défaut, sinon la valeur */
- if (! *buf)
- *val = def;
- else
- *val = atol (buf);
- }
-
- void InfReadString (InitFile *strIni, char *section, char *var, char *def, char *val)
- {
- char buf[129];
-
- /* Récupération de la valeur de la variable */
- GetValeur (strIni, section, var, buf);
-
- /* Si rien trouvé -> retourner val par défaut, sinon la valeur */
- if (! *buf)
- strcpy (val, def);
- else
- strcpy (val, buf);
- }
-
- /*-----------------------------------------------------------------*
- * InfFree : Fonction de sauvegarde du fichier .inf et de *
- * restitution de la mémoire allouée dynamiquement. *
- * *
- * Entrée : Le pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- void InfFree (InitFile *strIni)
- {
- /* Si on ne pointe pas sur du vide */
- if (strIni != (InitFile *)0)
- {
- /* Sauvegarde du fichier */
- SaveFile (strIni);
-
- /* Effacement de la structure */
- free (strIni->FileName);
- free (strIni);
- }
- }
-
- /*-----------------------------------------------------------------*
- * InfDeleteVar : Fonction de suppression d'une variable. *
- * *
- * Entrée : Le pointeur sur la structure de données. *
- * Le nom de la section *
- * Le nom de la variable *
- * *
- * Dernière modification : 25 décembre 1996 *
- *-----------------------------------------------------------------*/
- void InfDeleteVar (InitFile **strIni, char *section, char *var)
- {
- InfSection *strSection;
- InfVar *strVar, *oldVar;
-
- if (*strIni)
- {
- /* On recherche si la section existe bien */
- strSection = (*strIni)->FirstSection;
- while (strSection != (InfSection *)0)
- {
- if (strcmp(strSection->SectionName, section) == 0)
- {
- strVar = oldVar = strSection->FirstVar;
- while (strVar != (InfVar *)0)
- {
- if (strcmp (strVar->VarName, var) == 0)
- {
- oldVar->NextVar = strVar->NextVar;
- free (strVar->VarName);
- free (strVar->VarVal);
- free (strVar);
- return;
- }
- oldVar = strVar;
- strVar = strVar->NextVar;
- }
- }
- strSection = strSection->NextSection;
- }
- }
- }
-
- /*-----------------------------------------------------------------*
- * InfDeleteSection : Fonction de suppression d'une section. *
- * *
- * Entrée : Le nom de la section *
- * Le nom de la variable *
- * I/O : L'adresse du pointeur sur la structure de données. *
- * *
- * Dernière modification : 25 décembre 1996 *
- *-----------------------------------------------------------------*/
- void InfDeleteSection (InitFile **strIni, char *section)
- {
- InfSection *strSection, *oldSection;
-
- if (*strIni)
- {
- /* On recherche si la section existe bien */
- strSection = (*strIni)->FirstSection;
- oldSection = strSection;
- while (strSection != (InfSection *)0)
- {
- if (strcmp(strSection->SectionName, section) == 0)
- {
- if (oldSection == strSection)
- (*strIni)->FirstSection = strSection->NextSection;
- else
- oldSection->NextSection = strSection->NextSection;
- free (strSection->SectionName);
- free (strSection);
- return;
- }
- oldSection = strSection;
- strSection = strSection->NextSection;
- }
- }
- }
-
- /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*
- * Définition des fonctions privées de la librairie *
- *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
-
- /*-----------------------------------------------------------------*
- * ReadLine : Fonction de lecture d'une ligne dans un fichier ASCII*
- * *
- * Entrée : Le descripteur du fichier ouvert avec fopen *
- * I/O : un buffer pour stocker les données lues *
- * Sortie : Le nombre de caractères composant la ligne, -1 si EOF *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- static int ReadLine (FILE *ficIni, char *ligne)
- {
- register char car;
- register int cpt = 0;
-
- /* Lire car. par car. jusqu'à fin de ligne ou fin de fichier */
- while (! feof(ficIni) && ((car = fgetc(ficIni)) != '\n'))
- ligne[cpt++] = car;
- ligne[cpt] = '\0';
-
- /* Si fin de fichier, on le signale */
- if (feof(ficIni))
- cpt = -1;
-
- /* Renvoie le nombre de caractères lus */
- return cpt;
- }
-
- /*-----------------------------------------------------------------*
- * ReadFile : Fonction de lecture des données contenue dans le *
- * fichier .inf *
- * *
- * I/O : L'adresse du pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- * *
- * Remarque : une ligne ne doit pas dépasser 256 caractères c-à-d *
- * 128 car. pour la variable et autant pour la valeur. *
- *-----------------------------------------------------------------*/
- static void ReadFile (FILE *fileHandle, InitFile **strIni)
- {
- register int nbCar, i, j;
- char section[65];
- char read_buf[257];
- char var[129];
- char val[129];
-
- /* Lecture du fichier ligne par ligne */
- while ((nbCar = ReadLine (fileHandle, read_buf)) > -1)
- {
- if (nbCar > 0)
- { /* Pas une ligne vide */
- if (read_buf[0] == '[') /* Si début de Section */
- { /* [nom de la section] */
- i = 1;
- j = 0;
- /* Lire titre de la section */
- while (read_buf[i] != ']' && read_buf[i] != '\0')
- section[j++] = read_buf[i++];
- section[j] = '\0';
-
- /* Création de la section dans la structure de données */
- AddSection(strIni, section);
- }
- else
- { /* var=val */
- i = 0;
- j = 0;
- /* Lire nom de la variable */
- while (read_buf[i] != '=')
- var[j++] = read_buf[i++];
- var[j] = '\0';
- i++;
-
- /* Lire valeur de la variable */
- j = 0;
- while (read_buf[i] != '\0')
- val[j++] = read_buf[i++];
- val[j] = '\0';
-
- /* Création de la variable dans la structure de données*/
- AddVar(strIni, section, var, val);
- }
- }
- }
- }
-
- /*-----------------------------------------------------------------*
- * AddSection : Fonction permettant de créer une nouvelle section *
- * si celle-ci n'existe pas déjà. *
- * *
- * Entrée : le nom de la section *
- * I/O : L'adresse du pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- static InfSection *AddSection (InitFile **strIni, char *section)
- {
- InfSection *strSection = (*strIni)->FirstSection;
- InfSection *NewSection;
-
- /* On recherche si la section existe déjà */
- while (strSection != (InfSection *)0)
- {
- if (strcmp(strSection->SectionName, section) == 0)
- return strSection;
- strSection = strSection->NextSection;
- }
-
- /* Si elle n'existe pas, on la rajoute */
- NewSection = (InfSection *)malloc (sizeof(InfSection));
- if (NewSection)
- {
- /* Allocation mémoire pour le nom de la section */
- NewSection->SectionName = (char *)malloc (strlen(section) + 1);
- if (! NewSection->SectionName)
- {
- free (NewSection);
- return (InfSection *)0;
- }
-
- /* Mettre à jour le contenu de la structure */
- strcpy (NewSection->SectionName, section);
- NewSection->NextSection = (InfSection *)0;
- NewSection->FirstVar = (InfVar *)0;
-
- strSection = (*strIni)->FirstSection;
- /* Si première section ...*/
- if (strSection == (InfSection *)0)
- {
- (*strIni)->FirstSection = NewSection;
- return (*strIni)->FirstSection;
- }
- else /* ...sinon */
- {
- while (strSection->NextSection != (InfSection *)NULL)
- strSection = strSection->NextSection;
-
- strSection->NextSection = NewSection;
- return strSection->NextSection;
- }
- }
- return (InfSection *)0;
- }
-
- /*-----------------------------------------------------------------*
- * AddVar : Fonction permettant de créer une nouvelle variable dans*
- * une section données si elle n'existe pas déjà. *
- * *
- * Entrée : le nom de la section *
- * le nom de la variable *
- * la valeur de la variable *
- * I/O : L'adresse du pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- static void AddVar (InitFile **strIni, char *section, char *var, char *val)
- {
- InfSection *strSection;
- InfVar *strVar, *NewVar;
- short ok = FALSE;
-
- if (*strIni)
- {
- /* Recherche la bonne section (création si nécessaire) */
- strSection = AddSection (strIni, section);
- if (strSection == (InfSection *)0)
- return;
-
- /* On recherche si la variable existe déjà */
- strVar = strSection->FirstVar;
- while (strVar != (InfVar *)0)
- {
- /* Si on trouve la variable, on sort de la boucle */
- if (strcmp(strVar->VarName,var) == 0)
- {
- ok = TRUE;
- break;
- }
- strVar = strVar->NextVar;
- }
-
- /* Si variable non trouvée, on la crée */
- if (! ok)
- {
- NewVar = (InfVar *)malloc (sizeof(InfVar));
- if (NewVar)
- {
- NewVar->VarName = (char *)malloc(strlen(var) + 1);
- if (! NewVar->VarName)
- {
- free (NewVar);
- return;
- }
- strcpy (NewVar->VarName, var);
- NewVar->NextVar = (InfVar *)0;
-
- strVar = strSection->FirstVar;
- if (strVar == (InfVar *)0)
- {
- strSection->FirstVar = NewVar;
- strVar = strSection->FirstVar;
- }
- else
- {
- while (strVar->NextVar != (InfVar *)NULL)
- strVar = strVar->NextVar;
-
- strVar->NextVar = NewVar;
- strVar = strVar->NextVar;
- }
- }
- }
-
- /* Mise à jour de la valeur de la variable */
- strVar->VarVal = (char *)malloc (strlen(val) + 1);
- if (strVar->VarVal)
- strcpy (strVar->VarVal, val);
- }
- }
-
- /*-----------------------------------------------------------------*
- * SaveFile : Fonction permettant de sauvegarder le fichier .inf *
- * tout en effacant au fur er à mesure la structure. *
- * *
- * Entrée : Le pointeur sur la structure de données. *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- static void SaveFile (InitFile *strIni)
- {
- FILE *fileHandle;
- InfSection *strSection, *oldSection;
- InfVar *strVar, *oldVar;
-
- /* Ouvrir fichier paramètre */
- fileHandle = fopen (strIni->FileName, "w");
-
- strSection = strIni->FirstSection;
- while (strSection != (InfSection *)0)
- {
- /* Ecriture du nom de la section */
- fprintf (fileHandle, "[%s]\n", strSection->SectionName);
-
- /* Première variable */
- strVar = strSection->FirstVar;
- while (strVar != (InfVar *)0)
- {
- /* Ecriture de la variable et de sa valeur */
- fprintf (fileHandle, "%s=%s\n", strVar->VarName, strVar->VarVal);
-
- /* Variable suivante */
- oldVar = strVar;
- strVar = strVar->NextVar;
-
- /* Supprimer variable, valeur et structure */
- free (oldVar->VarName);
- free (oldVar->VarVal);
- free (oldVar);
- }
- fprintf (fileHandle, "\n");
- /* Section suivante */
- oldSection = strSection;
- strSection = strSection->NextSection;
-
- /* Supprimer nom et structure */
- free (oldSection->SectionName);
- free (oldSection);
- }
-
- /* Fermer fichier paramètre */
- fclose (fileHandle);
-
- }
-
- /*-----------------------------------------------------------------*
- * GetValeur : Fonction permettant de récupérer le contenu d'une *
- * variable donnée d'une section donnée. *
- * *
- * Entrée : Le pointeur sur la structure de données. *
- * le nom de la section *
- * le nom de la variable *
- * I/O : Le buffer qui va contenir la valeur *
- * *
- * Dernière modification : 11 décembre 1996 *
- *-----------------------------------------------------------------*/
- static void GetValeur (InitFile *strIni, char *section, char *var, char *buf)
- {
- InfSection *strSection;
- InfVar *strVar;
-
- /* Recherche de la section */
- strSection = strIni->FirstSection;
- while ((strSection != (InfSection *)0) && strcmp(strSection->SectionName, section) != 0)
- strSection = strSection->NextSection;
-
- /* Si section non trouvée, buffer vide */
- if (strSection == (InfSection *)0)
- strcpy (buf, "");
- else
- {
- /* Recherche de la variable */
- strVar = strSection->FirstVar;
- while ((strVar != (InfVar *)0) && strcmp(strVar->VarName, var) != 0)
- strVar = strVar->NextVar;
-
- /* Si variable non trouvée, buffer vide */
- if (strVar == (InfVar *)0)
- strcpy (buf, "");
- else /* sinon copier la valeur dans le buffer */
- strcpy (buf, strVar->VarVal);
- }
- }
-
- /*******************************************************************/