Fix a prevent defect.
[platform/core/multimedia/libmm-common.git] / mm_attrs_private.c
1 /*
2  * libmm-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22  
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <malloc.h>
28 #include <assert.h>
29 #include <stdbool.h>
30 #include "mm_debug.h"
31 #include "mm_attrs_private.h"
32
33 int mmf_value_init(mmf_value_t *value, int type)
34 {
35         return_val_if_fail(value, -1);
36         return_val_if_fail(MMF_IS_VALUE_TYPE(type), -1);
37         memset(value, 0, sizeof(*value));
38         value->type = type;
39         return 0;
40 }
41
42 int mmf_value_copy(mmf_value_t *dest, const mmf_value_t *src)
43 {
44         return_val_if_fail(dest && src && src->type == dest->type, -1);
45         switch (src->type) {
46         case MM_ATTRS_TYPE_INT:
47                 dest->value.i_val = src->value.i_val;
48                 break;
49         case MM_ATTRS_TYPE_DOUBLE:
50                 dest->value.d_val = src->value.d_val;
51                 break;
52         case MM_ATTRS_TYPE_STRING:
53                 if (dest->value.s_val) {
54                         free(dest->value.s_val);
55                         dest->value.s_val = NULL;
56                         dest->size = 0;
57                 }
58                 if (src->value.s_val) {
59                         dest->value.s_val = strdup(src->value.s_val);
60                         dest->size = src->size;
61                 }
62                 break;
63         case MM_ATTRS_TYPE_DATA:
64                 dest->value.p_val = src->value.p_val;
65                 dest->size = src->size;
66                 break;
67         default:
68                 break;
69         }
70         return 0;
71 }
72
73 int mmf_value_set_int(mmf_value_t *v, int ival)
74 {
75         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
76         v->value.i_val = ival;
77         return 0;
78 }
79
80 int mmf_value_get_int(const mmf_value_t *v)
81 {
82         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
83         return v->value.i_val;
84 }
85
86 int mmf_value_set_double(mmf_value_t *v, double dval)
87 {
88         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DOUBLE, -1);
89         v->value.d_val = dval;
90         return 0;
91 }
92
93 double mmf_value_get_double(mmf_value_t *v)
94 {
95         return_val_if_fail(v->type == MMF_VALUE_TYPE_DOUBLE, -1);
96         return v->value.d_val;
97 }
98
99 int mmf_value_set_string(mmf_value_t *v, const char *sval,int size)
100 {
101         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, -1);
102         if (v->value.s_val != NULL) {
103                 free(v->value.s_val);
104                 v->value.s_val = NULL;
105                 v->size = 0;
106         }
107         if (sval){
108                 v->value.s_val = strdup(sval);
109                 v->size = size;
110                 }
111         else{
112                 v->value.s_val = NULL;
113                 v->size = 0;
114                 }
115         return 0;
116 }
117
118 const char* mmf_value_get_string(const mmf_value_t *v, int *size)
119 {
120         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, NULL);
121         *size=v->size;
122         return v->value.s_val;
123 }
124
125 int mmf_value_set_data(mmf_value_t *v, void *data,int size)
126 {
127         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, -1);
128         v->value.p_val = data;
129         v->size=size;
130         return 0;
131 }
132
133 void* mmf_value_get_data(const mmf_value_t *v,int *size)
134 {
135         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, NULL);
136         *size=v->size;
137         return v->value.p_val;
138 }
139
140 void mmf_value_dump(const mmf_value_t *value)
141 {
142         return_if_fail(value);
143         switch (value->type) {
144         case MMF_VALUE_TYPE_INT:
145                 //mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
146                 break;
147         case MMF_VALUE_TYPE_DOUBLE:
148                 //mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
149                 break;
150         case MMF_VALUE_TYPE_STRING:
151                 //mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
152                 break;
153         case MMF_VALUE_TYPE_DATA:
154                 //mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
155                 break;
156         default:
157                 //mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
158                 break;
159         }
160 }
161
162 int mmf_value_clear(mmf_value_t *value)
163 {
164         return_val_if_fail(value, -1);
165         if (value->type == MMF_VALUE_TYPE_STRING) {
166                 if (value->value.s_val) {
167                         free(value->value.s_val);
168                         value->value.s_val = NULL;
169                         value->size = 0;
170                 }
171         }
172         return 0;
173 }
174
175 int mmf_value_spec_init(mmf_value_spec_t *vs, int vs_type)
176 {
177         return_val_if_fail(vs, -1);
178         memset(vs, 0, sizeof(*vs));
179         vs->type = vs_type;
180         return 0;
181 }
182
183 int mmf_value_spec_set_int_range(mmf_value_spec_t *vs, int min, int max, int dval)
184 {
185         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
186
187         vs->spec.int_spec.range.min = min;
188         vs->spec.int_spec.range.max = max;
189         vs->spec.int_spec.range.dval = dval;
190
191         return 0;
192 }
193
194 int mmf_value_spec_get_int_range(mmf_value_spec_t *vs, int *min, int *max, int *dval)
195 {
196         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
197
198         *min = vs->spec.int_spec.range.min;
199         *max = vs->spec.int_spec.range.max;
200         *dval = vs->spec.int_spec.range.dval;
201
202         return 0;
203 }
204
205 int mmf_value_spec_set_int_array(mmf_value_spec_t *vs, const int *array, int count, int dval)
206 {
207         int *array_copy = NULL;
208
209         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_INT_ARRAY && count > 0, -1);
210
211         array_copy = (int *) malloc(sizeof(int) * count);
212         if (array_copy == NULL) {
213                 debug_error("malloc failed");
214                 return -1;
215         }
216
217         memcpy(array_copy, array, sizeof(int) * count);
218         vs->spec.int_spec.array.array = array_copy;
219         vs->spec.int_spec.array.count = count;
220         vs->spec.int_spec.array.dval = dval;
221
222         return 0;
223 }
224
225 int mmf_value_spec_get_int_array(mmf_value_spec_t *vs, int **array, int *count, int *dval)
226 {
227         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_INT_ARRAY, -1);
228
229         *array = vs->spec.int_spec.array.array;
230         *count = vs->spec.int_spec.array.count;
231         *dval = vs->spec.int_spec.array.dval;
232
233         return 0;
234 }
235
236 int mmf_value_spec_set_double_range(mmf_value_spec_t *vs, double min, double max, double dval)
237 {
238         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
239
240         vs->spec.double_spec.range.min = min;
241         vs->spec.double_spec.range.max = max;
242         vs->spec.double_spec.range.dval = dval;
243
244         return 0;
245 }
246
247 int mmf_value_spec_get_double_range(mmf_value_spec_t *vs, double *min, double *max, double *dval)
248 {
249         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
250
251         *min = vs->spec.double_spec.range.min;
252         *max = vs->spec.double_spec.range.max;
253         *dval = vs->spec.double_spec.range.dval;
254
255         return 0;
256 }
257
258 int mmf_value_spec_set_double_array(mmf_value_spec_t *vs, const double *array, int count, double dval)
259 {
260         double *array_copy = NULL;
261
262         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY && count > 0, -1);
263
264         array_copy = (double *) malloc(sizeof(double) * count);
265         if (array_copy == NULL) {
266                 debug_error("malloc failed");
267                 return -1;
268         }
269
270         memcpy(array_copy, array, sizeof(double) * count);
271         vs->spec.double_spec.array.array = array_copy;
272         vs->spec.double_spec.array.count = count;
273         vs->spec.double_spec.array.dval = dval;
274
275         return 0;
276 }
277
278 int mmf_value_spec_get_double_array(mmf_value_spec_t *vs, double **array, int *count, double *dval)
279 {
280         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY, -1);
281
282         *array = vs->spec.double_spec.array.array;
283         *count = vs->spec.double_spec.array.count;
284         *dval = vs->spec.double_spec.array.dval;
285
286         return 0;
287 }
288
289 int mmf_value_spec_clear(mmf_value_spec_t *vs)
290 {
291         return_val_if_fail(vs, -1);
292         switch(vs->type) {
293         case MMF_VALUE_SPEC_INT_ARRAY:
294                 if (vs->spec.int_spec.array.array) {
295                         free(vs->spec.int_spec.array.array);
296                         vs->spec.int_spec.array.array = NULL;
297                         vs->spec.int_spec.array.count = 0;
298                         vs->spec.int_spec.array.dval = 0;
299                 }
300                 break;
301         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
302                 if (vs->spec.double_spec.array.array) {
303                         free(vs->spec.double_spec.array.array);
304                         vs->spec.double_spec.array.array = NULL;
305                         vs->spec.double_spec.array.count = 0;
306                         vs->spec.double_spec.array.dval = 0;
307                 }
308                 break;
309
310         default:
311                 break;
312         }
313
314         return 0;
315 }
316
317 int mmf_attribute_init(mmf_attribute_t *item, const char *name, int value_type, int flags)
318 {
319         return_val_if_fail(item && name, -1);
320
321         item->name = strdup(name);
322         item->flags = flags;
323
324         mmf_value_spec_init(&item->value_spec, MMF_VALUE_SPEC_NONE);
325         mmf_value_init(&item->value, value_type);
326         mmf_value_init(&item->tmpval, value_type);
327
328         return 0;
329 }
330
331 bool mmf_attribute_check_flags(mmf_attribute_t *item, int flags)
332 {
333         return_val_if_fail(item, false);
334         return item->flags & flags;
335 }
336
337 bool mmf_attribute_validate_int(mmf_attribute_t *item, int val)
338 {
339         return_val_if_fail(item, false);
340         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_INT, false);
341         
342         bool valid = true;
343         int i = 0;
344         
345         switch (item->value_spec.type) {
346         case MMF_VALUE_SPEC_INT_RANGE:
347                 if (val < item->value_spec.spec.int_spec.range.min || 
348                                 val > item->value_spec.spec.int_spec.range.max) {
349                         valid = false;
350                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
351                 }
352                 break;
353         case MMF_VALUE_SPEC_INT_ARRAY:
354                 valid = false;
355                 for (i = 0; i < item->value_spec.spec.int_spec.array.count; i++) {
356                         if (val == item->value_spec.spec.int_spec.array.array[i]) {
357                                 valid = true;
358                                 break;
359                         }
360                 }
361                 if (!valid) {
362                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
363                 }
364                 break;
365         default:
366                 break;          
367         }
368         
369         return valid;
370 }
371
372 bool mmf_attribute_validate_double(mmf_attribute_t *item, double val)
373 {
374         return_val_if_fail(item, false);
375         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_DOUBLE, false);
376         
377         bool valid = true;
378         int i = 0;
379         
380         switch (item->value_spec.type) {
381         case MMF_VALUE_SPEC_DOUBLE_RANGE:
382                 if (val < item->value_spec.spec.double_spec.range.min || 
383                                 val > item->value_spec.spec.double_spec.range.max) {
384                         valid = false;
385                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
386                 }
387                 break;
388         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
389                 valid = false;
390                 for (i = 0; i < item->value_spec.spec.double_spec.array.count; i++) {
391                         if (val == item->value_spec.spec.double_spec.array.array[i]) {
392                                 valid = true;
393                                 break;
394                         }
395                 }
396                 if (!valid) {
397                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
398                 }
399                 break;
400         default:
401                 break;          
402         }
403         
404         return valid;
405 }
406
407 void mmf_attribute_clear(mmf_attribute_t *item)
408 {
409         assert(item);
410         if (item->name) {
411                 free(item->name);
412                 item->name = NULL;
413         }
414
415         mmf_value_clear(&item->tmpval);
416         mmf_value_clear(&item->value);
417         mmf_value_spec_clear(&item->value_spec);
418 }
419
420 bool mmf_attribute_is_modified(mmf_attribute_t *item)
421 {
422         return_val_if_fail(item, false);
423         return (item->flags & MM_ATTRS_FLAG_MODIFIED);
424 }
425
426 void mmf_attribute_set_modified(mmf_attribute_t *item)
427 {
428         return_if_fail(item);
429         if (!(item->flags & MM_ATTRS_FLAG_MODIFIED)) {
430                 mmf_value_copy(&item->tmpval, &item->value);
431                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
432         }
433 }
434
435 void mmf_attribute_set_readonly(mmf_attribute_t *item)
436 {
437         return_if_fail(item);
438         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
439                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
440 }
441
442 void mmf_attribute_set_disabled(mmf_attribute_t *item)
443 {
444         return_if_fail(item);
445         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
446                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
447         if (item->flags & MM_ATTRS_FLAG_READABLE)
448                 item->flags -= MM_ATTRS_FLAG_READABLE;
449         if (item->flags & MM_ATTRS_FLAG_MODIFIED)
450                 item->flags -= MM_ATTRS_FLAG_MODIFIED;
451 }
452
453 void mmf_attribute_commit(mmf_attribute_t *item)
454 {
455         return_if_fail(item);
456         if (item->flags & MM_ATTRS_FLAG_MODIFIED) {
457                 mmf_value_copy(&item->value, &item->tmpval);
458                 mmf_value_clear(&item->tmpval);
459                 item->flags ^= MM_ATTRS_FLAG_MODIFIED;
460         }
461 }
462
463 int mmf_attribute_set_int(mmf_attribute_t *item, int val)
464 {
465         return_val_if_fail(item, -1);
466         if (mmf_value_set_int(&item->tmpval, val) == 0) {
467                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
468                 return 0;
469         }
470         return -1;
471 }
472
473 int mmf_attribute_set_double(mmf_attribute_t *item, double val)
474 {
475         return_val_if_fail(item, -1);
476         if (mmf_value_set_double(&item->tmpval, val) == 0) {
477                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
478                 return 0;
479         }
480         return -1;
481 }
482
483 int mmf_attribute_set_string(mmf_attribute_t *item, const char *string, int size)
484 {
485         return_val_if_fail(item, -1);
486
487         if (mmf_value_set_string(&item->tmpval, string,size) == 0) {
488                 if (string)
489                         item->flags |= MM_ATTRS_FLAG_MODIFIED;
490
491                 return 0;
492         }
493         return -1;
494 }
495
496 int mmf_attribute_set_data(mmf_attribute_t *item, void *data, int size)
497 {
498         return_val_if_fail(item, -1);
499
500         if (mmf_value_set_data(&item->tmpval, data,size) == 0) {
501                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
502                 return 0;
503         }
504         return -1;
505 }
506
507 MMHandleType mmf_attrs_new(int count)
508 {
509         return_val_if_fail(count > 0, 0);
510         mmf_attrs_t *attrs;
511         attrs = (mmf_attrs_t *) malloc (sizeof(mmf_attrs_t));
512
513         if (attrs == NULL) {
514                 debug_error("malloc failed");
515                 return 0;
516         }
517
518         attrs->count = count;
519         attrs->items = (mmf_attribute_t *) malloc (sizeof(mmf_attribute_t) * count);
520         if(attrs->items == NULL) {
521                 debug_error("Failed to malloc for attrs->items.");
522                 free(attrs);
523                 attrs=NULL;
524                 return 0;
525         }
526
527         memset(attrs->items, 0, sizeof(mmf_attribute_t) * count);
528
529         if (pthread_mutex_init(&attrs->write_lock, NULL) != 0) {
530                 //mmf_debug(MMF_DEBUG_ERROR, "mutex init failed");
531                 if (attrs) {
532                         if (attrs->items) {
533                                 free(attrs->items);
534                                 attrs->items = NULL;
535                         }
536                         free(attrs);
537                         attrs=NULL;
538                 }
539                 return 0;
540         }
541
542         return (MMHandleType) attrs;
543 }
544
545 MMHandleType mmf_attrs_new_from_data(const char *name,
546                                      mmf_attrs_construct_info_t *info,
547                                      int count,
548                                      mmf_attrs_commit_func_t commit_func,
549                                      void *commit_param)
550 {
551         return_val_if_fail(info && count > 0, 0);
552         MMHandleType h;
553         mmf_attrs_t *attrs;
554
555         h = mmf_attrs_new(count);
556         if(!h) {
557                 return 0;
558         }
559         mmf_attrs_init(h, info, count);
560         attrs = (mmf_attrs_t *) h;
561         attrs->name = NULL;
562         if (name)
563                 attrs->name = strdup(name);
564         attrs->commit_func = commit_func;
565         attrs->commit_param = commit_param;
566         return h;
567 }
568
569 void mmf_attrs_free(MMHandleType h)
570 {
571         return_if_fail(h);
572         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
573         if (attrs) {
574                 if (attrs->name) {
575                         free(attrs->name);
576                         attrs->name = NULL;
577                 }
578                 if (attrs->items) {
579                         int i;
580                         for (i = 0; i < attrs->count; i++) {
581                                 mmf_attribute_clear(&attrs->items[i]);
582                         }
583                         free(attrs->items);
584                         attrs->items = NULL;
585                 }
586                 pthread_mutex_destroy(&attrs->write_lock);
587                 free(attrs);
588         }
589 }
590
591 int mmf_attrs_init(MMHandleType h, mmf_attrs_construct_info_t *info, int count)
592 {
593         return_val_if_fail(h && info && count > 0, -1);
594         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
595         int i;
596         int size = 0;
597
598         for (i = 0; i < count; i++) {
599                 mmf_attribute_init(&attrs->items[i],
600                                    info[i].name,
601                                    info[i].value_type,
602                                    info[i].flags);
603
604                 switch (info[i].value_type) {
605                 case MMF_VALUE_TYPE_INT:
606                         assert(attrs->items[i].value.value.i_val == 0);
607                         mmf_value_set_int(&attrs->items[i].value,
608                                           (int)info[i].default_value);
609                         break;
610                 case MMF_VALUE_TYPE_DOUBLE:
611                 {
612                         int i_val = (int)info[i].default_value;
613                         double d_val = (double) i_val;
614                         assert(attrs->items[i].value.value.d_val == 0);
615                         mmf_value_set_double(&attrs->items[i].value, d_val);
616                         break;
617                 }
618                 case MMF_VALUE_TYPE_STRING:
619                         assert(attrs->items[i].value.value.s_val == NULL);
620                         if (info[i].default_value) {
621                                 size = strlen(info[i].default_value)+1;
622                         }
623                         mmf_value_set_string(&attrs->items[i].value,
624                                              (const char *)info[i].default_value,size);
625                         break;
626                 case MMF_VALUE_TYPE_DATA:
627                         assert(attrs->items[i].value.value.p_val == NULL);
628                         if (info[i].default_value) {
629                                 size = sizeof(info[i].default_value)+1;
630                         }
631                         mmf_value_set_data(&attrs->items[i].value, info[i].default_value,size);
632                         break;
633                 default:
634                         //mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
635                         assert(0);
636                         break;
637                 }
638         }
639
640         return 0;
641 }
642
643 int mmf_attrs_commit(MMHandleType h)
644 {
645         return_val_if_fail(h, -1);
646         
647         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
648         int i;
649         int ret = 0;
650
651         MM_ATTRS_WRITE_LOCK(attrs);
652
653         for (i = 0; i < attrs->count; ++i) {
654                 if (mmf_attribute_is_modified(&attrs->items[i])) {
655                         if (attrs->commit_func) {
656                                 if (attrs->commit_func(i, attrs->items[i].name,
657                                                                                 &attrs->items[i].tmpval,
658                                                                                 attrs->commit_param)) {
659                                         mmf_attribute_commit(&attrs->items[i]);
660                                 } else {
661                                         /* without this, there is no way to solve modify when commit_func failed. */
662                                         if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
663                                                 attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
664                                         ret = -1;
665                                 }
666                         } else {
667                                 mmf_attribute_commit(&attrs->items[i]);
668                         }
669                 }
670         }
671
672         MM_ATTRS_WRITE_UNLOCK(attrs);
673
674         return ret;
675 }
676
677 int mmf_attrs_commit_err(MMHandleType h, char **err_attr_name)
678 {
679         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
680         int i;
681         int ret = 0;
682
683         return_val_if_fail(h, -1);
684
685         MM_ATTRS_WRITE_LOCK(attrs);
686
687         for (i = 0; i < attrs->count; ++i) {
688                 if (mmf_attribute_is_modified(&attrs->items[i])) {
689                         if (attrs->commit_func) {
690                                 if (attrs->commit_func(i, attrs->items[i].name,
691                                                                                 &attrs->items[i].tmpval,
692                                                                                 attrs->commit_param)) {
693                                         mmf_attribute_commit(&attrs->items[i]);
694                                 } else {
695                                         /* without this, there is no way to solve modify when commit_func failed. */
696                                         if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
697                                                 attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
698                                         ret = -1;
699
700                                         /* Set Error information */
701                                         if (err_attr_name)
702                                                 *err_attr_name = strdup(attrs->items[i].name);
703
704                                         break;
705                                 }
706                         } else {
707                                 mmf_attribute_commit(&attrs->items[i]);
708                         }
709                 }
710         }
711
712         MM_ATTRS_WRITE_UNLOCK(attrs);
713
714         return ret;
715 }
716
717 int mmf_attrs_set_valid_type(MMHandleType h, int idx, int v_type)
718 {
719         return_val_if_fail(h, -1);
720         return_val_if_fail(idx>=0, -1);
721         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
722         return mmf_value_spec_init(&attrs->items[idx].value_spec, v_type);
723 }
724
725 int mmf_attrs_set_valid_range(MMHandleType h, int idx, int min, int max, int dval)
726 {
727         return_val_if_fail(h, -1);
728         return_val_if_fail(idx>=0, -1);
729         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
730         mmf_value_spec_clear(&attrs->items[idx].value_spec);
731         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_RANGE);
732         return mmf_value_spec_set_int_range(&attrs->items[idx].value_spec, min, max, dval);
733 }
734
735 int mmf_attrs_set_valid_array(MMHandleType h, int idx, const int *array, int count, int dval)
736 {
737         return_val_if_fail(h, -1);
738         return_val_if_fail(idx>=0, -1);
739         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
740         mmf_value_spec_clear(&attrs->items[idx].value_spec);
741         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_ARRAY);
742         return mmf_value_spec_set_int_array(&attrs->items[idx].value_spec, array, count, dval);
743 }
744
745 int mmf_attrs_set_valid_double_range(MMHandleType h, int idx, double min, double max, double dval)
746 {
747         return_val_if_fail(h, -1);
748         return_val_if_fail(idx>=0, -1);
749         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
750         mmf_value_spec_clear(&attrs->items[idx].value_spec);
751         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_RANGE);
752         return mmf_value_spec_set_double_range(&attrs->items[idx].value_spec, min, max, dval);
753 }
754
755 int mmf_attrs_set_valid_double_array(MMHandleType h, int idx, const double *array, int count, double dval)
756 {
757         return_val_if_fail(h, -1);
758         return_val_if_fail(idx>=0, -1);
759         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
760         mmf_value_spec_clear(&attrs->items[idx].value_spec);
761         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_ARRAY);
762         return mmf_value_spec_set_double_array(&attrs->items[idx].value_spec, array, count, dval);
763 }