Changeset 233a3a06 in mainline for uspace/app/contacts/contacts.c
- Timestamp:
- 2018-08-02T12:03:39Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 42964a7
- Parents:
- 7b87e1d
- git-author:
- Jiri Svoboda <jiri@…> (2018-08-01 18:03:17)
- git-committer:
- Jiri Svoboda <jiri@…> (2018-08-02 12:03:39)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/contacts/contacts.c
r7b87e1d r233a3a06 58 58 /** Contact entry */ 59 59 typedef struct { 60 /** Link to contacts_t.entries */ 60 /** Containing contacts */ 61 contacts_t *contacts; 62 /** Link to contacts->entries */ 61 63 link_t lentries; 64 /** SIF node for this entry */ 65 sif_node_t *nentry; 66 /** Contact name */ 67 char *name; 62 68 } contacts_entry_t; 63 69 … … 71 77 ac_exit 72 78 } contact_action_t; 79 80 static errno_t contacts_unmarshal(sif_node_t *, contacts_t *); 73 81 74 82 /** Open contacts repo or create it if it does not exist. … … 139 147 } 140 148 149 rc = contacts_unmarshal(node, contacts); 150 if (rc != EOK) 151 goto error; 152 141 153 contacts->nentries = node; 142 154 } … … 156 168 } 157 169 170 /** Unmarshal contact entries from SIF repository. 171 * 172 * @param nentries Entries node 173 * @param contacts Contacts object to unmarshal to 174 * @return EOK on success or error code 175 */ 176 static errno_t contacts_unmarshal(sif_node_t *nentries, contacts_t *contacts) 177 { 178 sif_node_t *nentry; 179 contacts_entry_t *entry; 180 const char *name; 181 182 contacts->nentries = nentries; 183 184 nentry = sif_node_first_child(nentries); 185 while (nentry != NULL) { 186 if (str_cmp(sif_node_get_type(nentry), "entry") != 0) 187 return EIO; 188 189 entry = calloc(1, sizeof(contacts_entry_t)); 190 if (entry == NULL) 191 return ENOMEM; 192 193 name = sif_node_get_attr(nentry, "name"); 194 if (name == NULL) { 195 free(entry); 196 return EIO; 197 } 198 199 entry->name = str_dup(name); 200 if (entry->name == NULL) { 201 free(entry); 202 return ENOMEM; 203 } 204 205 entry->contacts = contacts; 206 entry->nentry = nentry; 207 list_append(&entry->lentries, &contacts->entries); 208 209 nentry = sif_node_next_child(nentry); 210 } 211 212 return EOK; 213 } 214 215 158 216 /** Interaction to create new contact. 159 217 * … … 165 223 sif_trans_t *trans = NULL; 166 224 sif_node_t *nentry; 225 contacts_entry_t *entry = NULL; 167 226 errno_t rc; 168 227 char *cname = NULL; … … 182 241 goto error; 183 242 243 entry = calloc(1, sizeof(contacts_entry_t)); 244 if (entry == NULL) { 245 rc = ENOMEM; 246 goto error; 247 } 248 184 249 rc = sif_trans_begin(contacts->repo, &trans); 185 250 if (rc != EOK) … … 199 264 200 265 trans = NULL; 201 202 free(cname); 266 entry->contacts = contacts; 267 entry->nentry = nentry; 268 entry->name = cname; 269 list_append(&entry->lentries, &contacts->entries); 270 203 271 tinput_destroy(tinput); 204 272 return EOK; … … 208 276 if (cname != NULL) 209 277 free(cname); 278 if (entry != NULL) 279 free(entry); 210 280 return rc; 211 281 } … … 229 299 sif_close(contacts->repo); 230 300 free(contacts); 301 } 302 303 /** Get first contacts entry. 304 * 305 * @param contacts Contacts 306 * @return First entry or @c NULL if there is none 307 */ 308 static contacts_entry_t *contacts_first(contacts_t *contacts) 309 { 310 link_t *link; 311 312 link = list_first(&contacts->entries); 313 if (link == NULL) 314 return NULL; 315 316 return list_get_instance(link, contacts_entry_t, lentries); 317 } 318 319 /** Get next contacts entry. 320 * 321 * @param cur Current entry 322 * @return Next entry or @c NULL if there is none 323 */ 324 static contacts_entry_t *contacts_next(contacts_entry_t *cur) 325 { 326 link_t *link; 327 328 link = list_next(&cur->lentries, &cur->contacts->entries); 329 if (link == NULL) 330 return NULL; 331 332 return list_get_instance(link, contacts_entry_t, lentries); 333 } 334 335 /** List all contacts. 336 * 337 * @param contacts Contacts 338 */ 339 static void contacts_list_all(contacts_t *contacts) 340 { 341 contacts_entry_t *entry; 342 343 entry = contacts_first(contacts); 344 while (entry != NULL) { 345 printf(" * %s\n", entry->name); 346 entry = contacts_next(entry); 347 } 231 348 } 232 349 … … 282 399 283 400 while (!quit) { 401 contacts_list_all(contacts); 402 284 403 rc = nchoice_get(choice, &sel); 285 404 if (rc != EOK) {
Note:
See TracChangeset
for help on using the changeset viewer.