2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <Ecore_File.h>
21 #define STORAGE_FILEPATH "/opt/var/.savecbh"
22 #define STORAGE_KEY_INDEX "index"
23 #define STORAGE_KEY_FORMAT "data%02d"
24 #define STORAGE_INDEX_ITEM_NONE 0.0
26 static void storage_item_index_wrote(StorageData *sd);
27 static Eina_Bool item_write(Eet_File *ef, int index, CNP_ITEM *item);
28 static void storage_rewrite_all_items(StorageData *sd);
29 static Eina_Bool storage_index_write(StorageData *sd);
30 static Eina_Bool storage_item_write(AppData *ad, CNP_ITEM *item);
31 static Eina_Bool storage_item_delete(AppData *ad, CNP_ITEM *item);
32 static CNP_ITEM *storage_item_load(StorageData *sd, int index);
34 static void dump_items(StorageData *sd);
39 static int getMinIndex(indexType *indexTable, int len)
47 for (i = 1; i < len; i++)
49 if ((min > indexTable[i]))
58 static int getMaxIndex(indexType *indexTable, int len)
61 indexType max = indexTable[i];
63 for (i = 1; i < len; i++)
65 if (max < indexTable[i])
74 StorageData *init_storage(AppData *ad)
77 StorageData *sd = CALLOC(1, sizeof(StorageData));
81 sd->ef = eet_open(STORAGE_FILEPATH, EET_FILE_MODE_READ_WRITE);
87 read_data = eet_read(sd->ef, STORAGE_KEY_INDEX, &read_size);
89 int storage_size = sizeof(indexType) * STORAGE_ITEM_CNT;
91 int copy_size = storage_size < read_size ? storage_size : read_size;
95 indexType *temp = MALLOC(read_size);
98 memcpy(temp, read_data, read_size);
101 int copy_cnt = copy_size/sizeof(indexType);
102 for (i = 0; i < copy_cnt; i++)
104 int maxIndex = getMaxIndex(temp, copy_cnt);
105 if (temp[maxIndex] == STORAGE_INDEX_ITEM_NONE)
107 sd->itemTable[i] = storage_item_load(sd, maxIndex);
108 if (sd->itemTable[i])
109 sd->indexTable[i] = temp[maxIndex];
110 temp[maxIndex] = STORAGE_INDEX_ITEM_NONE;
112 DMSG("load storage item index %d\n", i);
114 for (i = copy_cnt - 1; i >= 0; i--)
116 if (sd->itemTable[i])
117 item_add_by_CNP_ITEM(ad, sd->itemTable[i]);
122 DMSG("load storage index failed\n");
126 DMSG("storage ef is NULLd\n");
130 ad->storage_item_add = storage_item_write;
131 ad->storage_item_del = storage_item_delete;
132 // ad->storage_item_load = storage_item_load;
137 void depose_storage(StorageData *sd)
140 storage_rewrite_all_items(sd);
146 ecore_file_shutdown();
150 static void dump_items(StorageData *sd)
154 for (i = 0; i < STORAGE_ITEM_CNT; i++)
156 CNP_ITEM *item = storage_item_load(sd, i);
158 printf("item #%d type: 0x%x, data: %s\n, len: %d\n", i, item->type_index, item->data, item->len);
163 static Eina_Bool item_write(Eet_File *ef, int index, CNP_ITEM *item)
167 DMSG("eet_file is NULL\n");
171 snprintf(datakey, 10, STORAGE_KEY_FORMAT, index);
172 int buf_size = item->len + sizeof(int);
173 char *buf = MALLOC(buf_size);
176 ((int *)buf)[0] = item->type_index;
177 char *data = buf + sizeof(int);
178 memcpy(data, item->data, item->len);
180 int ret = eet_write(ef, datakey, buf, buf_size, 1);
181 DMSG("write result: %d, datakey: %s, buf_size: %d, item_len: %d\n", ret, datakey, buf_size, item->len);
187 static void storage_rewrite_all_items(StorageData *sd)
192 ecore_file_remove(STORAGE_FILEPATH);
193 sd->ef = eet_open(STORAGE_FILEPATH, EET_FILE_MODE_READ_WRITE);
196 for (i = 0; i < STORAGE_ITEM_CNT; i++)
198 if ((sd->indexTable[i] != STORAGE_INDEX_ITEM_NONE) && (sd->itemTable[i]))
199 item_write(sd->ef, i, sd->itemTable[i]);
201 storage_index_write(sd);
204 static Eina_Bool storage_item_write(AppData *ad, CNP_ITEM *item)
207 StorageData *sd = ad->storage;
208 int index = getMinIndex(sd->indexTable, STORAGE_ITEM_CNT);
209 sd->indexTable[index] = ecore_time_unix_get();
210 sd->itemTable[index] = item;
212 item_write(sd->ef, index, item);
213 storage_index_write(sd);
218 static Eina_Bool storage_item_delete(AppData *ad, CNP_ITEM *item)
221 StorageData *sd = ad->storage;
223 for (index = 0; index < STORAGE_ITEM_CNT; index++)
225 if (sd->itemTable[index] == item)
229 if (index < STORAGE_ITEM_CNT)
231 sd->indexTable[index] = STORAGE_INDEX_ITEM_NONE;
232 storage_index_write(sd);
237 static CNP_ITEM *storage_item_load(StorageData *sd, int index)
241 DMSG("eet_file is NULL\n");
244 if (index >= STORAGE_ITEM_CNT)
247 indexType copyTable[STORAGE_ITEM_CNT];
248 memcpy(copyTable, sd->indexTable, sizeof(copyTable));
250 for (i = 0; i < index; i++)
252 int maxIndex = getMaxIndex(copyTable, STORAGE_ITEM_CNT);
255 copyTable[maxIndex] = 0;
259 snprintf(datakey, 10, STORAGE_KEY_FORMAT, i);
262 char *read_data = eet_read(sd->ef, datakey, &read_size);
266 DMSG("read failed index: %d\n", index);
269 CNP_ITEM *item = CALLOC(1, sizeof(CNP_ITEM));
272 char *data = read_data + sizeof(int);
273 int data_size = read_size - sizeof(int);
274 char *buf = CALLOC(1, data_size);
280 item->type_index = ((int *)read_data)[0];
281 memcpy(buf, data, data_size);
283 item->len = data_size;
288 static Eina_Bool storage_index_write(StorageData *sd)
294 DMSG("eet_file is NULL\n");
298 for (ret = 0; ret < STORAGE_ITEM_CNT; ret++)
299 printf(", index %d: %lf", ret, sd->indexTable[ret]);
302 ret = eet_write(sd->ef, STORAGE_KEY_INDEX, sd->indexTable, sizeof(indexType) * STORAGE_ITEM_CNT, 1);