theme.c (2540B)
1 /* 2 * This file is part of the Lumidify ToolKit (LTK) 3 * Copyright (c) 2016, 2017 Lumidify Productions <lumidify@openmailbox.org> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in all 13 * copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #include "theme.h" 25 26 LtkTheme *ltk_load_theme(const char *path) 27 { 28 char *file_contents = ltk_read_file(path); 29 30 cJSON *json = cJSON_Parse(file_contents); 31 if (!json) 32 { 33 printf("Theme error before: [%s]\n", cJSON_GetErrorPtr()); 34 return NULL; 35 } 36 cJSON *button_json = cJSON_GetObjectItem(json, "button"); 37 if (!button_json) 38 { 39 printf("Theme error before: [%s]\n", cJSON_GetErrorPtr()); 40 return NULL; 41 } 42 cJSON *window_json = cJSON_GetObjectItem(json, "window"); 43 if (!window_json) 44 { 45 printf("Theme error before: [%s]\n", cJSON_GetErrorPtr()); 46 return NULL; 47 } 48 49 LtkTheme *theme = malloc(sizeof(LtkTheme)); 50 theme->button = ltk_parse_button_theme(button_json); 51 theme->window = ltk_parse_window_theme(window_json); 52 53 free(file_contents); 54 cJSON_Delete(json); 55 56 return theme; 57 } 58 59 void ltk_destroy_theme(LtkTheme *theme) 60 { 61 free(theme->button); 62 free(theme->window); 63 free(theme); 64 } 65 66 char *ltk_read_file(const char *path) 67 { 68 FILE *f; 69 long len; 70 char *file_contents; 71 f = fopen(path, "rb"); 72 fseek(f, 0, SEEK_END); 73 len = ftell(f); 74 fseek(f, 0, SEEK_SET); 75 file_contents = malloc(len + 1); 76 fread(file_contents, 1, len, f); 77 file_contents[len] = '\0'; 78 fclose(f); 79 80 return file_contents; 81 }