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