GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
key_value2.c
Go to the documentation of this file.
1 
16 #include <grass/gis.h>
17 
27 int G_fwrite_key_value(FILE * fd, const struct Key_Value *kv)
28 {
29  int n;
30  int err;
31 
32  err = 0;
33  for (n = 0; n < kv->nitems; n++)
34  if (kv->value[n][0]) {
35  if (EOF == fprintf(fd, "%s: %s\n", kv->key[n], kv->value[n]))
36  err = -1;
37  }
38  return err;
39 }
40 
51 struct Key_Value *G_fread_key_value(FILE * fd)
52 {
53  struct Key_Value *kv;
54  char *key, *value;
55  char buf[1024];
56 
57  kv = G_create_key_value();
58  if (kv == NULL)
59  return NULL;
60  while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
61  key = value = buf;
62  while (*value && *value != ':')
63  value++;
64  if (*value != ':')
65  continue;
66  *value++ = 0;
67  G_strip(key);
68  G_strip(value);
69  if (!G_set_key_value(key, value, kv)) {
70  G_free_key_value(kv);
71  return NULL;
72  }
73  }
74  return kv;
75 }
struct Key_Value * G_fread_key_value(FILE *fd)
Read key/values pairs from file.
Definition: key_value2.c:51
FILE * fd
Definition: g3dcolor.c:368
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:145
const char * err
Definition: g3dcolor.c:50
int G_fwrite_key_value(FILE *fd, const struct Key_Value *kv)
Write key/value pairs to file.
Definition: key_value2.c:27
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char * value
Definition: env.c:30
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
int n
Definition: dataquad.c:291
int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:55
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:25