smeteo.c (3173B)
1 /* Copyright (C) 2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is dismshbuted in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "smeteo.h" 17 #include "smeteo_c.h" 18 19 #include <rsys/mem_allocator.h> 20 21 /******************************************************************************* 22 * Helper functions 23 ******************************************************************************/ 24 static INLINE res_T 25 check_smeteo_create_args(const struct smeteo_create_args* args) 26 { 27 if(!args) return RES_BAD_ARG; 28 return RES_OK; 29 } 30 31 static void 32 release_smeteo(ref_T* ref) 33 { 34 struct smeteo* smeteo = CONTAINER_OF(ref, struct smeteo, ref); 35 ASSERT(smeteo); 36 str_release(&smeteo->filename); 37 darray_entry_release(&smeteo->entries); 38 MEM_RM(smeteo->allocator, smeteo); 39 } 40 41 /******************************************************************************* 42 * Exported functions 43 ******************************************************************************/ 44 res_T 45 smeteo_create 46 (const struct smeteo_create_args* args, 47 struct smeteo** out_smeteo) 48 { 49 struct smeteo* smeteo = NULL; 50 struct mem_allocator* allocator = NULL; 51 res_T res = RES_OK; 52 53 if(!out_smeteo) { res = RES_BAD_ARG; goto error; } 54 res = check_smeteo_create_args(args); 55 if(res != RES_OK) goto error; 56 57 allocator = args->allocator ? args->allocator : &mem_default_allocator; 58 smeteo = MEM_CALLOC(allocator, 1, sizeof(*smeteo)); 59 if(!smeteo) { res = RES_MEM_ERR; goto error; } 60 61 ref_init(&smeteo->ref); 62 smeteo->allocator = allocator; 63 smeteo->logger = args->logger ? args->logger : LOGGER_DEFAULT; 64 smeteo->verbose = args->verbose; 65 str_init(smeteo->allocator, &smeteo->filename); 66 darray_entry_init(smeteo->allocator, &smeteo->entries); 67 68 exit: 69 if(out_smeteo) *out_smeteo = smeteo; 70 return res; 71 error: 72 if(smeteo) { SMETEO(ref_put(smeteo)); smeteo = NULL; } 73 goto exit; 74 } 75 76 res_T 77 smeteo_ref_get(struct smeteo* smeteo) 78 { 79 return smeteo ? ref_get(&smeteo->ref), RES_OK : RES_BAD_ARG; 80 } 81 82 res_T 83 smeteo_ref_put(struct smeteo* smeteo) 84 { 85 return smeteo ? ref_put(&smeteo->ref, release_smeteo), RES_OK : RES_BAD_ARG; 86 } 87 88 res_T 89 smeteo_get_desc(const struct smeteo* smeteo, struct smeteo_desc* desc) 90 { 91 if(!smeteo || !desc) return RES_BAD_ARG; 92 93 *desc = SMETEO_DESC_NULL; 94 95 desc->albedo = smeteo->albedo; 96 desc->longitude = smeteo->longitude; 97 desc->latitude = smeteo->latitude; 98 99 desc->nentries = darray_entry_size_get(&smeteo->entries); 100 if(str_len(&smeteo->filename)) desc->filename = str_cget(&smeteo->filename); 101 if(desc->nentries) desc->entries = darray_entry_cdata_get(&smeteo->entries); 102 103 return RES_OK; 104 }