GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
raster_metadata.c
Go to the documentation of this file.
1 /* raster_metadata.c
2  *
3  * PURPOSE: functions to read and write raster "units" and "vertical datum"
4  * meta-data info
5  *
6  * Copyright (C) 2007 by Hamish Bowman, and the GRASS Development Team
7  * Author(s): Hamish Bowman, Dunedin, New Zealand
8  *
9  * This program is free software under the GNU General Public
10  * License (>=v2). Read the file COPYING that comes with GRASS
11  * for details.
12  */
13 #include <stdio.h>
14 #include <string.h>
15 #include <grass/gis.h>
16 #include <grass/glocale.h>
17 
18 
30 int G_read_raster_units(const char *name, const char *mapset, char *str)
31 {
32  return G__raster_misc_read_line("units", name, mapset, str);
33 }
34 
35 
46 int G_write_raster_units(const char *name, const char *str)
47 {
48  return G__raster_misc_write_line("units", name, str);
49 }
50 
51 
63 int G_read_raster_vdatum(const char *name, const char *mapset, char *str)
64 {
65  return G__raster_misc_read_line("vertical_datum", name, mapset, str);
66 }
67 
68 
79 int G_write_raster_vdatum(const char *name, const char *str)
80 {
81  return G__raster_misc_write_line("vertical_datum", name, str);
82 }
83 
84 
85 
98 int G__raster_misc_read_line(const char *elem, const char *name,
99  const char *mapset, char *str)
100 {
101  FILE *fd;
102  char buff[GNAME_MAX];
103 
104  buff[0] = '\0';
105 
106  if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
107  return -1;
108 
109  fd = G_fopen_old_misc("cell_misc", elem, name, mapset);
110  if (!fd) {
111  G_warning(_("Can't read %s for [%s in %s]"), elem, name, mapset);
112  return -1;
113  }
114  if (G_getl2(buff, sizeof(buff) - 1, fd) == 0) {
115  /* file is empty */
116  return fclose(fd);
117  }
118 
119  strcpy(str, buff);
120 
121  return fclose(fd);
122 }
123 
124 
137 int G__raster_misc_write_line(const char *elem, const char *name,
138  const char *str)
139 {
140  FILE *fd;
141 
142  fd = G_fopen_new_misc("cell_misc", elem, name);
143  if (fd == NULL) {
144  G_warning(_("Can't create %s metadata file for [%s in %s]"),
145  elem, name, G_mapset());
146  return -1;
147  }
148 
149  fprintf(fd, "%s", str);
150 
151  return fclose(fd);
152 }