Integrate cctools-822 changes
[striptease.git] / libstuff / symbol_list.c
blob613bc1a0da1f5a0534fd2e256ce3409c83884e7a
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #ifndef RLD
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <ctype.h>
30 #include <stuff/symbol_list.h>
31 #include <stuff/allocate.h>
32 #include <stuff/errors.h>
34 static int cmp_qsort_name(
35 const struct symbol_list *sym1,
36 const struct symbol_list *sym2);
39 * This is called to setup a symbol list from a file. It reads the file with
40 * the strings in it and places them in an array of symbol_list structures and
41 * then sorts them by name.
43 * The file that contains the symbol names must have symbol names one per line,
44 * leading and trailing white space is removed and lines starting with a '#'
45 * and lines with only white space are ignored.
47 __private_extern__
48 void
49 setup_symbol_list(
50 char *file,
51 struct symbol_list **list,
52 uint32_t *size)
54 int fd;
55 uint32_t i, j, len, strings_size;
56 struct stat stat_buf;
57 char *strings, *p, *line;
59 if((fd = open(file, O_RDONLY)) < 0){
60 system_error("can't open: %s", file);
61 return;
63 if(fstat(fd, &stat_buf) == -1){
64 system_error("can't stat: %s", file);
65 close(fd);
66 return;
68 strings_size = stat_buf.st_size;
69 strings = (char *)allocate(strings_size + 2);
70 strings[strings_size] = '\n';
71 strings[strings_size + 1] = '\0';
72 if(read(fd, strings, strings_size) != (int)strings_size){
73 system_error("can't read: %s", file);
74 close(fd);
75 return;
78 * Change the newlines to '\0' and count the number of lines with
79 * symbol names. Lines starting with '#' are comments and lines
80 * contain all space characters do not contain symbol names.
82 p = strings;
83 line = p;
84 for(i = 0; i < strings_size + 1; i++){
85 if(*p == '\n' || *p == '\r'){
86 *p = '\0';
87 if(*line != '#'){
88 while(*line != '\0' && isspace(*line))
89 line++;
90 if(*line != '\0')
91 (*size)++;
93 p++;
94 line = p;
96 else{
97 p++;
100 *list = (struct symbol_list *)
101 allocate((*size) * sizeof(struct symbol_list));
104 * Place the strings in the list trimming leading and trailing spaces
105 * from the lines with symbol names.
107 p = strings;
108 line = p;
109 for(i = 0; i < (*size); ){
110 p += strlen(p) + 1;
111 if(*line != '#' && *line != '\0'){
112 while(*line != '\0' && isspace(*line))
113 line++;
114 if(*line != '\0'){
115 (*list)[i].name = line;
116 (*list)[i].seen = FALSE;
117 i++;
118 len = strlen(line);
119 j = len - 1;
120 while(j > 0 && isspace(line[j])){
121 j--;
123 if(j > 0 && j + 1 < len && isspace(line[j+1]))
124 line[j+1] = '\0';
127 line = p;
130 qsort(*list, *size, sizeof(struct symbol_list),
131 (int (*)(const void *, const void *))cmp_qsort_name);
133 /* remove duplicates on the list */
134 for(i = 0; i < (*size); i++){
135 if(i + 1 < (*size)){
136 if(strcmp((*list)[i].name, (*list)[i+1].name) == 0){
137 for(j = 1; j < ((*size) - i - 1); j++){
138 (*list)[i + j].name = (*list)[i + j + 1].name;
140 *size = *size - 1;
142 * Since there may be more than two of the same name
143 * check this one again against the next one in the
144 * list before moving on.
146 i--;
151 #ifdef DEBUG
152 printf("symbol list:\n");
153 for(i = 0; i < (*size); i++){
154 printf("0x%x name = %s\n", &((*list)[i]),(*list)[i].name);
156 #endif /* DEBUG */
160 * Function for qsort for comparing symbol list names.
162 static
164 cmp_qsort_name(
165 const struct symbol_list *sym1,
166 const struct symbol_list *sym2)
168 return(strcmp(sym1->name, sym2->name));
172 * Function for bsearch for finding a symbol name.
174 __private_extern__
176 symbol_list_bsearch(
177 const char *name,
178 const struct symbol_list *sym)
180 return(strcmp(name, sym->name));
182 #endif /* !defined(RLD) */