list.c revision ea6ae205
1/* 2 Copyright (c) 2002-2003 by Juliusz Chroboczek 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21*/ 22/* $XFree86: xc/programs/mkfontscale/list.c,v 1.5 2003/07/04 16:24:30 eich Exp $ */ 23 24#include <stdlib.h> 25#include <stdio.h> 26#include <stdarg.h> 27#include <string.h> 28#include "list.h" 29 30#ifdef NEED_SNPRINTF 31#undef SCOPE 32#define SCOPE static 33#include "snprintf.c" 34#endif 35 36int 37listMember(char *elt, ListPtr list) 38{ 39 while(list != NULL) { 40 if(strcmp(elt, list->value) == 0) 41 return 1; 42 list = list->next; 43 } 44 return 0; 45} 46 47ListPtr 48listCons(char *car, ListPtr cdr) 49{ 50 ListPtr lcar = malloc(sizeof(ListRec)); 51 if(!lcar) 52 return NULL; 53 lcar -> value = car; 54 lcar -> next = cdr; 55 return lcar; 56} 57 58ListPtr 59listAdjoin(char *car, ListPtr cdr) 60{ 61 if(listMember(car, cdr)) { 62 free(car); 63 return cdr; 64 } 65 return listCons(car, cdr); 66} 67 68char * 69dsprintf(char *f, ...) 70{ 71 va_list args; 72 char *string; 73 { 74 int n, size = 20; 75 while(1) { 76 if(size > 4096) 77 return NULL; 78 string = malloc(size); 79 if(!string) 80 return NULL; 81 va_start(args, f); 82 n = vsnprintf(string, size, f, args); 83 va_end(args); 84 if(n >= 0 && n < size) 85 return string; 86 else if(n >= size) 87 size = n + 1; 88 else 89 size = size * 3 / 2 + 1; 90 free(string); 91 } 92 } 93} 94 95 96ListPtr 97listConsF(ListPtr cdr, char *f, ...) 98{ 99 va_list args; 100 char *string; 101 { 102 int n, size = 20; 103 while(1) { 104 if(size > 4096) 105 return NULL; 106 string = malloc(size); 107 if(!string) 108 return NULL; 109 va_start(args, f); 110 n = vsnprintf(string, size, f, args); 111 va_end(args); 112 if(n >= 0 && n < size) 113 return listCons(string, cdr); 114 else if(n >= size) 115 size = n + 1; 116 else 117 size = size * 3 / 2 + 1; 118 free(string); 119 } 120 } 121} 122 123ListPtr 124listAdjoinF(ListPtr cdr, char *f, ...) 125{ 126 va_list args; 127 char *string; 128 { 129 int n, size = 20; 130 while(1) { 131 if(size > 4096) 132 return NULL; 133 string = malloc(size); 134 if(!string) 135 return NULL; 136 va_start(args, f); 137 n = vsnprintf(string, size, f, args); 138 va_end(args); 139 if(n >= 0 && n < size) 140 return listAdjoin(string, cdr); 141 else if(n >= size) 142 size = n + 1; 143 else 144 size = size * 3 / 2 + 1; 145 free(string); 146 } 147 } 148} 149 150int 151listLength(ListPtr list) 152{ 153 int n = 0; 154 while(list) { 155 n++; 156 list = list->next; 157 } 158 return n; 159} 160 161ListPtr 162appendList(ListPtr first, ListPtr second) 163{ 164 ListPtr current; 165 166 if(second == NULL) 167 return first; 168 169 if(first == NULL) 170 return second; 171 172 for(current = first; current->next; current = current->next) 173 ; 174 175 current->next = second; 176 return first; 177} 178 179ListPtr 180makeList(char **a, int n, ListPtr old, int begin) 181{ 182 ListPtr first, current, next; 183 int i; 184 185 if(n == 0) 186 return old; 187 188 first = malloc(sizeof(ListRec)); 189 if(!first) 190 return NULL; 191 192 first->value = a[0]; 193 first->next = NULL; 194 195 current = first; 196 for(i = 1; i < n; i++) { 197 next = malloc(sizeof(ListRec)); 198 if(!next) 199 return NULL; 200 next->value = a[i]; 201 next->next = NULL; 202 203 current->next = next; 204 current = next; 205 } 206 if(begin) { 207 current->next = old; 208 return first; 209 } else { 210 return appendList(old, first); 211 } 212} 213 214ListPtr 215reverseList(ListPtr old) 216{ 217 ListPtr new = NULL, current; 218 while(old) { 219 current = old; 220 old = old->next; 221 current->next = new; 222 new = current; 223 } 224 return new; 225} 226 227void 228destroyList(ListPtr old) 229{ 230 ListPtr next; 231 if(!old) 232 return; 233 while(old) { 234 next = old->next; 235 free(old); 236 old = next; 237 } 238} 239 240void 241deepDestroyList(ListPtr old) 242{ 243 ListPtr next; 244 if(!old) 245 return; 246 while(old) { 247 next = old->next; 248 free(old->value); 249 free(old); 250 old = next; 251 } 252} 253