key_value4.c

Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 #include <string.h>
00003 
00004 /* -1 can't open file for reading
00005  * -2 no memory for key,value info, file not modified
00006  * -3 can't open file for re-write
00007  * -4 error writing the file (might be damaged)
00008  */
00009 int G_update_key_value_file (char *file,char *key,char *value)
00010 {
00011     struct Key_Value *kv;
00012     int stat;
00013 
00014     kv = G_read_key_value_file (file, &stat);
00015     if (stat != 0)
00016         return stat;
00017 
00018     if(!G_set_key_value (key, value, kv))
00019     {
00020         G_free_key_value(kv);
00021         return -2;
00022     }
00023 
00024     G_write_key_value_file (file, kv, &stat);
00025     G_free_key_value(kv);
00026 
00027     return stat;
00028 }
00029 
00030 /* returns: <0 are file/memory errors
00031  *           0 not found
00032  *           1 ok
00033  */
00034 int G_lookup_key_value_from_file(
00035     char *file,
00036     char *key,
00037     char value[],
00038     int n)
00039 {
00040     struct Key_Value *kv;
00041     int stat;
00042     char *v;
00043 
00044     *value = 0;
00045     kv = G_read_key_value_file (file, &stat);
00046     if (stat != 0)
00047         return stat;
00048     
00049     v = G_find_key_value (key, kv);
00050     if (v)
00051     {
00052         strncpy (value, v, n);
00053         value[n-1] = 0;
00054         stat = 1;
00055     }
00056     else
00057         stat = 0;
00058     G_free_key_value (kv);
00059     return stat;
00060 }

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4