00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
#include <stdlib.h>
00037
#include "idliste.h"
00038
#include "idprint.h"
00039
#include "iderr.h"
00040
00041
00042
00043
00044
00045
00093
int
00094 IdLstAddElement(
PLIST list,
void *obj,
int mode,
PLIST_ELEMENT elem)
00095 {
00096
PLIST_ELEMENT e, p;
00097
00098
if (!list) {
00099
IdErrno =
IDERR_POINTER_IS_NULL;
00100
return (0);
00101 }
00102
00103 e = calloc(1,
sizeof(
LIST_ELEMENT));
00104
if (!e) {
00105
IdErrno =
IDERR_ALLOC_ELEM;
00106
return (0);
00107 }
00108
00109 (
_IdLstPrivate(list)->NbElems)++;
00110 e->
Numero =
IdLstNbElems(list);
00111
switch (mode & 0x0f) {
00112
case LST_BEG:
00113
if (mode & (
LST_NEXT |
LST_BIDIR)) {
00114 p =
IdLstFirst(list);
00115
if (p == 0)
00116
IdLstLast(list) = e;
00117 e->
Next = p;
00118
IdLstFirst(list) = e;
00119 e->
Object = obj;
00120
if (mode &
LST_BIDIR)
00121
if (e->
Next)
00122 e->
Next->
Pred = e;
00123 }
00124
if (mode &
LST_PRED) {
00125 e->
Pred =
IdLstFirst(list);
00126
IdLstFirst(list) = e;
00127 e->
Object = obj;
00128 }
00129
break;
00130
case LST_END:
00131
if (mode & (
LST_NEXT |
LST_BIDIR)) {
00132
00133 p =
IdLstFirst(list);
00134
if (p) {
00135
while (p->
Next)
00136 p = p->
Next;
00137 p->
Next = e;
00138
if (mode &
LST_BIDIR)
00139 e->
Pred = p;
00140 }
else
00141
IdLstFirst(list) = e;
00142 e->
Object = obj;
00143
IdLstLast(list) = e;
00144 }
00145
if (mode &
LST_PRED) {
00146 p =
IdLstFirst(list);
00147
if (p) {
00148
while (p->
Pred)
00149 p = p->
Pred;
00150 p->
Pred = e;
00151 }
else
00152
IdLstFirst(list) = e;
00153 e->
Object = obj;
00154 }
00155
break;
00156
case LST_ELEM:
00157
if (mode & (
LST_NEXT |
LST_BIDIR)) {
00158
if (elem) {
00159
if (elem ==
IdLstLast(list))
00160
IdLstLast(list) = e;
00161 e->
Next = elem->
Next;
00162 elem->
Next = e;
00163
if (mode &
LST_BIDIR) {
00164 e->
Pred = elem;
00165
if (e->
Next)
00166 e->
Next->
Pred = e;
00167 }
00168 }
00169 e->
Object = obj;
00170 }
00171
if (mode &
LST_PRED) {
00172
if (elem) {
00173 e->
Pred = elem->
Pred;
00174 elem->
Pred = e;
00175 }
00176 e->
Object = obj;
00177 }
00178
break;
00179
00180
case LST_ELEM_AV:
00181
if (mode & (
LST_NEXT |
LST_BIDIR)) {
00182
if (elem) {
00183
if (elem ==
IdLstFirst(list))
00184
IdLstFirst(list) = e;
00185 e->
Pred = elem->
Pred;
00186 elem->
Pred = e;
00187
if (mode &
LST_BIDIR) {
00188 e->
Next = elem;
00189
if (e->
Pred)
00190 e->
Pred->
Next = e;
00191 }
00192 }
00193 e->
Object = obj;
00194 }
00195
if (mode &
LST_PRED) {
00196
if (elem) {
00197 e->
Next = elem->
Next;
00198 elem->
Next = e;
00199 }
00200 e->
Object = obj;
00201 }
00202
break;
00203 }
00204
return 1;
00205 }
00206
00213
int
00214 IdLstRemoveFirst(
PLIST l)
00215 {
00216
PLIST_ELEMENT e;
00217
if (
IdLstNbElems(l) == 0) {
00218
00219
00220
00221
IdErrno =
IDERR_EMPTY_LIST;
00222
return (0);
00223 }
00224 (
_IdLstPrivate(l)->NbElems)--;
00225
00226 e =
IdLstFirst(l);
00227
IdLstFirst(l) =
IdLstNext(e);
00228
00229
if (
IdLstNext(e) == NULL)
00230
00231
00232
00233
IdLstLast(l) = NULL;
00234
else
00235
IdLstPrevious(
IdLstNext(e)) = NULL;
00236
00237
00238
00239
00240 free(e);
00241
return (1);
00242 }
00243
00250
int
00251 IdLstRemoveLast(
PLIST l)
00252 {
00253
PLIST_ELEMENT e;
00254
if (
IdLstNbElems(l) == 0) {
00255
00256
00257
00258
IdErrno =
IDERR_EMPTY_LIST;
00259
return (0);
00260 }
00261 (
_IdLstPrivate(l)->NbElems)--;
00262
00263 e =
IdLstLast(l);
00264
IdLstLast(l) =
IdLstPrevious(e);
00265
00266
if (
IdLstPrevious(e) == NULL)
00267
00268
00269
00270
IdLstFirst(l) = NULL;
00271
else
00272
IdLstNext(
IdLstPrevious(e)) = NULL;
00273
00274
00275
00276
00277 free(e);
00278
return (1);
00279 }
00280
00288
int
00289 IdLstRemoveElem(
PLIST l,
PLIST_ELEMENT e)
00290 {
00291
PLIST_ELEMENT pr, sv;
00292
00293
if (
IdLstNbElems(l) == 0) {
00294
00295
00296
00297
IdErrno =
IDERR_EMPTY_LIST;
00298
return (0);
00299 }
00300 pr =
IdLstPrevious(e);
00301 sv =
IdLstNext(e);
00302
00303
if (pr == NULL)
00304
IdLstRemoveFirst(l);
00305
00306
00307
00308
else if (sv == NULL)
00309
IdLstRemoveLast(l);
00310
00311
00312
00313
00314
else {
00315
00316
00317
00318
IdLstNext(pr) =
IdLstNext(e);
00319
IdLstPrevious(sv) =
IdLstPrevious(e);
00320 (
_IdLstPrivate(l)->NbElems)--;
00321
00322
00323
00324
00325 free(e);
00326 }
00327
return (1);
00328 }
00329
00339
int
00340 LstAddAfter(
PLIST l,
void *pobj,
PLIST_ELEMENT e)
00341 {
00342
00343
PLIST_ELEMENT elem;
00344
00345
if (e ==
IdLstLast(l))
00346
IdLstAddLast(l, pobj);
00347
else {
00348
00349
00350
00351 elem = calloc(1,
sizeof(
LIST_ELEMENT));
00352
if (!elem) {
00353
IdErrPrintf(
"(Erreur allocation nouvel element\n");
00354
IdErrno =
IDERR_ALLOC_ELEM;
00355
return 0;
00356 }
00357 (
_IdLstPrivate(l)->NbElems)++;
00358 elem->
Numero =
IdLstNbElems(l);
00359 elem->
Object = pobj;
00360
IdLstNext(elem) =
IdLstNext(e);
00361
IdLstPrevious(elem) = e;
00362
IdLstPrevious(
IdLstNext(e)) = elem;
00363
IdLstNext(e) = elem;
00364 }
00365
return 1;
00366 }
00367
00377
int
00378 LstAddBefore(
PLIST l,
void *pobj,
PLIST_ELEMENT e)
00379 {
00380
00381
PLIST_ELEMENT elem;
00382
00383
if (e ==
IdLstFirst(l))
00384
IdLstAddFirst(l, pobj);
00385
else {
00386
00387
00388
00389 elem = calloc(1,
sizeof(
LIST_ELEMENT));
00390
if (!elem) {
00391
IdErrPrintf(
"Erreur allocation nouvel element\n");
00392
IdErrno =
IDERR_ALLOC_ELEM;
00393
return 0;
00394 }
00395 (
_IdLstPrivate(l)->NbElems)++;
00396
00397 elem->
Numero =
IdLstNbElems(l);
00398 elem->
Object = pobj;
00399
IdLstNext(elem) = e;
00400
IdLstPrevious(elem) =
IdLstPrevious(e);
00401
IdLstNext(
IdLstPrevious(e)) = elem;
00402
IdLstPrevious(e) = elem;
00403 }
00404
return 1;
00405 }