source: BAORadio/libindi/libindi/drivers/telescope/filetools.cpp @ 620

Last change on this file since 620 was 620, checked in by frichard, 13 years ago
File size: 30.7 KB
Line 
1/*filetools.c*/
2
3#include "filetools.h"
4
5#define MAX_LENGHT 255
6
7
8
9/******************************************************
10 * NOM:              readINI                          *
11 * SPECIFICATION:                                     *
12 *   retourne la valeur de la cle du                  *
13 *               fichier dont la structure est celle  *
14 *               d'un fichier .ini windows            *
15 * ARGUMENTS:                                         *
16 * >section: le nom de la section sans les crochets   *
17 * >key: nom de la cle de la section                  *
18 * >pStr: pointeur pour mettre le resultat            *
19 *       allocation memoire effectuee                 *
20 * ENVIRONNEMENT:                                     *
21 * VALEUR RETOURNEE:0 si echec 1 si oui               *
22 ******************************************************/
23int
24readINI (char *sectionName, char *pKeyName, char **ppStr, char *fileName)
25{
26    INI *pIni = NULL;
27    SECTION *pSection = NULL;
28    KEY *pKey = NULL;
29    int trouve = 0;
30    char *pSectionName = NULL;
31    int offset = 0;
32 
33
34    trouve = 0;
35    pIni = _loadINI (fileName);
36    if (!pIni) return 0;
37
38    /*recherche de la section */
39    offset = strlen (sectionName) + 3;
40    pSectionName = new char [offset];
41    pSectionName[0] = '[';
42    strcpy (pSectionName + 1, sectionName);
43    pSectionName[strlen (sectionName) + 1] = ']';
44    pSectionName[offset - 1]='\0';
45
46    pSection = _findSECTION (pIni, pSectionName);
47
48    delete [] pSectionName;
49
50    if (pSection != NULL)
51    {
52        /*recherche de la cle */
53        pKey = _findKEY (pSection, pKeyName);
54        if (pKey != NULL)
55        {
56            *ppStr = new char [strlen (pKey->value) + 2];
57            strcpy (*ppStr, pKey->value);
58            (*ppStr)[strlen (pKey->value)] = '\0';
59            trouve = 1;
60
61        }
62    }
63
64    //On efface toutes les clés et toutes les sections
65
66    KEY *keyprev=NULL;
67    SECTION * secprev=NULL;
68    pSection=pIni->fils;
69
70    do
71    {
72        pKey=pSection->fils;
73
74        do
75        {
76            if (pKey->name) delete [] pKey->name;
77            if (pKey->value) delete [] pKey->value;
78
79            keyprev=pKey;
80
81            KEY *prev=pKey;
82            pKey=pKey->next;
83
84            delete prev;
85
86        } while (pKey!=keyprev);
87 
88        if (pSection->name) delete [] pSection->name;
89
90        secprev=pSection;
91
92        SECTION *sprev=pSection;
93        pSection=pSection->next;
94
95        delete sprev;
96
97    }  while (pSection!=secprev);
98
99
100
101    delete [] pIni->fileName;
102    delete pIni;
103    pIni=NULL;
104
105    return trouve;
106}
107
108int
109readINI (const char *sectionName, const char *pKeyName, char **ppStr, const char *fileName)
110{
111     return readINI((char*)sectionName, (char*)pKeyName, ppStr, (char*)fileName);
112}
113
114/******************************************************
115 * NOM:                delINI                         *
116 * SPECIFICATION:                                     *
117 *  seule la cle de la section est supprimee          *
118 *  si la section est vide, elle est supprimee        *
119 * ARGUMENTS:                                         *
120 * >section: le nom de la section sans les crochets   *
121 * >key: nom de la cle de la section                  *
122 * >pStr: pointeur pour mettre la valeur de la cle    *
123 *       avant sa suppression                         *
124 *       allocation memoire effectuee                 *
125 * ENVIRONNEMENT:                                     *
126 * VALEUR RETOURNEE:                                  *
127 * 1 si reussite de la suppression, 0 sinon           *
128 ******************************************************/
129/*
130int
131delINI (char *sectionName, char *pKeyName, char **ppStr, char *fileName)
132{
133  INI *pIni = NULL;
134  SECTION *pSection = NULL;
135  KEY *pKey = NULL;
136  int del = 0;
137  char *pSectionName = NULL;
138  int offset = 0;
139  //pStr = resultValue;
140  TRY
141  {
142
143    pIni = _loadINI (fileName);
144    //recherche de la section
145    offset = strlen (sectionName) + 3;
146    pSectionName = (char *) m_malloc (sizeof (char) * offset);
147    pSectionName[0] = '[';
148    strcpy (pSectionName + 1, sectionName);
149    pSectionName[strlen (sectionName) + 1] = ']';
150    pSectionName[offset - 1];
151
152    pSection = _findSECTION (pIni, pSectionName);
153
154    if (pSection != NULL)
155      {
156        if (pKeyName == NULL)
157          {
158            //on supprime toute la section
159
160            pKey = _lastKEY (pSection);
161            while (pKey != NULL)
162              {
163                _delKEY (pKey);
164                pKey = _lastKEY (pSection);
165              }
166            _delSECTION (pSection);
167            del = 1;
168          }
169        else
170          {
171            //recherche de la cle si indiquee
172            pKey = _findKEY (pSection, pKeyName);
173            if (pKey != NULL)
174              {
175                (*ppStr) =
176                  (char *) m_malloc (sizeof (char) * strlen (pKey->value) +
177                                     2);
178                strcpy (*ppStr, pKey->value);
179                (*ppStr)[strlen (pKey->value)] = '\0';
180                if (_delKEY (pKey))
181                  {
182                    //si la cle supprimee etait la derniere de la section
183                    _delSECTION (pSection);
184                  }
185                del = 1;
186              }
187          }
188      }
189    if (del)
190      {
191        _saveINI (pIni);
192      }
193  }
194  CATCH (ERR_FOPEN)
195  {
196    printf ("impossible d'ouvrir le fichier %s\n", fileName);
197    (*ppStr) = NULL;
198  }
199  CATCH (ERR_MALLOC)
200  {
201    perror ("Probleme d'allocation memoire");
202    exit (EXIT_FAILURE);
203  }
204  ENDTRY;
205
206  return del;
207}
208*/
209/******************************************************
210 * NOM:                writeINI                       *
211 * SPECIFICATION:                                     *
212 *  si la section n'existe pas elle est cree          *
213 *  si la cle n'existe pas elle est cree              *
214 *  et la valeur de la cle est initialisee            *
215 *  sinon la cle existante de la section existante est*
216 *  mise a jour                                       *
217 * ARGUMENTS:                                         *
218 * ENVIRONNEMENT:                                     *
219 * VALEUR RETOURNEE:                                  *
220 *  NULL si echec ou keyValue si reussite             *
221 ******************************************************/
222
223
224/*
225char * writeINI (char *sectionName, char *keyName, char *keyValue, char *fileName)
226{
227  char *pSectionName = NULL;
228  char *pKeyName = NULL;
229  char *pKeyValue = NULL;
230  INI  *pIni = NULL;
231  SECTION *pSection = NULL;
232  KEY *pKey = NULL;
233  TRY
234  {
235    pKeyName = new char [sizeof (char) * strlen (keyName) + 1];
236    pSectionName = new char [sizeof (char) * strlen (sectionName) + 3];
237    if (keyValue != NULL)
238      {
239        pKeyValue = new char [sizeof (char) * strlen (keyValue) + 1];
240        strcpy (pKeyValue, keyValue);
241        pKeyValue[strlen (keyValue)] = '\0';
242      }
243    else
244      {
245        pKeyValue = new char [sizeof (char)];
246        pKeyValue[0] = '\0';
247      }
248    strcpy (pKeyName, keyName);
249    pSectionName[0] = '[';
250    strcpy (pSectionName + 1, sectionName);
251    pSectionName[strlen (sectionName) + 1] = ']';
252    pSectionName[strlen (sectionName) + 2] = '\0';
253    //chargement
254    pIni = _loadINI (fileName);
255  }
256  CATCH (ERR_FOPEN)
257  {
258    printf ("Erreur en ouvrant le fichier .ini:%s\n", fileName);
259    exit (EXIT_FAILURE);
260  }
261  CATCH (ERR_MALLOC)
262  {
263    perror ("Probleme d'allocation memoire");
264    exit (EXIT_FAILURE);
265  }
266  ENDTRY;
267
268  //recherche de la section
269  pSection = _findSECTION (pIni, pSectionName);
270  //recherche de la cle
271  if (pSection != NULL)
272    {
273      pKey = _findKEY (pSection, pKeyName);
274      if (pKey != NULL)
275        {
276          if (pKey->value != NULL) { delete [] pKey->value; pKey->value = NULL; pKey->value = new char [sizeof (char) * strlen (pKeyValue) + 1]; }
277         
278          if (pKey->value == NULL)
279            {
280              perror ("probleme de reallocation memoire");
281              exit (EXIT_FAILURE);
282            }
283          strcpy (pKey->value, pKeyValue);
284        }
285      else
286        {
287          //cle pas trouve dans la section, on ajoute la cle a la section
288          pKey = _newKEY (pKeyName, pKeyValue);
289          _addKEY (pSection, pKey);
290        }
291    }
292  else
293    {
294       //section pas trouve, on la cree avec sa cle
295      pSection = _newSECTION (pSectionName);
296      _addSECTION (pIni, pSection);
297      pKey = _newKEY (pKeyName, pKeyValue);
298      _addKEY (pSection, pKey);
299
300    }
301  TRY
302  {
303    //sauvegarde
304    _saveINI (pIni);
305
306  }
307  CATCH (ERR_FOPEN)
308  {
309    printf ("erreur sur le fichier %s", pIni->fileName);
310  }
311  ENDTRY;
312
313  //nettoyage
314  if (pKey->value != NULL) delete [] pKey->value;
315  if (pKey != NULL) delete [] pKey;
316  if (pKeyName != NULL) delete [] pKeyName;
317  if (pSectionName != NULL) delete [] pSectionName;
318  if (pIni != NULL) delete [] pIni;
319  if (pSection != NULL) delete [] pSection;
320  pIni = NULL;
321  pKeyName = NULL;
322  pSectionName = NULL;
323  return pKeyValue;
324}
325*/
326
327/******************************************************
328 * NOM:                                               *
329 *                 _delKEY                            *
330 * SPECIFICATION:                                     *
331 * ARGUMENTS:                                         *
332 * ENVIRONNEMENT:                                     *
333 * VALEUR RETOURNEE:                                  *
334 *  1 si derniere cellule, 0 sinon, -1 si probleme    *
335 ******************************************************/
336int
337_delKEY (KEY * pKey)
338{
339    int result = 0;
340    if (pKey != NULL)
341    {
342        /*la cle n'est pas la premiere */
343        if (pKey->prev != pKey)
344        {
345            if (pKey->next != pKey)
346            {
347                /*la cle n'est pas la derniere */
348                pKey->prev->next = pKey->next;
349            }
350            else
351            {
352                result = 1;
353                pKey->prev->next = pKey->prev;
354            }
355        }
356        else
357        {
358            if (pKey->next != pKey)
359            {
360                /*la cle n'est pas non plus la derniere */
361                pKey->parent->fils = pKey->next;
362            }
363            else
364            {
365                result = 1;
366                pKey->parent->fils = NULL;
367            }
368        }
369        _freeKEY (pKey);
370        return result;
371    }
372    else
373    {
374        result = -1;
375        return result;
376    }
377}
378
379
380/******************************************************
381 * NOM:                                               *
382 *                 _delSECTION                        *
383 * SPECIFICATION:                                     *
384 * ARGUMENTS:                                         *
385 * ENVIRONNEMENT:                                     *
386 * VALEUR RETOURNEE:                                  *
387 *  pointeur sur la section                           *
388 ******************************************************/
389int
390_delSECTION (SECTION * pSection)
391{
392    if (pSection != NULL)
393    {
394        /*la cle n'est pas la premiere */
395        if (pSection->prev != pSection)
396        {
397            /*la cle n'est pas la derniere */
398            if (pSection->next != pSection)
399            {
400                pSection->prev->next = pSection->next;
401            }
402            else
403            {
404                pSection->prev->next = pSection->prev;
405            }
406        }
407        else
408        {
409            /*la cle n'estpas nonplus la derniere */
410            if (pSection->next != pSection)
411            {
412                pSection->parent->fils = pSection->next;
413                pSection->next->prev = pSection->next;
414            }
415            else
416            {
417                pSection->parent->fils = NULL;
418            }
419        }
420        _freeSECTION (pSection);
421        return 1;
422    }
423    else
424    {
425        return 0;
426    }
427}
428
429/************************
430 * liberation memoire   *
431 ************************/
432void
433_freeKEY (KEY * pKey)
434{
435    if (pKey != NULL)
436    {
437        pKey->parent = NULL;
438        pKey->next = NULL;
439        pKey->prev = NULL;
440        if (pKey->value != NULL)
441        {
442            delete [] pKey->value;
443        }
444        if (pKey->name != NULL)
445        {
446            delete [] pKey->name;
447        }
448        delete pKey;
449
450        pKey=NULL;
451    }
452}
453void
454_freeSECTION (SECTION * pSection)
455{
456    if (pSection != NULL)
457    {
458        pSection->parent = NULL;
459        pSection->next = NULL;
460        pSection->prev = NULL;
461        if (pSection->name != NULL)
462        {
463            delete [] pSection->name;
464        }
465        delete pSection;
466
467        pSection=NULL;
468    }
469}
470
471/******************************************************
472 * NOM:                                               *
473 *                 _newKEY                            *
474 * SPECIFICATION:                                     *
475 *  allocation d'une nouvelle cle                     *
476 * ARGUMENTS:                                         *
477 * le nom et la valeur de la cle                      *
478 * ENVIRONNEMENT:                                     *
479 * VALEUR RETOURNEE:                                  *
480 * pointeur sur la cle, NULL sinon                    *
481 ******************************************************/
482KEY *
483_newKEY (char *pKeyName, char *pKeyValue)
484{
485    KEY *pKey;
486
487    {
488        pKey = new KEY;
489
490        pKey->name = new char [strlen (pKeyName) + 1];
491        pKey->value = new char [strlen (pKeyValue) + 1];
492    }
493
494
495    strcpy (pKey->name, pKeyName);
496    strcpy (pKey->value, pKeyValue);
497    pKey->parent = NULL;
498    pKey->prev = pKey;
499    pKey->next = pKey;
500
501    return pKey;
502}
503
504/******************************************************
505 * NOM:                                               *
506 *                 _newSECTION                        *
507 * SPECIFICATION:                                     *
508 *  allocation d'une nouvelle section                 *
509 * ARGUMENTS:                                         *
510 * le nom de la section                               *
511 * ENVIRONNEMENT:                                     *
512 * VALEUR RETOURNEE:                                  *
513 * pointeur sur la section, NULL sinon                *
514 ******************************************************/
515SECTION *
516_newSECTION (char *pSectionName)
517{
518    SECTION *pSection;
519
520    {
521        pSection = new SECTION;
522
523        pSection->name = new char [strlen (pSectionName) + 1];
524    }
525
526
527    strcpy (pSection->name, pSectionName);
528    pSection->parent = NULL;
529    pSection->fils = NULL;
530    pSection->prev = pSection;
531    pSection->next = pSection;
532
533    return pSection;
534}
535
536/******************************************************
537 * NOM:                                               *
538 *                 _findSECTION                       *
539 * SPECIFICATION:                                     *
540 *  trouver la section dans une struture INI          *
541 * ARGUMENTS:                                         *
542 * la structure INI, le nom de la section avec ses [ ]*
543 * ENVIRONNEMENT:                                     *
544 * VALEUR RETOURNEE:                                  *
545 * pointeur sur la section, NULL sinon                *
546 ******************************************************/
547SECTION *
548_findSECTION (INI * pIni, char *pSectionName)
549{
550    SECTION *pSection = NULL;
551    pSection = pIni->fils;
552    while ((pSection != NULL) && (strcmp (pSection->name, pSectionName) != 0))
553    {
554        if (pSection == pSection->next)
555        {
556            pSection = NULL;
557        }
558        else
559        {
560            pSection = pSection->next;
561        }
562    }
563    return pSection;
564}
565
566/******************************************************
567 * NOM:                                               *
568 *                 _findKEY                           *
569 * SPECIFICATION:                                     *
570 *  trouver la cle dans une struture SECTION          *
571 * ARGUMENTS:                                         *
572 * la structure SECTION, le nom de la cle             *
573 * ENVIRONNEMENT:                                     *
574 * VALEUR RETOURNEE:                                  *
575 * pointeur sur la cle, NULL sinon                    *
576 ******************************************************/
577KEY *
578_findKEY (SECTION * pSection, char *pKeyName)
579{
580    KEY *pKey = NULL;
581    pKey = pSection->fils;
582    while ((pKey != NULL) && (strcmp (pKey->name, pKeyName) != 0))
583    {
584        if (pKey == pKey->next)
585        {
586            pKey = NULL;
587        }
588        else
589        {
590            pKey = pKey->next;
591        }
592    }
593    return pKey;
594}
595
596/******************************************************
597 * NOM:                                               *
598 *                 _lastKEY                           *
599 * SPECIFICATION:                                     *
600 *  trouver la derniere cle dans une struture SECTION *
601 * ARGUMENTS:                                         *
602 * la structure SECTION                               *
603 * ENVIRONNEMENT:                                     *
604 * VALEUR RETOURNEE:                                  *
605 * pointeur sur la cle, NULL sinon                    *
606 ******************************************************/
607KEY *
608_lastKEY (SECTION * pSection)
609{
610    KEY *pKey_pred = NULL;
611    KEY *pKey = NULL;
612    pKey = pSection->fils;
613    pKey_pred = pKey;
614    while (pKey != NULL)
615    {
616        pKey_pred = pKey;
617        if (pKey == pKey->next)
618        {
619            pKey = NULL;
620        }
621        else
622        {
623            pKey = pKey->next;
624        }
625    }
626    return pKey_pred;
627}
628
629/******************************************************
630 * NOM:                                               *
631 *                 _lastSECTION                       *
632 * SPECIFICATION:                                     *
633 *  trouver la derniere section dans une INI          *
634 * ARGUMENTS:                                         *
635 * la structure INI                                   *
636 * ENVIRONNEMENT:                                     *
637 * VALEUR RETOURNEE:                                  *
638 * pointeur sur la section, NULL sinon                *
639 ******************************************************/
640SECTION *
641_lastSECTION (INI * pIni)
642{
643    SECTION *pSection_pred = NULL;
644    SECTION *pSection = NULL;
645    pSection = pIni->fils;
646    pSection_pred = pSection;
647    while (pSection != NULL)
648    {
649        pSection_pred = pSection;
650        if (pSection == pSection->next)
651        {
652            pSection = NULL;
653        }
654        else
655        {
656            pSection = pSection->next;
657        }
658    }
659    return pSection_pred;
660}
661
662/******************************************************
663 * NOM:                                               *
664 *                 _addSECTION                        *
665 * SPECIFICATION:                                     *
666 *  ajoute une section en derniere place dans une INI *
667 *  la section ne doit pas appartenir a une INI       *
668 * ARGUMENTS:                                         *
669 * la structure INI, la structure SECTION             *
670 * ENVIRONNEMENT:                                     *
671 * VALEUR RETOURNEE:                                  *
672 * pointeur sur la section, NULL sinon                *
673 ******************************************************/
674SECTION *
675_addSECTION (INI * pIni, SECTION * pSection)
676{
677    SECTION *pSection_last = NULL;
678
679    if (pSection->parent != NULL)
680    {
681        return NULL;
682    }
683    else
684    {
685        pSection_last = _lastSECTION (pIni);
686        pSection->parent = pIni;
687        if (pSection_last != NULL)
688        {
689            pSection_last->next = pSection;
690            pSection->prev = pSection_last;
691        }
692        else
693        {
694            pIni->fils = pSection;
695        }
696        return pSection;
697    }
698}
699
700/******************************************************
701 * NOM:                                               *
702 *                 _addKEY                            *
703 * SPECIFICATION:                                     *
704 *  ajoute une cle en derniere place dans une SECTION *
705 *  la section ne doit pas appartenir a une SECTION   *
706 * ARGUMENTS:                                         *
707 * la structure KEY, la structure SECTION             *
708 * ENVIRONNEMENT:                                     *
709 * VALEUR RETOURNEE:                                  *
710 * pointeur sur la cle, NULL sinon                *
711 ******************************************************/
712KEY *
713_addKEY (SECTION * pSection, KEY * pKey)
714{
715    KEY *pKey_last = NULL;
716
717    if (pKey->parent != NULL)
718    {
719        return NULL;
720    }
721    else
722    {
723
724        pKey_last = _lastKEY (pSection);
725        pKey->parent = pSection;
726        if (pKey_last != NULL)
727        {
728            pKey_last->next = pKey;
729            pKey->prev = pKey_last;
730        }
731        else
732        {
733            pSection->fils = pKey;
734        }
735        pKey->next = pKey;
736        return pKey;
737    }
738}
739
740/******************************************************
741 * NOM:                                               *
742 *                 saveINI                            *
743 * SPECIFICATION:                                     *
744 *  sauvegarde de la structure INI dans son fichier   *
745 *  Attention, ecrase l'ancien fichier                *
746* ARGUMENTS:                                         *
747*  pointeur sur la structure INI a sauvegarder       *
748* ENVIRONNEMENT:                                     *
749* VALEUR RETOURNEE:                                  *
750*  -1 si un probleme a eu lieu, 0 sinon              *
751******************************************************/
752
753/*
754 int
755_saveINI (INI * pIni)
756{
757  FILE *pFile = NULL;
758  char *str = NULL;
759  SECTION *pSection_pred = NULL, *pSection = NULL;
760  KEY *pKey_pred = NULL, *pKey = NULL;
761  int offset = 0;
762
763  pFile = fopen (pIni->fileName, "w");
764  if (pFile == NULL)
765    THROW (ERR_FOPEN);
766  //printf("#SAUVEGARDE Fichier INI#\n");
767  //printf("# %s #\n\n",pIni->fileName);
768  if (pIni->fils != NULL)
769    {
770      pSection = pIni->fils;
771      do
772        {
773
774          fprintf (pFile, pSection->name);
775          fprintf (pFile, "\n");
776          pKey = pSection->fils;
777          do
778            {
779
780              offset = strlen (pKey->name) + strlen (pKey->value) + 2;
781
782              TRY
783              {
784                str = new char [sizeof (char) * offset];
785              }
786              CATCH (ERR_MALLOC)
787              {
788                perror ("probleme d'allocation memoire");
789                exit (EXIT_FAILURE);
790              }
791              ENDTRY;
792              memset (str, 0, offset);
793              strcpy (str, pKey->name);
794              offset = strlen (pKey->name);
795              strcpy (str + offset, "=");
796              offset = strlen (pKey->name) + 1;
797              strcpy (str + offset, pKey->value);
798              fprintf (pFile, str);
799              fprintf (pFile, "\n");
800              pKey_pred = pKey;
801              pKey = pKey->next;
802              delete [] str;
803            }
804          while (pKey != pKey_pred);
805
806          pSection_pred = pSection;
807          pSection = pSection->next;
808        }
809      while (pSection != pSection_pred);
810    }
811  //nettoyage
812  fclose (pFile);
813}*/
814
815/******************************************************
816 * NOM:                                               *
817 *                  loadINI                           *
818 * SPECIFICATION:                                     *
819 *  Charge le contenu d'un fichier .ini en memoire    *
820 * ARGUMENTS:                                         *
821 *  nom du fichier                                    *
822 * ENVIRONNEMENT:                                     *
823 * VALEUR RETOURNEE:                                  *
824 *  pointeur sur la structure INI contenant le fichier*
825 *  .ini avec les structures decrites dans le .h      *
826 * EXCEPTION:                                         *
827 * ERR_FOPEN, ERR_MALLOC
828 ******************************************************/
829INI *
830_loadINI (char *fileName)
831{
832    FILE *pFile = NULL;
833    char *pBuf;
834    int nbRead = 0;
835    SECTION *pSection = NULL;
836    KEY *pKey = NULL;
837
838    INI *pIni = NULL;
839    pBuf = new char [MAX_LENGHT];
840
841    pFile = fopen (fileName, "r");
842    if (pFile == NULL)
843    {
844        pFile = fopen (fileName, "a");
845        if (pFile == NULL)
846           
847        return NULL;
848    }
849
850    pIni = new INI;
851    pIni->fileName = new char [strlen (fileName) + 1];
852    strcpy (pIni->fileName, fileName);
853    pIni->fils = NULL;
854    /* on lit ligne par ligne */
855    nbRead = readline (pFile, pBuf);
856  //  if (nbRead==-1) { fclose(pFile); delete  [] pIni->fileName; delete pIni; delete [] pBuf; return 0;}
857    while (nbRead != -1)
858    {
859
860        if ((pBuf[0] == '[') && (pBuf[strlen (pBuf) - 1] == ']'))
861        {
862            /* une section */
863            pSection = NULL;
864
865            pSection = _newSECTION (pBuf);
866            _addSECTION (pIni, pSection);
867        }
868
869        /* les cles de la section */
870        while (((nbRead = readline (pFile, pBuf)) !=-1) && (pBuf[0] != '['))
871        {
872            int ind = 0;
873            pKey = NULL;
874            if ((ind = _isKey (pBuf)) >= 0)
875            {
876
877                char *strName = NULL;
878                strName = new char [ind + 1];
879                strncpy (strName, pBuf, ind);
880                strName[ind] = '\0';
881                pKey = _newKEY (strName, pBuf + ind + 1);
882                _addKEY (pSection, pKey);
883                delete [] strName;
884            }
885        }
886    }
887
888    /*nettoyage */
889    delete [] pBuf;
890    fclose (pFile);
891    return pIni;
892}
893
894/******************************************************
895 * NOM:                                               *
896 *                 _isKey                             *
897 * SPECIFICATION:                                     *
898 *  determine si une chaine est une cle valide        *
899 * ARGUMENTS:                                         *
900 *  pointeur sur la chaine                            *
901 * ENVIRONNEMENT:                                     *
902 * VALEUR RETOURNEE:                                  *
903 *  indice du signe '=' si oui, -1 sinon              *
904 ******************************************************/
905int
906_isKey (char *str)
907{
908    int ind = 0;
909    for (ind; (ind < strlen (str)) && (str[ind] != '='); ind++);
910    if (str[ind] == '=')
911        return ind;
912    else
913        return -1;
914}
915
916
917/******************************************************
918 * NOM:                                               *
919 *                  _printINI                         *
920 * SPECIFICATION:                                     *
921 * affiche a l'ecran le contenu de structure INI      *
922 * ARGUMENTS:                                         *
923 *  pointeur sur la structure INI                     *
924 * ENVIRONNEMENT:                                     *
925 * VALEUR RETOURNEE:                                  *
926 ******************************************************/
927/*void
928_printINI (INI * pIni)
929{
930  SECTION *pSection_pred = NULL;
931  SECTION *pSection = NULL;
932  printf ("#Fichier INI#\n");
933  printf ("# %s #\n\n", pIni->fileName);
934  pSection = pIni->fils;
935  if (pSection != NULL)
936    {
937      do
938        {
939          _printSECTION (pSection);
940          pSection_pred = pSection;
941          pSection = pSection->next;
942        }
943      while (pSection != pSection_pred);
944    }
945  printf ("\n#Fin du fichier INI#\n");
946}*/
947
948/******************************************************
949 * NOM:                                               *
950 *                  _printSECTION                     *
951 * SPECIFICATION:                                     *
952 * affiche a l'ecran le contenu de structure SECTION  *
953 * ARGUMENTS:                                         *
954 *  pointeur sur la structure SECTION                 *
955 * ENVIRONNEMENT:                                     *
956 * VALEUR RETOURNEE:                                  *
957 ******************************************************/
958/*void
959_printSECTION (SECTION * pSection)
960{
961  KEY *pKey = NULL;
962  KEY *pKey_pred = NULL;
963  printf ("%s\n", pSection->name);
964  pKey = pSection->fils;
965  if (pKey != NULL)
966    {
967      do
968        {
969          _printKEY (pKey);
970          pKey_pred = pKey;
971          pKey = pKey->next;
972        }
973      while (pKey != pKey_pred);
974    }
975}*/
976
977/******************************************************
978 * NOM:                                               *
979 *                  _printKEY                         *
980 * SPECIFICATION:                                     *
981 * affiche a l'ecran le contenu de structure SECTION  *
982 * ARGUMENTS:                                         *
983 *  pointeur sur la structure SECTION                 *
984 * ENVIRONNEMENT:                                     *
985 * VALEUR RETOURNEE:                                  *
986 ******************************************************/
987/*
988 * void
989_printKEY (KEY * pKey)
990{
991  printf ("%s=%s\n", pKey->name, pKey->value);
992}*/
993
994/******************************************************
995 * NOM:                                               *
996 *                  printINI                          *
997 * SPECIFICATION:                                     *
998 * affiche a l'ecran le contenu du fichier .ini       *
999 * ARGUMENTS:                                         *
1000 *  nom du fichier                                    *
1001 * ENVIRONNEMENT:                                     *
1002 * VALEUR RETOURNEE:                                  *
1003 ******************************************************/
1004
1005/*void
1006printINI (char *file)
1007{
1008  INI *pIni = NULL;
1009  TRY
1010  {
1011    pIni = _loadINI (file);
1012  }
1013  CATCH (ERR_MALLOC)
1014  {
1015    printf ("Erreur d'allocation memoire en chargeant le fichier .ini:%s\n",
1016            file);
1017    exit (EXIT_FAILURE);
1018  }
1019  CATCH (ERR_FOPEN)
1020  {
1021    printf ("Erreur en ouvrant le fichier .ini:%s\n", file);
1022    exit (EXIT_FAILURE);
1023  }
1024  ENDTRY;
1025  _printINI (pIni);
1026  free (pIni);
1027}*/
1028
1029/******************************************************
1030 * NOM:                 jumpline                      *
1031 * SPECIFICATION:                                     *
1032 * fonction permettant de sauter des lignes dans le   *
1033 * fichier a partir de la position courante dans le   *
1034 * fichier                                            *
1035 * ARGUMENTS:                                         *
1036 * nombre de lignes a saute, pointeur sur le fichier  *
1037 * ENVIRONNEMENT:                                     *
1038 * VALEUR RETOURNEE: nombre de lignes sautees         *
1039 ******************************************************/
1040
1041int
1042jumpline (int nbline, FILE * pfile)
1043{
1044    int compteur = 0, c;
1045
1046    while ((compteur < nbline) && ((c = getc (pfile)) != EOF))
1047    {
1048        if (c == '\n')
1049            compteur++;
1050    }
1051    return (compteur);
1052}
1053
1054/******************************************************
1055 * NOM:                readline                       *
1056 * SPECIFICATION:                                     *
1057 * recupere la ligne courante du fichier et renvoie le*
1058 * nombre de caracteres lus                           *
1059 * ARGUMENTS:                                         *
1060 * pointeur sur le fichier a lire                     *
1061 * pointeur sur le tableau recevant la ligne          *
1062 * ENVIRONNEMENT:                                     *
1063 * VALEUR RETOURNEE:                                  *
1064 * nombre de caracteres lus                           *
1065 ******************************************************/
1066int
1067readline (FILE * pfile, char *tab)
1068{
1069    int nbchar = 0;
1070    char c;
1071    while ((c = getc (pfile)) != '\n')
1072    {
1073        if (c == EOF)
1074        {
1075            break;
1076        }
1077        tab[nbchar++] = c;
1078    }
1079    tab[nbchar] = '\0';
1080
1081    while (nbchar>0 && tab[nbchar-1]==' ') {
1082        tab[--nbchar] = '\0';
1083    }
1084
1085    while (tab[0]==' ') for (int i=1; i<=nbchar; i++) tab[i-1]=tab[i];
1086
1087    if (c == EOF) nbchar=-1;
1088
1089    return (nbchar);
1090}
1091
1092/******************************************************
1093 * NOM:                m_malloc                       *
1094 * SPECIFICATION:                                     *
1095 *  clone de malloc renvoyant une exception           *
1096 * ARGUMENTS:                                         *
1097 * ENVIRONNEMENT:                                     *
1098 * VALEUR RETOURNEE:                                  *
1099 ******************************************************/
1100
1101/*
1102void * m_malloc (size_t size)
1103{
1104  void *ptr = malloc (size);
1105  if (ptr == NULL)
1106    THROW (ERR_MALLOC);
1107  else
1108    return ptr;
1109}*/
Note: See TracBrowser for help on using the repository browser.