Changes in uspace/srv/devman/match.c [9d58539:aed3e6a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/devman/match.c
r9d58539 raed3e6a 31 31 */ 32 32 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <io/log.h> 33 36 #include <str.h> 37 #include <str_error.h> 38 #include <sys/types.h> 39 #include <sys/stat.h> 34 40 35 41 #include "devman.h" 42 #include "match.h" 43 44 #define COMMENT '#' 36 45 37 46 /** Compute compound score of driver and device. … … 93 102 } 94 103 104 /** Read match id at the specified position of a string and set the position in 105 * the string to the first character following the id. 106 * 107 * @param buf The position in the input string. 108 * @return The match id. 109 */ 110 char *read_match_id(char **buf) 111 { 112 char *res = NULL; 113 size_t len = get_nonspace_len(*buf); 114 115 if (len > 0) { 116 res = malloc(len + 1); 117 if (res != NULL) { 118 str_ncpy(res, len + 1, *buf, len); 119 *buf += len; 120 } 121 } 122 123 return res; 124 } 125 126 /** 127 * Read match ids and associated match scores from a string. 128 * 129 * Each match score in the string is followed by its match id. 130 * The match ids and match scores are separated by whitespaces. 131 * Neither match ids nor match scores can contain whitespaces. 132 * 133 * @param buf The string from which the match ids are read. 134 * @param ids The list of match ids into which the match ids and 135 * scores are added. 136 * @return True if at least one match id and associated match score 137 * was successfully read, false otherwise. 138 */ 139 bool parse_match_ids(char *buf, match_id_list_t *ids) 140 { 141 int score = 0; 142 char *id = NULL; 143 int ids_read = 0; 144 145 while (true) { 146 /* skip spaces */ 147 if (!skip_spaces(&buf)) 148 break; 149 150 if (*buf == COMMENT) { 151 skip_line(&buf); 152 continue; 153 } 154 155 /* read score */ 156 score = strtoul(buf, &buf, 10); 157 158 /* skip spaces */ 159 if (!skip_spaces(&buf)) 160 break; 161 162 /* read id */ 163 id = read_match_id(&buf); 164 if (NULL == id) 165 break; 166 167 /* create new match_id structure */ 168 match_id_t *mid = create_match_id(); 169 mid->id = id; 170 mid->score = score; 171 172 /* add it to the list */ 173 add_match_id(ids, mid); 174 175 ids_read++; 176 } 177 178 return ids_read > 0; 179 } 180 181 /** 182 * Read match ids and associated match scores from a file. 183 * 184 * Each match score in the file is followed by its match id. 185 * The match ids and match scores are separated by whitespaces. 186 * Neither match ids nor match scores can contain whitespaces. 187 * 188 * @param buf The path to the file from which the match ids are read. 189 * @param ids The list of match ids into which the match ids and 190 * scores are added. 191 * @return True if at least one match id and associated match score 192 * was successfully read, false otherwise. 193 */ 194 bool read_match_ids(const char *conf_path, match_id_list_t *ids) 195 { 196 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_match_ids(conf_path=\"%s\")", conf_path); 197 198 bool suc = false; 199 char *buf = NULL; 200 bool opened = false; 201 int fd; 202 size_t len = 0; 203 204 fd = open(conf_path, O_RDONLY); 205 if (fd < 0) { 206 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to open `%s' for reading: %s.", 207 conf_path, str_error(errno)); 208 goto cleanup; 209 } 210 opened = true; 211 212 len = lseek(fd, 0, SEEK_END); 213 lseek(fd, 0, SEEK_SET); 214 if (len == 0) { 215 log_msg(LOG_DEFAULT, LVL_ERROR, "Configuration file '%s' is empty.", 216 conf_path); 217 goto cleanup; 218 } 219 220 buf = malloc(len + 1); 221 if (buf == NULL) { 222 log_msg(LOG_DEFAULT, LVL_ERROR, "Memory allocation failed when parsing file " 223 "'%s'.", conf_path); 224 goto cleanup; 225 } 226 227 ssize_t read_bytes = read(fd, buf, len); 228 if (read_bytes <= 0) { 229 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to read file '%s' (%d).", conf_path, 230 errno); 231 goto cleanup; 232 } 233 buf[read_bytes] = 0; 234 235 suc = parse_match_ids(buf, ids); 236 237 cleanup: 238 free(buf); 239 240 if (opened) 241 close(fd); 242 243 return suc; 244 } 245 95 246 /** @} 96 247 */
Note:
See TracChangeset
for help on using the changeset viewer.