/[public]/psiconv/trunk/lib/psiconv/list.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2 Revision 63
1/* 1/*
2 list.c - Part of psiconv, a PSION 5 file formats converter 2 list.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
21#include "config.h" 21#include "config.h"
22#include <stddef.h> 22#include <stddef.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <stdio.h> 24#include <stdio.h>
25#include "list.h" 25#include "list.h"
26#include "error.h"
26 27
27static void psiconv_list_resize(psiconv_list l,unsigned int nr); 28static int psiconv_list_resize(psiconv_list l,unsigned int nr);
28 29
29struct psiconv_list { 30struct psiconv_list_s {
30 int cur_len; 31 int cur_len;
31 int max_len; 32 int max_len;
32 int el_size; 33 int el_size;
33 void *els; 34 void *els;
34}; 35};
35 36
36psiconv_list psiconv_list_new(int element_size) 37psiconv_list psiconv_list_new(int element_size)
37{ 38{
38 psiconv_list l; 39 psiconv_list l;
39 l = malloc(sizeof(*l)); 40 l = malloc(sizeof(*l));
41 if (!l)
42 return NULL;
40 l->cur_len = 0; 43 l->cur_len = 0;
41 l->max_len = 0; 44 l->max_len = 0;
42 l->el_size=element_size; 45 l->el_size=element_size;
43 l->els = NULL; 46 l->els = NULL;
44 return l; 47 return l;
75 return NULL; 78 return NULL;
76 else 79 else
77 return ((char *) (l->els)) + indx * l->el_size; 80 return ((char *) (l->els)) + indx * l->el_size;
78} 81}
79 82
80void psiconv_list_add(psiconv_list l, void *el) 83int psiconv_list_add(psiconv_list l, void *el)
81{ 84{
82 psiconv_list_resize(l,l->cur_len + 1); 85 if (psiconv_list_resize(l,l->cur_len + 1))
86 return -PSICONV_E_NOMEM;
83 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); 87 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
84 l->cur_len ++; 88 l->cur_len ++;
89 return 0;
85} 90}
86 91
87void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) 92void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
88{ 93{
89 int i; 94 int i;
94psiconv_list psiconv_list_clone(const psiconv_list l) 99psiconv_list psiconv_list_clone(const psiconv_list l)
95{ 100{
96 psiconv_list l2; 101 psiconv_list l2;
97 int i; 102 int i;
98 l2 = psiconv_list_new(l->el_size); 103 l2 = psiconv_list_new(l->el_size);
104 if (!l2)
105 return NULL;
99 for (i = 0; i < l->cur_len; i ++) 106 for (i = 0; i < l->cur_len; i ++)
100 psiconv_list_add(l2,psiconv_list_get(l,i)); 107 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
108 psiconv_list_free(l2);
109 return NULL;
110 }
101 return l2; 111 return l2;
102 112
103} 113}
104 114
105psiconv_list psiconv_list_clone_el(const psiconv_list l,void clone_el(void *el))
106{
107 psiconv_list l2 = psiconv_list_clone(l);
108 psiconv_list_foreach_el(l2,clone_el);
109 return l2;
110}
111 115
112size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) 116size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
113{ 117{
114 size_t res; 118 size_t res;
115 psiconv_list_resize(l,l->cur_len + size); 119 if (psiconv_list_resize(l,l->cur_len + size))
120 return 0;
116 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); 121 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f);
117 l->cur_len += res; 122 l->cur_len += res;
118 return res; 123 return res;
119} 124}
120 125
121void psiconv_list_resize(psiconv_list l,unsigned int nr) 126int psiconv_list_resize(psiconv_list l,unsigned int nr)
122{ 127{
128 void * temp;
123 if (nr > l->max_len) { 129 if (nr > l->max_len) {
124 l->max_len = 1.1 * nr; 130 l->max_len = 1.1 * nr;
125 l->max_len += 16 - l->max_len % 16; 131 l->max_len += 16 - l->max_len % 16;
126 l->els = realloc(l->els,l->max_len * l->el_size); 132 temp = realloc(l->els,l->max_len * l->el_size);
133 if (temp) {
134 l->els = temp;
135 return 0;
136 } else
137 return -PSICONV_E_NOMEM;
127 } 138 }
128} 139}
129 140

Legend:
Removed from v.2  
changed lines
  Added in v.63

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26