1 /*
2 * Copyright (c) 2003 Matthijs Hollemans
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "rdef.h"
29 #include "private.h"
30
31 ptr_list_t include_dirs;
32 ptr_list_t input_files;
33
34 uint32 flags = 0;
35
36 status_t rdef_err;
37 int32 rdef_err_line;
38 char rdef_err_file[B_PATH_NAME_LENGTH];
39 char rdef_err_msg[1024];
40
41
42 void
free_ptr_list(ptr_list_t & list)43 free_ptr_list(ptr_list_t &list)
44 {
45 for (ptr_iter_t i = list.begin(); i != list.end(); ++i) {
46 free(*i);
47 }
48
49 list.clear();
50 }
51
52
53 int32
rdef_get_version()54 rdef_get_version()
55 {
56 return 2;
57 }
58
59
60 status_t
rdef_add_include_dir(const char * dir,bool toEndOfList)61 rdef_add_include_dir(const char *dir, bool toEndOfList)
62 {
63 clear_error();
64
65 char *path = (char *)malloc(strlen(dir) + 2);
66 if (path == NULL) {
67 rdef_err = B_NO_MEMORY;
68 return B_NO_MEMORY;
69 }
70
71 strcpy(path, dir);
72 strcat(path, "/");
73
74 if (toEndOfList)
75 include_dirs.push_back(path);
76 else
77 include_dirs.push_front(path);
78
79 return B_OK;
80 }
81
82
83 status_t
rdef_remove_include_dir(const char * dir)84 rdef_remove_include_dir(const char *dir)
85 {
86 size_t length = strlen(dir);
87 bool noSlash = false;
88 if (dir[length - 1] != '/')
89 noSlash = true;
90
91 for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
92 char *path = (char *)*i;
93
94 if (!strncmp(dir, path, length)
95 && path[length + (noSlash ? 1 : 0)] == '\0') {
96 // we found the entry in the list, let's remove it
97 include_dirs.erase(i);
98 free(path);
99 return B_OK;
100 }
101 }
102
103 return B_ENTRY_NOT_FOUND;
104 }
105
106
107 void
rdef_free_include_dirs()108 rdef_free_include_dirs()
109 {
110 free_ptr_list(include_dirs);
111 }
112
113
114 status_t
rdef_add_input_file(const char * file)115 rdef_add_input_file(const char *file)
116 {
117 clear_error();
118
119 char *temp = strdup(file);
120 if (temp == NULL) {
121 rdef_err = B_NO_MEMORY;
122 return B_NO_MEMORY;
123 }
124
125 input_files.push_back(temp);
126 return B_OK;
127 }
128
129
130 void
rdef_free_input_files()131 rdef_free_input_files()
132 {
133 free_ptr_list(input_files);
134 }
135
136
137 void
rdef_set_flags(uint32 flags_)138 rdef_set_flags(uint32 flags_)
139 {
140 flags = flags_;
141 }
142
143
144 void
rdef_clear_flags()145 rdef_clear_flags()
146 {
147 flags = 0;
148 }
149
150
151 void
clear_error()152 clear_error()
153 {
154 rdef_err = B_OK;
155 rdef_err_line = 0;
156 rdef_err_file[0] = '\0';
157 rdef_err_msg[0] = '\0';
158 }
159
160
161 bool
open_file_from_include_dir(const char * filename,char * outname)162 open_file_from_include_dir(const char *filename, char *outname)
163 {
164 for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
165 char tmpname[B_PATH_NAME_LENGTH];
166 strcpy(tmpname, (char *)*i);
167 strcat(tmpname, filename);
168
169 if (access(tmpname, R_OK) == 0) {
170 strcpy(outname, tmpname);
171 return true;
172 }
173 }
174
175 return false;
176 }
177
178