1 /**
2 * @file lv_utils.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_utils.h"
10 #include "lv_fs.h"
11 #include "lv_types.h"
12 #include "cache/lv_image_cache.h"
13
14 /*********************
15 * DEFINES
16 *********************/
17
18 /**********************
19 * TYPEDEFS
20 **********************/
21
22 /**********************
23 * STATIC PROTOTYPES
24 **********************/
25
26 /**********************
27 * STATIC VARIABLES
28 **********************/
29
30 /**********************
31 * MACROS
32 **********************/
33
34 /**********************
35 * GLOBAL FUNCTIONS
36 **********************/
37
lv_utils_bsearch(const void * key,const void * base,size_t n,size_t size,int (* cmp)(const void * pRef,const void * pElement))38 void * lv_utils_bsearch(const void * key, const void * base, size_t n, size_t size,
39 int (*cmp)(const void * pRef, const void * pElement))
40 {
41 const char * middle;
42 int32_t c;
43
44 for(middle = base; n != 0;) {
45 middle += (n / 2) * size;
46 if((c = (*cmp)(key, middle)) > 0) {
47 n = (n / 2) - ((n & 1) == 0);
48 base = (middle += size);
49 }
50 else if(c < 0) {
51 n /= 2;
52 middle = base;
53 }
54 else {
55 return (char *)middle;
56 }
57 }
58 return NULL;
59 }
60
lv_draw_buf_save_to_file(const lv_draw_buf_t * draw_buf,const char * path)61 lv_result_t lv_draw_buf_save_to_file(const lv_draw_buf_t * draw_buf, const char * path)
62 {
63 lv_fs_file_t file;
64 lv_fs_res_t res = lv_fs_open(&file, path, LV_FS_MODE_WR);
65 if(res != LV_FS_RES_OK) {
66 LV_LOG_ERROR("create file %s failed", path);
67 return LV_RESULT_INVALID;
68 }
69
70 /*Image content modified, invalidate image cache.*/
71 lv_image_cache_drop(path);
72
73 uint32_t bw;
74 res = lv_fs_write(&file, &draw_buf->header, sizeof(draw_buf->header), &bw);
75 if(res != LV_FS_RES_OK || bw != sizeof(draw_buf->header)) {
76 LV_LOG_ERROR("write draw_buf->header failed");
77 lv_fs_close(&file);
78 return LV_RESULT_INVALID;
79 }
80
81 res = lv_fs_write(&file, draw_buf->data, draw_buf->data_size, &bw);
82 if(res != LV_FS_RES_OK || bw != draw_buf->data_size) {
83 LV_LOG_ERROR("write draw_buf->data failed");
84 lv_fs_close(&file);
85 return LV_RESULT_INVALID;
86 }
87
88 lv_fs_close(&file);
89 LV_LOG_TRACE("saved draw_buf to %s", path);
90 return LV_RESULT_OK;
91 }
92
93 /**********************
94 * STATIC FUNCTIONS
95 **********************/
96