This repository was archived by the owner on Apr 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneralTypes.c
198 lines (174 loc) · 6.51 KB
/
GeneralTypes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************/
/* */
/* But: Implementation des types generaux. */
/* */
/****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "GeneralTypes.h"
/*******************************/
/* IMPLEMENTATION DES ROUTINES */
/*******************************/
/*========================================*/
/* Manipulation des chaines de caracteres */
/*========================================*/
/*-----------------*/
/* Gestion memoire */
/*-----------------*/
/*--------------------------------------------------------------------------*/
/* Construction d'une chaine de caracteres */
/*--------------------------------------------------------------------------*/
/* Entree: | CharStrSize | Taille de la chaine */
/* Sortie: | I_CharStr | Chaine */
/* Erreur: | I_CharStr=NULL | Erreur d'allocation */
/*--------------------------------------------------------------------------*/
TCharStr I_CharStr(TCharStrSize CharStrSize)
{
TCharStr CharStr;
CharStr = (TChar*)calloc(CharStrSize + 1, sizeof(TChar));
if (!CharStr) {
return NULL;
}
return CharStr;
}
/*--------------------------------------------------------------------------*/
/* Destruction d'une chaine de caracteres */
/*--------------------------------------------------------------------------*/
/* Entree: | CharStr | Chaine */
/* Sortie: | - | - */
/* Erreur: | - | - */
/*--------------------------------------------------------------------------*/
void Free_CharStr(TCharStr CharStr)
{
free(CharStr);
}
/*---------------*/
/* Constructeurs */
/*---------------*/
/*--------------------------------------------------------------------------*/
/* Construction d'une chaine a partir de n caracteres */
/*--------------------------------------------------------------------------*/
/* Entree: | CharStr | Chaine */
/* Sortie: | CharStr_ToUpper | Chaine convertie */
/* Erreur: | - | - */
/*--------------------------------------------------------------------------*/
TCharStr CharStr_ToUpper(TCharStr CharStr)
{
unsigned int CharIndex;
/* Conversion en majuscules */
for (CharIndex = 0; CharIndex < CharStr_Length(CharStr); CharIndex++) {
CharStr[CharIndex] = toupper(CharStr[CharIndex]);
}
return CharStr;
}
/*--------------------------------------------------------------------------*/
/* Construction d'un mot a partir de n caracteres */
/*--------------------------------------------------------------------------*/
/* Entree: | Str | Chaine */
/* | CharNbr | Nbr de car a prendre de la chaine */
/* Sortie: | nChar_To_CharStr | Chaine */
/* Erreur: | nChar_To_CharStr=NULL | Erreur d'allocation */
/*--------------------------------------------------------------------------*/
TCharStr nChar_To_CharStr(char *Str, unsigned int CharNbr)
{
TCharStr CharStr;
CharStr = I_CharStr((TCharStrSize)CharNbr);
if (!CharStr) {
return NULL;
}
CharStr = (TCharStr)strncpy((char*)CharStr, Str, (size_t)CharNbr);
CharStr[CharNbr] = 0;
return CharStr;
}
/*--------------------------------------------------------------------------*/
/* Construction d'un mot a partir d'une chaine */
/*--------------------------------------------------------------------------*/
/* Entree: | Str | Chaine */
/* Sortie: | Str_To_CharStr | Chaine */
/* Erreur: | Str_To_CharStr=NULL | Erreur d'allocation */
/*--------------------------------------------------------------------------*/
TCharStr Str_To_CharStr(char *Str)
{
TCharStr CharStr;
CharStr = I_CharStr((TCharStrSize)strlen(Str));
if (!CharStr) {
return NULL;
}
CharStr = (TCharStr)strcpy((char*)CharStr, Str);
return CharStr;
}
/*=========================*/
/* Manipulation des listes */
/*=========================*/
/*-----------------*/
/* Gestion memoire */
/*-----------------*/
/*--------------------------------------------------------------------------*/
/* Construction d'une liste */
/*--------------------------------------------------------------------------*/
/* Entree: | MaxPtrNbr | Nombre maximum d'elements */
/* Sortie: | I_List | Liste */
/* Erreur: | I_List=NULL | Erreur d'allocation */
/*--------------------------------------------------------------------------*/
TListPtr I_List(TPtrIndex MaxPtrNbr)
{
TListPtr ListPtr;
ListPtr = (TListPtr)malloc(sizeof(TList));
if (!ListPtr) {
/* Erreur d'allocation */
return NULL;
}
ListPtr->PtrArray = (void*)calloc(MaxPtrNbr, sizeof(void*));
if (!(ListPtr->PtrArray)) {
/* Erreur d'allocation */
free(ListPtr);
return NULL;
}
ListPtr->PtrNbr = 0;
ListPtr->MaxPtrNbr = MaxPtrNbr;
return ListPtr;
}
/*--------------------------------------------------------------------------*/
/* Destruction d'une liste sans ses element */
/*--------------------------------------------------------------------------*/
/* Entree: | ListPtr | Liste */
/* Sortie: | - | - */
/* Erreur: | - | - */
/*--------------------------------------------------------------------------*/
void Free_List(TListPtr ListPtr)
{
free(ListPtr->PtrArray);
free(ListPtr);
}
/*--------------------------------------------------------------------------*/
/* Recalibration d'une liste */
/*--------------------------------------------------------------------------*/
/* Entree: | ListPtr | Liste */
/* Sortie: | - | - */
/* Erreur: | - | - */
/*--------------------------------------------------------------------------*/
void Resize_List(TListPtr ListPtr)
{
ListPtr->PtrArray = (void*)realloc(ListPtr->PtrArray, (ListPtr->PtrNbr)*
sizeof(void*));
ListPtr->MaxPtrNbr = ListPtr->PtrNbr;
}
/*------------*/
/* Selecteurs */
/*------------*/
/*--------------------------------------------------------------------------*/
/* Routine indiquant si la liste est complete */
/*--------------------------------------------------------------------------*/
/* Entree: | ListPtr | Liste */
/* Sortie: | List_Complete | Etat de la liste */
/* Erreur: | - | - */
/*--------------------------------------------------------------------------*/
TBoolean List_Complete(TListPtr ListPtr)
{
TPtrIndex PtrIndex;
for (PtrIndex = 0; PtrIndex < List_PtrNbr(ListPtr); PtrIndex++) {
if (List_PtrNth(ListPtr, PtrIndex) == NULL) return FALSE;
}
return TRUE;
}