this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implement the Import feature in PlistBuddy, the program is now feature complete

+139 -9
+139 -9
src/PlistBuddy/PlistBuddy.c
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2017 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 1 20 #include <stdlib.h> 2 21 #include <stdio.h> 3 22 #include <CoreFoundation/CoreFoundation.h> ··· 21 40 void deleteEntry(const char* entry); 22 41 void copyEntry(const char* src, const char* dst); 23 42 void mergeFile(const char* path, const char* entry); 43 + void importFile(const char* entry, const char* path); 24 44 25 45 // Forces XML output when printing to screen 26 46 bool forceXML = false; ··· 332 352 333 353 char* getWord(const char* cmd, const char** next) 334 354 { 335 - char* p = strchr(cmd, ' '); 336 - if (p == NULL) 355 + if (cmd[0] == '"') 337 356 { 357 + int i = 1, j = 0; 358 + char* out = strdup(cmd); 359 + 360 + for (; cmd[i] != '\0'; i++) 361 + { 362 + if (cmd[i] == '\\' && cmd[i+1] != '\0') 363 + { 364 + i++; 365 + switch (cmd[i]) 366 + { 367 + case '"': 368 + out[j++] = '"'; 369 + break; 370 + case 'n': 371 + out[j++] = '\n'; 372 + break; 373 + case 't': 374 + out[j++] = '\t'; 375 + break; 376 + default: 377 + out[j++] = '\\'; 378 + out[j++] = cmd[i]; 379 + } 380 + } 381 + else if (cmd[i] == '"') 382 + { 383 + out[j++] = '\0'; 384 + break; 385 + } 386 + else 387 + { 388 + out[j++] = cmd[i]; 389 + } 390 + } 391 + 392 + if (j > 0 && out[j-1] != '\0') 393 + { 394 + free(out); 395 + puts("Unterminated Quotes"); 396 + return NULL; 397 + } 338 398 if (next) 339 - *next = NULL; 340 - return strdup(cmd); 399 + *next = nextWord(cmd + i + 1); 400 + 401 + return out; 341 402 } 342 403 else 343 404 { 344 - char* rv = strndup(cmd, p-cmd); 345 - if (next) 346 - *next = nextWord(p); 347 - return rv; 405 + char* p = strchr(cmd, ' '); 406 + if (p == NULL) 407 + { 408 + if (next) 409 + *next = NULL; 410 + return strdup(cmd); 411 + } 412 + else 413 + { 414 + char* rv = strndup(cmd, p-cmd); 415 + if (next) 416 + *next = nextWord(p); 417 + return rv; 418 + } 348 419 } 349 420 } 350 421 ··· 479 550 return; 480 551 } 481 552 482 - // FIXME: This isn't quite correct, this doesn't support paths with spaces 483 553 char* path = getWord(next, &next); 484 554 485 555 mergeFile(path, next); ··· 488 558 else if (isCommand(cmd, "Import", &next)) 489 559 { 490 560 // entry file.plist 561 + if (!next) 562 + { 563 + puts("Missing arguments"); 564 + return; 565 + } 566 + char* entry = getWord(next, &next); 567 + 568 + if (!next) 569 + { 570 + puts("Missing arguments"); 571 + free(entry); 572 + return; 573 + } 574 + char* path = getWord(next, &next); 575 + 576 + importFile(entry, path); 577 + 578 + free(path); 579 + free(entry); 491 580 } 492 581 else 493 582 { ··· 1153 1242 CFRelease(fileContents); 1154 1243 } 1155 1244 1245 + // NOTE: This function doesn't seem to work at all in the original program 1246 + void importFile(const char* entry, const char* path) 1247 + { 1248 + CFPropertyListRef dest, fileContents; 1249 + char* leafName; 1250 + 1251 + fileContents = loadPlist(path); 1252 + if (fileContents == NULL) 1253 + return; 1254 + 1255 + resolvePlistEntry(entry, &dest, NULL, &leafName, true); 1256 + if (dest == NULL) 1257 + { 1258 + printf("Import: Entry, \"%s\", Does Not Exist\n", entry); 1259 + CFRelease(fileContents); 1260 + return; 1261 + } 1262 + 1263 + CFTypeID typeID = CFGetTypeID(dest); 1264 + if (typeID == CFDictionaryGetTypeID()) 1265 + { 1266 + CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault, leafName, kCFStringEncodingUTF8); 1267 + CFDictionarySetValue((CFMutableDictionaryRef) dest, key, fileContents); 1268 + CFRelease(key); 1269 + } 1270 + else if (typeID == CFArrayGetTypeID()) 1271 + { 1272 + CFIndex index = atoi(leafName); 1273 + if (index < 0) 1274 + index = 0; 1275 + 1276 + if (index >= CFArrayGetCount((CFArrayRef) dest)) 1277 + CFArrayAppendValue((CFMutableArrayRef) dest, fileContents); 1278 + else 1279 + CFArraySetValueAtIndex((CFMutableArrayRef) dest, index, fileContents); 1280 + } 1281 + 1282 + free(leafName); 1283 + CFRelease(fileContents); 1284 + } 1285 +