]>
Commit | Line | Data |
---|---|---|
f67e2651 | 1 | |
2 | /* | |
3 | * functions for the array_hdef structure | |
4 | */ | |
5 | ||
6 | #include <stdlib.h> | |
7 | #include <string.h> | |
8 | ||
9 | #include "rdmc.h" | |
10 | ||
11 | static int rdmc_exists_hdef_tag(const array_hdef_t *p, | |
12 | int ndef, const char *tag); | |
13 | ||
14 | ||
15 | ||
16 | /* Get a unique id for a new defpar object */ | |
17 | int rdmc_unique_hdef_id(const array_hdef_t *p, int ndef) | |
18 | { | |
19 | int id; | |
20 | for (id = ndef; 1; id++) | |
21 | if (rdmc_get_hdef_id(p, ndef, id) == RDMC_NA ) | |
22 | return (id); | |
23 | } | |
24 | ||
25 | ||
26 | /* get a unique tag for a new defpar object. It is the tag_root itself | |
27 | * as long it is unique, or the tag_root with a number at the end */ | |
28 | int rdmc_unique_hdef_tag(char *tag, const array_hdef_t *p | |
29 | , int ndef, const char *tag_root){ | |
30 | int i; | |
31 | ||
32 | /* First check if we can use the orginal tag */ | |
33 | if (!rdmc_exists_hdef_tag(p, ndef, tag_root)) { | |
34 | strcpy(tag, tag_root); | |
35 | return 0; | |
36 | } | |
37 | /* now try to construct a new one with "Tag" + "number" */ | |
38 | for (i = 1; 1; i++) { | |
39 | sprintf(tag, "%s%i", tag_root, i); | |
40 | if (!rdmc_exists_hdef_tag(p, ndef, tag)) | |
41 | return 0; | |
42 | } | |
43 | } | |
44 | ||
45 | ||
46 | static int rdmc_exists_hdef_tag(const array_hdef_t *p, int ndef, const char *tag){ | |
47 | int i; | |
48 | ||
49 | for (i = 0; i < ndef; i++){ | |
50 | if (rdmc_get_hdef_tag(p, ndef, tag) != RDMC_NA) { | |
51 | return 1; | |
52 | } | |
53 | } | |
54 | return 0; | |
55 | } | |
56 | ||
57 | /* add a new initilized header def */ | |
58 | int rdmc_add_array_hdef(array_hdef_t **list, | |
59 | int *count, array_hdef_t *new, int ipos){ | |
60 | int i; | |
61 | ||
62 | if( ( list == NULL ) | |
63 | || (new == NULL ) | |
64 | || (count == NULL ) | |
65 | || (*count < 0) | |
66 | || (ipos < 0) | |
67 | || (ipos > *count) | |
68 | ) | |
69 | return 1; | |
70 | ||
71 | (*count)++; | |
72 | *list = (array_hdef_t *) | |
73 | realloc(*list,sizeof(array_hdef_t)*(*count)); | |
74 | ||
75 | for (i = (*count - 1) ; i > ipos ; i-- ){ | |
76 | rdmc_cp_hdef( &((*list)[i]), &((*list)[i-1]) ); | |
77 | } | |
78 | rdmc_cp_hdef( &((*list)[ipos]), new ); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | ||
84 | /* remove header def */ | |
85 | int rdmc_del_array_hdef(array_hdef_t **list, | |
86 | int *count, int ipos){ | |
87 | int i; | |
88 | if( (list == NULL ) | |
89 | || (*list == NULL ) | |
90 | || (*count <= 0) | |
91 | || (ipos < 0) | |
92 | || (ipos >= *count) | |
93 | ) | |
94 | return 1; | |
95 | for (i = ipos ; i < (*count-1) ; i++ ){ | |
96 | rdmc_cp_hdef( &((*list)[i]), &((*list)[i+1]) ); | |
97 | } | |
98 | (*count)--; | |
99 | if (*count > 0) | |
100 | *list = (array_hdef_t *) | |
101 | realloc(*list,sizeof(array_hdef_t)*(*count)); | |
102 | else { | |
103 | free(*list); | |
104 | *list=NULL; | |
105 | } | |
106 | return 0; | |
107 | } | |
108 | ||
109 | ||
110 | ||
111 | void rdmc_init_array_hdef(array_hdef_t *def){/*init the structures */ | |
112 | int i; | |
113 | def->id = RDMC_NA; | |
114 | strcpy(def->tag,"unknown"); | |
115 | def->nwords=0; | |
116 | for (i =0 ; i < RDMC_MAXTOKEN_PER_LINE ; i++){ | |
117 | def->words[i][0] = '\0'; | |
118 | } | |
119 | def->npars=0; | |
120 | for (i =0 ; i < RDMC_MAXTOKEN_PER_LINE ; i++){ | |
121 | def->pars[i][0] = '\0'; | |
122 | } | |
123 | } /* init_array_header_def */ | |
124 | ||
125 | void rdmc_clear_array_hdef(array_hdef_t *def){/*init the structures */ | |
126 | rdmc_free_array_hdef(def); | |
127 | rdmc_init_array_hdef(def); | |
128 | } /* clear_array_header_def */ | |
129 | ||
130 | void rdmc_free_array_hdef(array_hdef_t *def){/*init the structures */ | |
131 | } /* free_array_header_def */ | |
132 | ||
133 | ||
134 | void rdmc_cp_hdef(array_hdef_t *out, const array_hdef_t *in){ | |
135 | memcpy(out,in,sizeof(array_hdef_t)); | |
136 | } /* init_array_header_def */ | |
137 | ||
138 | /* search an array of ndef array_header_def_t elements */ | |
139 | /* find the number of a header definition */ | |
140 | /* returns the number 0..(ndef-1) or RDMC_NA */ | |
141 | int rdmc_get_hdef_tag(const array_hdef_t *hd, int ndef, const char *tag){ | |
142 | int i; | |
143 | for (i=0 ; i < ndef ; i++ ){ | |
144 | #if 0 | |
145 | rdmc_msgprintf("%s:%s",hd[i].tag,tag); | |
146 | #endif | |
147 | if (strcmp(hd[i].tag,tag) == 0) | |
148 | return i; | |
149 | } | |
150 | return RDMC_NA; | |
151 | } | |
152 | ||
153 | int rdmc_get_hdef_id(const array_hdef_t *hd, int ndef,int id){ /*according to an id */ | |
154 | int i; | |
155 | for (i=0 ; i < ndef ; i++ ){ | |
156 | if (hd[i].id == id) | |
157 | return i; | |
158 | } | |
159 | return RDMC_NA; | |
160 | } | |
161 | ||
162 | /* returns 1 if differetn */ | |
163 | int rdmc_comp_array_hdef(const array_hdef_t *d1, const array_hdef_t *d2){ | |
164 | int i; | |
165 | if(d1->id != d2->id) | |
166 | return 1; | |
167 | if(strcmp(d1->tag,d2->tag)) | |
168 | return 1; | |
169 | if(d1->nwords != d2->nwords) | |
170 | return 1; | |
171 | for ( i =0 ; i < d1->nwords ; i++){ | |
172 | if(strcmp(d1->words[i],d2->words[i])) | |
173 | return 1; | |
174 | } | |
175 | if(d1->npars != d2->npars) | |
176 | return 1; | |
177 | for ( i =0 ; i < d1->npars ; i++){ | |
178 | if(strcmp(d1->pars[i],d2->pars[i])) | |
179 | return 1; | |
180 | } | |
181 | return 0; | |
182 | } | |
183 | ||
184 | /* returns the index of the string token in the list of hdef parameters */ | |
185 | /* if the token is not found RDMC_NA is returned */ | |
186 | int rdmc_token_in_hdef(const array_hdef_t *p, const char *token){ | |
187 | register int i; | |
188 | for (i=0 ; i < p->nwords ; i++ ){ | |
189 | if (! strcmp(p->words[i],token) ) | |
190 | return i; | |
191 | } | |
192 | return RDMC_NA; | |
193 | } |