Fix TC crash issue
[platform/core/api/media-content.git] / src / media_filter.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17
18 #include <media_info_private.h>
19
20 static const char *media_token[] =
21 {
22         " ",
23         "\"",
24         "'",
25         "(",
26         ")",
27         "=",
28         "<=",
29         "<",
30         ">=",
31         ">",
32         "!=",
33 };
34
35
36 typedef struct _token_t
37 {
38         int type;
39         char *str;
40 }token_t;
41
42
43 #define MAX_LEFT_VALUE 512
44 #define SPACE_LEN 1
45 #define SPACE " "
46 #define UNKNOWN_TYPE 1000
47 #define STRING_TYPE 100
48
49 static char *__get_order_str(media_content_order_e order_enum);
50 static char *__get_collate_str(media_content_collation_e collate_type);
51 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data);
52 static char *__media_filter_replace_attr(attribute_h attr, char *name);
53 static int __tokenize_operator(token_t *token, const char *str, int op_type);
54 static int __tokenize(GList **token_list, const char *str);
55
56 static bool __is_pinyin_needed(void)
57 {
58 #if 0
59         char *lang = NULL;
60         const char *china = "zh_CN";
61         const char *hongkong = "zh_HK";
62 #endif
63         int ret = FALSE;
64 #if 0
65         /*Check CSC first*/
66         bool pinyin_support = FALSE;
67         media_svc_check_pinyin_support(&pinyin_support);
68         if(pinyin_support)
69         {
70                 /*Check Language Setting*/
71                 lang = vconf_get_str(VCONFKEY_LANGSET);
72                 media_content_retvm_if(lang == NULL, ret, "Fail to get string of language set");
73
74                 if((strncmp(china, lang, strlen(china)) == 0) ||
75                         (strncmp(hongkong, lang, strlen(hongkong)) == 0))
76                 {
77                         ret = TRUE;
78                 }
79
80                 SAFE_FREE(lang);
81         }
82 #endif
83         return ret;
84 }
85
86 static char *__get_order_str(media_content_order_e order_enum)
87 {
88         switch(order_enum) {
89                 case MEDIA_CONTENT_ORDER_ASC:
90                         return (char *)"ASC";
91                 case MEDIA_CONTENT_ORDER_DESC:
92                         return (char *)"DESC";
93                 default:
94                         return (char *)" ";
95         }
96 }
97
98 static char *__get_collate_str(media_content_collation_e collate_type)
99 {
100         switch(collate_type) {
101                 case MEDIA_CONTENT_COLLATE_NOCASE:
102                         return (char *)"NOCASE";
103                 case MEDIA_CONTENT_COLLATE_RTRIM:
104                         return (char *)"RTRIM";
105                 case MEDIA_CONTENT_COLLATE_LOCALIZED:
106                         if(__is_pinyin_needed())
107                                 return (char *)"NOCASE";
108                         else
109                                 return (char *)"localized";
110                 default: return (char *)" ";
111         }
112 }
113
114 static void __filter_attribute_free_value(gpointer key, gpointer value, gpointer user_data)
115 {
116         SAFE_FREE(key);
117         SAFE_FREE(value);
118 }
119
120 static char *__media_filter_replace_attr(attribute_h attr, char *name)
121 {
122         char *key_temp = NULL;
123         char *generated_value = NULL;
124         attribute_s *_attr = (attribute_s *)attr;
125
126         if(!g_hash_table_lookup_extended(_attr->attr_map,
127                                                                                 name,
128                                                                                 (gpointer)&key_temp, (gpointer)&generated_value))
129         {
130                 //can't find the value
131                 //media_content_error("NOT_FOUND_VALUE(%s)", name);
132                 return NULL;
133         }
134
135         if(STRING_VALID(generated_value))
136         {
137                 return strdup(generated_value);
138         }
139
140         media_content_error("__media_filter_replace_attr fail");
141
142         return NULL;
143 }
144
145 static int __tokenize_operator(token_t *token, const char *str, int op_type)
146 {
147         int ret = 0;
148         const char *tmp = str;
149
150         if(token != NULL && STRING_VALID(tmp))
151         {
152                 token->type = op_type;
153                 int token_size = strlen(media_token[op_type]);
154                 media_content_retvm_if(token_size == 0, -1, "Invalid token_size. op_type[%d]", op_type);
155
156                 token->str = (char*)calloc(token_size+1, sizeof(char));
157                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
158
159                 strncpy(token->str, tmp, token_size);
160                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
161                 ret = token_size;
162         }
163         else
164         {
165                 ret = -1;
166         }
167
168         return ret;
169 }
170
171 static int __tokenize_string(token_t *token, const char *str, int size)
172 {
173         int ret = size;
174         const char *tmp = str;
175
176         if(token != NULL && STRING_VALID(tmp) && size > 0)
177         {
178                 token->str = (char*)calloc(size+1, sizeof(char));
179                 media_content_retvm_if(token->str == NULL, -1, "OUT_OF_MEMORY");
180
181                 token->type = UNKNOWN_TYPE;
182                 strncpy(token->str, tmp, size);
183                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
184         }
185         else
186         {
187                 ret = -1;
188         }
189
190         return ret;
191 }
192
193 static int __tokenize_attribute(GList **token_list, const char *str)
194 {
195         int ret = 0;
196         int idx = 0;
197
198         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
199
200         const char *tmp = str;
201         const char *dst_ptr = str + strlen(str);
202
203         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
204         {
205                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
206                 if(tmp[idx] == ' ')             //" "
207                 {
208                         if(idx == 0)            // ignore the space.
209                         {
210                                 tmp++;
211                                 idx = -1;
212                                 continue;
213                         }
214                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
215                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
216
217                         token->type = UNKNOWN_TYPE;
218                         token->str = (char*)calloc(idx+1, sizeof(char));
219                         if(token->str == NULL)
220                         {
221                                 SAFE_FREE(token);
222                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
223                                 return -1;
224                         }
225                         strncpy(token->str, tmp, idx);
226                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
227                         *token_list = g_list_append(*token_list, token);
228                         tmp = tmp +idx + strlen(media_token[0]);
229                         idx = -1;
230                 }
231                 else if(tmp[idx] == ',')        // " , "
232                 {
233                         if(idx != 0)
234                         {
235                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
236                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
237
238                                 ret = __tokenize_string(token, tmp, idx);
239                                 if (ret < 0)
240                                 {
241                                         SAFE_FREE(token);
242                                         media_content_error("tokenize error occured");
243                                         return -1;
244                                 }
245                                 else
246                                 {
247                                         *token_list = g_list_append(*token_list, token);
248                                         tmp = tmp + idx;
249                                 }
250                         }
251
252                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
253                         int size = __tokenize_operator(token, tmp,3);
254
255                         if(token != NULL && STRING_VALID(token->str))
256                         {
257                                 *token_list = g_list_append(*token_list, token);
258                                 tmp += size;
259                                 idx = -1;
260                         }
261                         else
262                         {
263                                 SAFE_FREE(token);
264                                 media_content_error("tokenize error occured");
265                                 return -1;
266                         }
267                 }
268         }
269
270         if(*tmp)                        //remained string
271         {
272                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
273                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
274
275                 ret = __tokenize_string(token, tmp, idx);
276                 if (ret < 0)
277                 {
278                         SAFE_FREE(token);
279                         media_content_error("tokenize error occured");
280                         return -1;
281                 }
282
283                 if(token != NULL && STRING_VALID(token->str))
284                 {
285                         *token_list = g_list_append(*token_list, token);
286                 }
287                 else
288                 {
289                         SAFE_FREE(token);
290                         media_content_error("tokenize error occured");
291                         return -1;
292                 }
293         }
294
295         return MEDIA_CONTENT_ERROR_NONE;
296 }
297
298 static int __tokenize(GList **token_list, const char *str)
299 {
300         int ret = 0;
301         int idx = 0;
302
303         media_content_retvm_if(!STRING_VALID(str), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Parameter string is invalid");
304
305         const char *tmp = str;
306         const char *dst_ptr = str + strlen(str);
307
308         for (idx = 0; (*(tmp+idx)) && (tmp < dst_ptr); idx++)
309         {
310                 //media_content_debug("[%d] '%c'", idx, tmp[idx]);
311                 if(tmp[idx] == media_token[0][0])               //" "
312                 {
313                         if(idx == 0)            // ignore the space.
314                         {
315                                 tmp++;
316                                 idx = -1;
317                                 continue;
318                         }
319
320                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
321                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
322
323                         token->type = UNKNOWN_TYPE;
324                         token->str = (char*)calloc(idx+1, sizeof(char));
325                         if(token->str == NULL)
326                         {
327                                 SAFE_FREE(token);
328                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
329                                 return -1;
330                         }
331                         strncpy(token->str, tmp, idx);
332                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
333                         *token_list = g_list_append(*token_list, token);
334                         tmp = tmp +idx + strlen(media_token[0]);
335                         idx = -1;
336                 }
337                 else if(tmp[idx] == media_token[1][0])  // " \" "
338                 {
339                         int j;
340                         bool flag = false;
341                         for(j = idx+1; tmp[j]; j++)     //find next quotation
342                         {
343                                 if(tmp[j] == media_token[1][0] && tmp[j+1] == media_token[1][0])
344                                 {
345                                         j += 1;
346                                         continue;
347                                 }
348                                 if(tmp[j] == media_token[1][0])
349                                 {
350                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
351                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
352
353                                         token->str = (char*) calloc(j+1+1, sizeof(char));
354                                         if(token->str == NULL)
355                                         {
356                                                 SAFE_FREE(token);
357                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
358                                                 return -1;
359                                         }
360                                         token->type = STRING_TYPE;
361                                         strncpy(token->str, tmp, j+1);
362                                         //media_content_debug("type : [%d] str : [%s], j : %d", token->type, token->str, j);
363                                         *token_list = g_list_append(*token_list, token);
364                                         tmp = tmp + strlen(token->str);
365                                         idx = -1;
366                                         flag = true;
367                                         break;
368                                 }
369                         }
370
371                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
372                         {
373                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
374                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
375
376                                 token->str = (char*) calloc(j+1,sizeof(char));
377                                 if(token->str == NULL)
378                                 {
379                                         SAFE_FREE(token);
380                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
381                                         return -1;
382                                 }
383                                 token->type = UNKNOWN_TYPE;
384                                 strncpy(token->str, tmp,j);
385                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
386                                 *token_list = g_list_append(*token_list, token);
387                                 tmp = tmp +strlen(token->str);
388                                 idx = -1;
389                         }
390                 }
391                 else if(tmp[idx] == media_token[2][0])  // " \' "
392                 {
393                         int j;
394                         bool flag = false;
395                         for(j = idx+1; tmp[j]; j++)
396                         {
397                                 if(tmp[j] == media_token[2][0] && tmp[j+1] == media_token[2][0])
398                                 {
399                                         j += 1;
400                                         continue;
401                                 }
402                                 if(tmp[j] == media_token[2][0])
403                                 {
404                                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
405                                         media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
406
407                                         token->str = (char*) calloc(j+1+1, sizeof(char));
408                                         if(token->str == NULL)
409                                         {
410                                                 SAFE_FREE(token);
411                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
412                                                 return -1;
413                                         }
414                                         token->type = STRING_TYPE;
415                                         strncpy(token->str, tmp, j+1);
416                                         //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
417                                         *token_list = g_list_append(*token_list, token);
418                                         tmp = tmp + strlen(token->str);
419                                         idx = -1;
420                                         flag = true;
421                                         break;
422                                 }
423                         }
424
425                         if(!flag && *tmp != '\0' && tmp[j]=='\0')
426                         {
427                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
428                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
429
430                                 token->str = (char*) calloc(j+1,sizeof(char));
431                                 if(token->str == NULL)
432                                 {
433                                         SAFE_FREE(token);
434                                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
435                                         return -1;
436                                 }
437                                 token->type = UNKNOWN_TYPE;
438                                 strncpy(token->str, tmp,j);
439                                 //media_content_debug("type : [%d] str : [%s]", token->type, token->str);
440                                 *token_list = g_list_append(*token_list, token);
441                                 tmp = tmp + strlen(token->str);
442                                 idx = -1;
443                         }
444                 }
445                 else if(tmp[idx] == media_token[3][0])  //"("
446                 {
447                         if(idx != 0)
448                         {
449                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
450                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
451
452                                 ret = __tokenize_string(token, tmp, idx);
453                                 if (ret < 0)
454                                 {
455                                         SAFE_FREE(token);
456                                         media_content_error("tokenize error occured");
457                                         return -1;
458                                 }
459                                 else
460                                 {
461                                         *token_list = g_list_append(*token_list, token);
462                                         tmp = tmp + idx;
463                                 }
464                         }
465                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
466                         int size = __tokenize_operator(token, tmp,3);
467
468                         if(token != NULL && STRING_VALID(token->str))
469                         {
470                                 *token_list = g_list_append(*token_list, token);
471                                 tmp += size;
472                                 idx = -1;
473                         }
474                         else
475                         {
476                                 SAFE_FREE(token);
477                                 media_content_error("tokenize error occured");
478                                 return -1;
479                         }
480
481                 }
482                 else if(tmp[idx] == media_token[4][0])  //")"
483                 {
484                         if(idx != 0)
485                         {
486                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
487                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
488
489                                 ret = __tokenize_string(token, tmp, idx);
490                                 if (ret < 0)
491                                 {
492                                         SAFE_FREE(token);
493                                         media_content_error("tokenize error occured");
494                                         return -1;
495                                 }
496                                 else
497                                 {
498                                         *token_list = g_list_append(*token_list, token);
499                                         tmp = tmp + idx;
500                                 }
501                         }
502                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
503                         int size = __tokenize_operator(token, tmp,4);
504
505                         if(token != NULL && STRING_VALID(token->str))
506                         {
507                                 *token_list = g_list_append(*token_list, token);
508                                 tmp += size;
509                                 idx = -1;
510                         }
511                         else
512                         {
513                                 SAFE_FREE(token);
514                                 media_content_error("tokenize error occured");
515                                 return -1;
516                         }
517
518                 }
519                 else if(tmp[idx] == media_token[5][0])  //"="
520                 {
521                         if(idx != 0)
522                         {
523                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
524                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
525
526                                 ret = __tokenize_string(token, tmp, idx);
527                                 if (ret < 0)
528                                 {
529                                         SAFE_FREE(token);
530                                         media_content_error("tokenize error occured");
531                                         return -1;
532                                 }
533                                 else
534                                 {
535                                         *token_list = g_list_append(*token_list, token);
536                                         tmp = tmp + idx;
537                                 }
538                         }
539                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
540                         int size = __tokenize_operator(token, tmp,5);
541
542                         if(token != NULL && STRING_VALID(token->str))
543                         {
544                                 *token_list = g_list_append(*token_list, token);
545                                 tmp += size;
546                                 idx = -1;
547                         }
548                         else
549                         {
550                                 SAFE_FREE(token);
551                                 media_content_error("tokenize error occured");
552                                 return -1;
553                         }
554
555                 }
556                 else if(tmp[idx] == media_token[6][0] && tmp[idx+1] == media_token[6][1])       //"<=",
557                 {
558                         if(idx != 0)
559                         {
560                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
561                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
562
563                                 ret = __tokenize_string(token, tmp, idx);
564                                 if (ret < 0)
565                                 {
566                                         SAFE_FREE(token);
567                                         media_content_error("tokenize error occured");
568                                         return -1;
569                                 }
570                                 else
571                                 {
572                                         *token_list = g_list_append(*token_list, token);
573                                         tmp = tmp + idx;
574                                 }
575                         }
576                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
577                         int size = __tokenize_operator(token, tmp,6);
578
579                         if(token != NULL && STRING_VALID(token->str))
580                         {
581                                 *token_list = g_list_append(*token_list, token);
582                                 tmp += size;
583                                 idx = -1;
584                         }
585                         else
586                         {
587                                 SAFE_FREE(token);
588                                 media_content_error("tokenize error occured");
589                                 return -1;
590                         }
591
592                 }
593                 else if(tmp[idx] == media_token[7][0])  //"<",
594                 {
595                         if(idx != 0)
596                         {
597                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
598                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
599
600                                 ret = __tokenize_string(token, tmp, idx);
601                                 if (ret < 0)
602                                 {
603                                         SAFE_FREE(token);
604                                         media_content_error("tokenize error occured");
605                                         return -1;
606                                 }
607                                 else
608                                 {
609                                         *token_list = g_list_append(*token_list, token);
610                                         tmp = tmp + idx;
611                                 }
612                         }
613                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
614                         int size = __tokenize_operator(token, tmp,7);
615
616                         if(token != NULL && STRING_VALID(token->str))
617                         {
618                                 *token_list = g_list_append(*token_list, token);
619                                 tmp += size;
620                                 idx = -1;
621                         }
622                         else
623                         {
624                                 SAFE_FREE(token);
625                                 media_content_error("tokenize error occured");
626                                 return -1;
627                         }
628
629                 }
630                 else if(tmp[idx] == media_token[8][0] && tmp[idx+1] == media_token[8][1])       //">=",
631                 {
632                         if(idx != 0)
633                         {
634                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
635                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
636
637                                 ret = __tokenize_string(token, tmp, idx);
638                                 if (ret < 0)
639                                 {
640                                         SAFE_FREE(token);
641                                         media_content_error("tokenize error occured");
642                                         return -1;
643                                 }
644                                 else
645                                 {
646                                         *token_list = g_list_append(*token_list, token);
647                                         tmp = tmp + idx;
648                                 }
649                         }
650                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
651                         int size = __tokenize_operator(token, tmp,8);
652
653                         if(token != NULL && STRING_VALID(token->str))
654                         {
655                                 *token_list = g_list_append(*token_list, token);
656                                 tmp += size;
657                                 idx = -1;
658                         }
659                         else
660                         {
661                                 SAFE_FREE(token);
662                                 media_content_error("tokenize error occured");
663                                 return -1;
664                         }
665
666                 }
667                 else if(tmp[idx] == media_token[9][0])  //">",
668                 {
669                         if(idx != 0)
670                         {
671                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
672                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
673
674                                 ret = __tokenize_string(token, tmp, idx);
675                                 if (ret < 0)
676                                 {
677                                         SAFE_FREE(token);
678                                         media_content_error("tokenize error occured");
679                                         return -1;
680                                 }
681                                 else
682                                 {
683                                         *token_list = g_list_append(*token_list, token);
684                                         tmp = tmp + idx;
685                                 }
686                         }
687                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
688                         int size = __tokenize_operator(token, tmp,9);
689
690                         if(token != NULL && STRING_VALID(token->str))
691                         {
692                                 *token_list = g_list_append(*token_list, token);
693                                 tmp += size;
694                                 idx = -1;
695                         }
696                         else
697                         {
698                                 SAFE_FREE(token);
699                                 media_content_error("tokenize error occured");
700                                 return -1;
701                         }
702                 }
703                 else if(tmp[idx] == media_token[10][0] && tmp[idx+1] == media_token[10][1])     //"!=",
704                 {
705                         if(idx != 0)
706                         {
707                                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
708                                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
709
710                                 ret = __tokenize_string(token, tmp, idx);
711                                 if (ret < 0)
712                                 {
713                                         SAFE_FREE(token);
714                                         media_content_error("tokenize error occured");
715                                         return -1;
716                                 }
717                                 else
718                                 {
719                                         *token_list = g_list_append(*token_list, token);
720                                         tmp = tmp + idx;
721                                 }
722                         }
723                         token_t *token = (token_t*)calloc(1, sizeof(token_t));
724                         int size = __tokenize_operator(token, tmp, 10);
725
726                         if(token != NULL && STRING_VALID(token->str))
727                         {
728                                 *token_list = g_list_append(*token_list, token);
729                                 tmp += size;
730                                 idx = -1;
731                         }
732                         else
733                         {
734                                 SAFE_FREE(token);
735                                 media_content_error("tokenize error occured");
736                                 return -1;
737                         }
738                 }
739         }
740
741         if(*tmp)                        //remained string
742         {
743                 token_t *token = (token_t*)calloc(1, sizeof(token_t));
744                 media_content_retvm_if(token == NULL, -1, "OUT_OF_MEMORY");
745
746                 ret = __tokenize_string(token, tmp, idx);
747                 if (ret < 0)
748                 {
749                         SAFE_FREE(token);
750                         media_content_error("tokenize error occured");
751                         return -1;
752                 }
753
754                 if(token != NULL && STRING_VALID(token->str))
755                 {
756                         *token_list = g_list_append(*token_list, token);
757                 }
758                 else
759                 {
760                         SAFE_FREE(token);
761                         media_content_error("tokenize error occured");
762                         return -1;
763                 }
764         }
765
766         return MEDIA_CONTENT_ERROR_NONE;
767 }
768
769 int _media_filter_attribute_create(attribute_h *attr)
770 {
771         int ret = MEDIA_CONTENT_ERROR_NONE;
772
773         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
774
775         attribute_s *_attr = (attribute_s*)calloc(1, sizeof(attribute_s));
776         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
777
778         _attr->attr_map = g_hash_table_new (g_str_hash, g_str_equal);
779         *attr = (attribute_h)_attr;
780
781         return ret;
782 }
783
784 int _media_filter_attribute_add(attribute_h attr, const char *user_attr, const char *platform_attr)
785 {
786         int ret = MEDIA_CONTENT_ERROR_NONE;
787         char *_user = NULL;
788         char *_platform = NULL;
789         attribute_s *_attr = (attribute_s*)attr;
790
791         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
792
793         if(STRING_VALID(user_attr) && STRING_VALID(platform_attr))
794         {
795                 _user = strdup(user_attr);
796                 media_content_retvm_if(_user == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
797
798                 _platform = strdup(platform_attr);
799                 if(_platform == NULL)
800                 {
801                         SAFE_FREE(_user);
802                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
803                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
804                 }
805
806                 g_hash_table_insert (_attr->attr_map, _user, _platform);
807         }
808         else
809         {
810                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
811                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
812         }
813
814         return ret;
815 }
816
817 int _media_filter_attribute_destory(attribute_h attr)
818 {
819         int ret = MEDIA_CONTENT_ERROR_NONE;
820         attribute_s *_attr = (attribute_s*)attr;
821
822         media_content_retvm_if(_attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
823
824         if(_attr->attr_map != NULL)
825         {
826                 g_hash_table_foreach(_attr->attr_map, __filter_attribute_free_value, NULL);
827                 g_hash_table_destroy(_attr->attr_map);
828         }
829
830         SAFE_FREE(_attr);
831
832         return ret;
833 }
834
835 int _media_filter_attribute_generate(attribute_h attr, char *condition, media_content_collation_e collate_type, char **generated_condition)
836 {
837         unsigned int idx = 0;
838         GList *token_list = NULL;
839         int size = 0;
840         int ret = MEDIA_CONTENT_ERROR_NONE;
841         int total_str_size = 0;
842         token_t *token;
843
844         media_content_retvm_if(condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid condition");
845         media_content_retvm_if(generated_condition == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid generated_condition");
846         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_DB_FAILED, "DB field mapping table doesn't exist. Check db connection");
847
848         if(__tokenize(&token_list, condition) < 0)
849         {
850                 media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
851                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
852         }
853
854         for(idx = 0; idx < g_list_length(token_list); idx++)
855         {
856                 token = (token_t*)g_list_nth_data(token_list, idx);
857
858                 if(token->type == UNKNOWN_TYPE)
859                 {
860                         char *replace_str = __media_filter_replace_attr(attr, token->str);
861                         if(STRING_VALID(replace_str))
862                         {
863                                 SAFE_FREE(token->str);
864                                 token->str = replace_str;
865                         }
866                 }
867
868                 total_str_size += strlen(token->str)+1;
869                 //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
870         }
871
872         //make the statment
873         size = total_str_size + COLLATE_STR_SIZE + 1;
874         * generated_condition = (char*)calloc(size, sizeof(char));
875
876         for(idx = 0; idx < g_list_length(token_list); idx++)
877         {
878                 token = (token_t*)g_list_nth_data(token_list, idx);
879
880                 if((token != NULL) && STRING_VALID(token->str))
881                 {
882                         SAFE_STRLCAT(*generated_condition, token->str, size);
883                         SAFE_STRLCAT(*generated_condition, SPACE, size);
884
885                         SAFE_FREE(token->str);
886                         SAFE_FREE(token);
887                 }
888         }
889
890         if(collate_type == MEDIA_CONTENT_COLLATE_NOCASE || collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
891                 SAFE_STRLCAT(*generated_condition, "COLLATE ", size);
892                 SAFE_STRLCAT(*generated_condition, __get_collate_str(collate_type), size);
893                 SAFE_STRLCAT(*generated_condition, SPACE, size);
894         }
895
896         //media_content_debug("statement : %s(%d) (total:%d)", *generated_condition, strlen(*generated_condition), total_str_size);
897         media_content_sec_debug("Condition : %s", *generated_condition);
898
899         //if(*generated_condition != NULL)
900         //      res = 1;
901
902         if(token_list != NULL)
903                 g_list_free(token_list);
904
905         return ret;
906 }
907
908 int _media_filter_attribute_option_generate(attribute_h attr, filter_h filter, char **generated_option)
909 {
910         int ret = MEDIA_CONTENT_ERROR_NONE;
911         filter_s *_filter = NULL;
912         char option_query[DEFAULT_QUERY_SIZE] = {0, };
913         char condition[DEFAULT_QUERY_SIZE] = {0, };
914         int size = 0;
915         //bool order_by = true;
916
917         media_content_retvm_if(attr == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid attr");
918         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
919
920         _filter = (filter_s*)filter;
921
922         memset(option_query, 0x00, sizeof(option_query));
923
924         /* Order by*/
925         if(STRING_VALID(_filter->order_keyword) && ((_filter->order_type == MEDIA_CONTENT_ORDER_ASC) ||(_filter->order_type == MEDIA_CONTENT_ORDER_DESC)))
926         {
927                 unsigned int idx = 0;
928                 int total_str_size = 0;
929                 GList *token_list = NULL;
930                 token_t *token;
931                 char *attr_str;
932
933                 if(__tokenize_attribute(&token_list, _filter->order_keyword) < 0)
934                 {
935                         media_content_error("INVALID_PARAMETER(0x%08x):Invalid the condition", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
936                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
937                 }
938
939                 for(idx = 0; idx < g_list_length(token_list); idx++)
940                 {
941                         token = (token_t*)g_list_nth_data(token_list, idx);
942
943                         if(token->type == UNKNOWN_TYPE)
944                         {
945                                 char *replace_str = __media_filter_replace_attr(attr, token->str);
946                                 if(STRING_VALID(replace_str))
947                                 {
948                                         attr_str = (char*)calloc(strlen(replace_str) + COLLATE_STR_SIZE + 1, sizeof(char));
949                                         if(attr_str == NULL)
950                                         {
951                                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
952                                                 SAFE_FREE(replace_str);
953                                                 continue;
954                                         }
955
956                                         if(_filter->order_collate_type == MEDIA_CONTENT_COLLATE_NOCASE || _filter->order_collate_type == MEDIA_CONTENT_COLLATE_RTRIM ||_filter->order_collate_type == MEDIA_CONTENT_COLLATE_LOCALIZED) {
957                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s COLLATE %s %s", replace_str, __get_collate_str(_filter->order_collate_type), __get_order_str(_filter->order_type));
958                                         } else {
959                                                 snprintf(attr_str, strlen(replace_str) + COLLATE_STR_SIZE + 1, "%s %s", replace_str, __get_order_str(_filter->order_type));
960                                         }
961
962                                         SAFE_FREE(token->str);
963                                         token->str = attr_str;
964                                         SAFE_FREE(replace_str);
965                                 }
966                                 else
967                                 {
968                                         media_content_error("There is no matched db field for %s", token->str);
969                                 }
970                         }
971
972                         total_str_size += strlen(token->str) + 1;
973                         //media_content_debug("[%d][type:%d]:%s", idx, token->type, token->str);
974                 }
975
976                 //make the statment
977                 char *generated_condition = NULL;
978                 size = total_str_size + COLLATE_STR_SIZE + 1;
979                 generated_condition = (char*)calloc(size, sizeof(char));
980
981                 for(idx = 0; idx < g_list_length(token_list); idx++)
982                 {
983                         token = (token_t*)g_list_nth_data(token_list, idx);
984
985                         if((token != NULL) && STRING_VALID(token->str))
986                         {
987                                 //media_content_debug("[%d] %s", idx, token->str);
988                                 SAFE_STRLCAT(generated_condition, token->str, size);
989                                 SAFE_STRLCAT(generated_condition, SPACE, size);
990
991                                 SAFE_FREE(token->str);
992                                 SAFE_FREE(token);
993                         }
994                 }
995
996                 snprintf(condition, sizeof(condition), "ORDER BY %s", generated_condition);
997                 SAFE_STRLCAT(option_query, condition, sizeof(option_query));
998
999                 if(token_list != NULL)
1000                         g_list_free(token_list);
1001
1002                 SAFE_FREE(generated_condition);
1003         }
1004
1005         /* offset */
1006         SAFE_STRLCAT(option_query, SPACE, sizeof(option_query));
1007         snprintf(condition, sizeof(condition), "LIMIT %d, %d", _filter->offset, _filter->count);
1008         SAFE_STRLCAT(option_query, condition, sizeof(option_query));
1009
1010         if(STRING_VALID(option_query))
1011         {
1012                 *generated_option = strdup(option_query);
1013         }
1014         else
1015         {
1016                 *generated_option = NULL;
1017         }
1018
1019         return ret;
1020 }
1021
1022 int media_filter_create(filter_h *filter)
1023 {
1024         int ret = MEDIA_CONTENT_ERROR_NONE;
1025
1026         media_content_retvm_if(filter == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid filter");
1027
1028         filter_s *_filter = (filter_s*)calloc(1, sizeof(filter_s));
1029         media_content_retvm_if(_filter == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1030
1031         _filter->storage_id = NULL;
1032         _filter->condition = NULL;
1033         _filter->order_keyword = NULL;
1034         _filter->order_type = -1;
1035         _filter->condition_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1036         _filter->order_collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
1037         _filter->offset = -1;
1038         _filter->count = -1;
1039
1040         *filter = (filter_h)_filter;
1041
1042         return ret;
1043 }
1044
1045 int media_filter_destroy(filter_h filter)
1046 {
1047         int ret = MEDIA_CONTENT_ERROR_NONE;
1048         filter_s *_filter = (filter_s*)filter;
1049
1050         if(_filter)
1051         {
1052                 SAFE_FREE(_filter->storage_id);
1053                 SAFE_FREE(_filter->condition);
1054                 SAFE_FREE(_filter->order_keyword);
1055                 SAFE_FREE(_filter);
1056
1057                 ret = MEDIA_CONTENT_ERROR_NONE;
1058         }
1059         else
1060         {
1061                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1062                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1063         }
1064
1065         return ret;
1066 }
1067
1068 int media_filter_set_offset(filter_h filter, int offset, int count)
1069 {
1070         int ret = MEDIA_CONTENT_ERROR_NONE;
1071         filter_s *_filter = (filter_s*)filter;
1072
1073         if(_filter != NULL)
1074         {
1075                 _filter->offset = offset;
1076                 _filter->count = count;
1077         }
1078         else
1079         {
1080                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1081                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1082         }
1083
1084         return ret;
1085 }
1086
1087 int media_filter_set_condition(filter_h filter, const char *condition, media_content_collation_e collate_type)
1088 {
1089         int ret = MEDIA_CONTENT_ERROR_NONE;
1090         filter_s *_filter = (filter_s*)filter;
1091
1092         if((_filter != NULL) && STRING_VALID(condition)
1093                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1094         {
1095                 if(STRING_VALID(_filter->condition))
1096                 {
1097                         SAFE_FREE(_filter->condition);
1098                 }
1099
1100                 _filter->condition = strdup(condition);
1101                 media_content_retvm_if(_filter->condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1102
1103                 media_content_sec_debug("Condition string : %s", _filter->condition);
1104
1105                 _filter->condition_collate_type = collate_type;
1106         }
1107         else
1108         {
1109                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1110                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1111         }
1112
1113         return ret;
1114 }
1115
1116 int media_filter_set_order(filter_h filter, media_content_order_e order_type, const char *order_keyword, media_content_collation_e collate_type)
1117 {
1118         int ret = MEDIA_CONTENT_ERROR_NONE;
1119         filter_s *_filter = (filter_s*)filter;
1120
1121         if((_filter != NULL) && STRING_VALID(order_keyword)
1122                 && ((order_type == MEDIA_CONTENT_ORDER_ASC) ||(order_type == MEDIA_CONTENT_ORDER_DESC))
1123                 && ((collate_type >= MEDIA_CONTENT_COLLATE_DEFAULT) && (collate_type <= MEDIA_CONTENT_COLLATE_LOCALIZED)))
1124         {
1125                 SAFE_FREE(_filter->order_keyword);
1126
1127                 if(STRING_VALID(order_keyword))
1128                 {
1129                         _filter->order_keyword = strdup(order_keyword);
1130                         media_content_retvm_if(_filter->order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1131
1132                         _filter->order_type = order_type;
1133                         _filter->order_collate_type = collate_type;
1134                 }
1135                 else
1136                 {
1137                         media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1138                         ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1139                 }
1140         }
1141         else
1142         {
1143                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1144                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1145         }
1146
1147         return ret;
1148 }
1149
1150 int media_filter_set_storage(filter_h filter, const char *storage_id)
1151 {
1152         int ret = MEDIA_CONTENT_ERROR_NONE;
1153         filter_s *_filter = (filter_s*)filter;
1154
1155         if((_filter != NULL) && STRING_VALID(storage_id))
1156         {
1157                 if(STRING_VALID(_filter->storage_id))
1158                 {
1159                         SAFE_FREE(_filter->storage_id);
1160                 }
1161
1162                 _filter->storage_id = strdup(storage_id);
1163                 media_content_retvm_if(_filter->storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1164
1165                 media_content_sec_debug("storage_id : %s", _filter->storage_id);
1166         }
1167         else
1168         {
1169                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1170                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1171         }
1172
1173         return ret;
1174 }
1175
1176 int media_filter_get_offset(filter_h filter, int *offset, int *count)
1177 {
1178         int ret = MEDIA_CONTENT_ERROR_NONE;
1179         filter_s *_filter = (filter_s*)filter;
1180
1181         if(_filter)
1182         {
1183                 *offset = _filter->offset;
1184                 *count = _filter->count;
1185         }
1186         else
1187         {
1188                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1189                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1190         }
1191
1192         return ret;
1193 }
1194
1195 int media_filter_get_condition(filter_h filter, char **condition, media_content_collation_e *collate_type)
1196 {
1197         int ret = MEDIA_CONTENT_ERROR_NONE;
1198         filter_s *_filter = (filter_s*)filter;
1199
1200         if(_filter)
1201         {
1202                 if(STRING_VALID(_filter->condition))
1203                 {
1204                         *condition = strdup(_filter->condition);
1205                         media_content_retvm_if(*condition == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1206                 }
1207                 else
1208                 {
1209                         *condition = NULL;
1210                 }
1211
1212                 *collate_type = _filter->condition_collate_type;
1213         }
1214         else
1215         {
1216                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1217                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1218         }
1219
1220         return ret;
1221 }
1222
1223 int media_filter_get_order(filter_h filter, media_content_order_e* order_type, char **order_keyword, media_content_collation_e *collate_type)
1224 {
1225         int ret = MEDIA_CONTENT_ERROR_NONE;
1226         filter_s *_filter = (filter_s*)filter;
1227
1228         if(_filter)
1229         {
1230                 if(STRING_VALID(_filter->order_keyword))
1231                 {
1232                         *order_keyword = strdup(_filter->order_keyword);
1233                         media_content_retvm_if(*order_keyword == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1234                 }
1235                 else
1236                 {
1237                         *order_keyword = NULL;
1238                 }
1239
1240                 *order_type = _filter->order_type;
1241                 *collate_type = _filter->order_collate_type;
1242         }
1243         else
1244         {
1245                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1246                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1247         }
1248
1249         return ret;
1250 }
1251
1252 int media_filter_get_storage(filter_h filter, char **storage_id)
1253 {
1254         int ret = MEDIA_CONTENT_ERROR_NONE;
1255         filter_s *_filter = (filter_s*)filter;
1256
1257         if(_filter)
1258         {
1259                 if(STRING_VALID(_filter->storage_id))
1260                 {
1261                         *storage_id = strdup(_filter->storage_id);
1262                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
1263                 }
1264                 else
1265                 {
1266                         *storage_id = NULL;
1267                 }
1268         }
1269         else
1270         {
1271                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
1272                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1273         }
1274
1275         return ret;
1276 }