source: BAORadio/libindi/v1.0.1/BAOControl/filetools.c@ 674

Last change on this file since 674 was 502, checked in by frichard, 15 years ago

-BAOControl : petite interface permettant de contrôler les antennes via le pilote indi_BAO
-Le pilote indi_BAO utilise désormais indilib v 0.7

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