浏览代码

Generate fatal errors rather than returning a status, for:
G_set_key_value
G_write_key_value_file
G_read_key_value_file
G_update_key_value_file


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@40701 15284696-431f-4ddb-bdfa-cd5b030d7da7

Glynn Clements 15 年之前
父节点
当前提交
4a29361f14

+ 2 - 7
general/g.proj/output.c

@@ -193,22 +193,17 @@ void create_location(char *location)
 	}
 
 	{
-	    int out_stat;
 	    char path[GPATH_MAX];
 
 	    /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
 	    if (projinfo != NULL) {
 		G__file_name(path, "", "PROJ_INFO", "PERMANENT");
-		G_write_key_value_file(path, projinfo, &out_stat);
-		if (out_stat != 0)
-		    G_fatal_error(_("Error writing PROJ_INFO"));
+		G_write_key_value_file(path, projinfo);
 	    }
 
 	    if (projunits != NULL) {
 		G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
-		G_write_key_value_file(path, projunits, &out_stat);
-		if (out_stat != 0)
-		    G_fatal_error(_("Error writing PROJ_UNITS"));
+		G_write_key_value_file(path, projunits);
 	    }
 
 	    if ((old_cellhd.zone != cellhd.zone) ||

+ 4 - 4
include/gisdefs.h

@@ -308,7 +308,7 @@ int G_is_mapset(const char *);
 
 /* key_value1.c */
 struct Key_Value *G_create_key_value(void);
-int G_set_key_value(const char *, const char *, struct Key_Value *);
+void G_set_key_value(const char *, const char *, struct Key_Value *);
 const char *G_find_key_value(const char *, const struct Key_Value *);
 void G_free_key_value(struct Key_Value *);
 
@@ -317,11 +317,11 @@ int G_fwrite_key_value(FILE *, const struct Key_Value *);
 struct Key_Value *G_fread_key_value(FILE *);
 
 /* key_value3.c */
-int G_write_key_value_file(const char *, const struct Key_Value *, int *);
-struct Key_Value *G_read_key_value_file(const char *, int *);
+void G_write_key_value_file(const char *, const struct Key_Value *);
+struct Key_Value *G_read_key_value_file(const char *);
 
 /* key_value4.c */
-int G_update_key_value_file(const char *, const char *, const char *);
+void G_update_key_value_file(const char *, const char *, const char *);
 int G_lookup_key_value_from_file(const char *, const char *, char[], int);
 
 /* legal_name.c */

+ 15 - 7
lib/g3d/g3dkeys.c

@@ -93,7 +93,8 @@ int G3d_keySetInt(struct Key_Value *keys, const char *key, const int *i)
     char keyValStr[200];
 
     sprintf(keyValStr, "%d", *i);
-    return (G_set_key_value(key, keyValStr, keys) != 0);
+    G_set_key_value(key, keyValStr, keys);
+    return 1;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -103,7 +104,8 @@ int G3d_keySetDouble(struct Key_Value *keys, const char *key, const double *d)
     char keyValStr[200];
 
     sprintf(keyValStr, "%.50f", *d);
-    return (G_set_key_value(key, keyValStr, keys) != 0);
+    G_set_key_value(key, keyValStr, keys);
+    return 1;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -112,7 +114,8 @@ int
 G3d_keySetString(struct Key_Value *keys, const char *key,
 		 char *const *keyValStr)
 {
-    return (G_set_key_value(key, *keyValStr, keys) != 0);
+    G_set_key_value(key, *keyValStr, keys);
+    return 1;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -122,10 +125,15 @@ G3d_keySetValue(struct Key_Value *keys, const char *key, const char *val1,
 		const char *val2, int keyval1, int keyval2,
 		const int *keyvalVar)
 {
-    if (*keyvalVar == keyval1)
-	return (G_set_key_value(key, val1, keys) != 0);
-    if (*keyvalVar == keyval2)
-	return (G_set_key_value(key, val2, keys) != 0);
+    if (*keyvalVar == keyval1) {
+	G_set_key_value(key, val1, keys);
+	return 1;
+    }
+
+    if (*keyvalVar == keyval2) {
+	G_set_key_value(key, val2, keys);
+	return 1;
+    }
 
     G3d_error("G3d_keySetValue: wrong key value");
     return 0;

+ 1 - 6
lib/g3d/g3dwindowio.c

@@ -141,7 +141,6 @@ int G3d_readWindow(G3D_Region * window, const char *windowName)
     struct Cell_head win;
     struct Key_Value *windowKeys;
     char path[GPATH_MAX];
-    int status;
 
 
     if (windowName == NULL) {
@@ -170,11 +169,7 @@ int G3d_readWindow(G3D_Region * window, const char *windowName)
 	    return 0;
 	}
 
-	windowKeys = G_read_key_value_file(path, &status);
-	if (status != 0) {
-	    G3d_error("G3d_readWindow: Unable to open %s", path);
-	    return 0;
-	}
+	windowKeys = G_read_key_value_file(path);
 
 	if (!G3d_readWriteWindow(windowKeys, 1,
 				 &(window->proj), &(window->zone),

+ 3 - 13
lib/g3d/header.c

@@ -119,7 +119,6 @@ G3d_readHeader(G3D_Map * map, int *proj, int *zone, double *north,
 {
     struct Key_Value *headerKeys;
     char path[GPATH_MAX];
-    int status;
 
     G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
     if (access(path, R_OK) != 0) {
@@ -127,11 +126,7 @@ G3d_readHeader(G3D_Map * map, int *proj, int *zone, double *north,
 	return 0;
     }
 
-    headerKeys = G_read_key_value_file(path, &status);
-    if (status != 0) {
-	G3d_error("G3d_readHeader: Unable to open %s", path);
-	return 0;
-    }
+    headerKeys = G_read_key_value_file(path);
 
     if (!G3d_readWriteHeader(headerKeys, 1,
 			     proj, zone,
@@ -162,7 +157,6 @@ G3d_writeHeader(G3D_Map * map, int proj, int zone, double north, double south,
 {
     struct Key_Value *headerKeys;
     char path[GPATH_MAX];
-    int status;
 
     headerKeys = G_create_key_value();
 
@@ -182,15 +176,11 @@ G3d_writeHeader(G3D_Map * map, int proj, int zone, double north, double south,
 
     G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
     G3d_makeMapsetMapDirectory(map->fileName);
-    G_write_key_value_file(path, headerKeys, &status);
+    G_write_key_value_file(path, headerKeys);
 
     G_free_key_value(headerKeys);
 
-    if (status == 0)
-	return 1;
-
-    G3d_error("G3d_writeHeader: error writing header file %s", path);
-    return 0;
+    return 1;
 }
 
 /*---------------------------------------------------------------------------*/

+ 2 - 7
lib/gis/get_ellipse.c

@@ -66,7 +66,7 @@ static int get_ellipsoid_parameters(struct Key_Value *, double *, double *);
  */
 int G_get_ellipsoid_parameters(double *a, double *e2)
 {
-    int in_stat, stat;
+    int stat;
     char ipath[GPATH_MAX];
     struct Key_Value *proj_keys;
 
@@ -80,12 +80,7 @@ int G_get_ellipsoid_parameters(double *a, double *e2)
 	return 0;
     }
 
-    proj_keys = G_read_key_value_file(ipath, &in_stat);
-
-    if (in_stat != 0) {
-	G_fatal_error(_("Unable to open file %s in <%s>"),
-		      PROJECTION_FILE, PERMANENT);
-    }
+    proj_keys = G_read_key_value_file(ipath);
 
     stat = get_ellipsoid_parameters(proj_keys, a, e2);
 

+ 3 - 14
lib/gis/get_projinfo.c

@@ -24,7 +24,6 @@
  */
 struct Key_Value *G_get_projunits(void)
 {
-    int stat;
     struct Key_Value *in_units_keys;
     char path[GPATH_MAX];
 
@@ -36,12 +35,7 @@ struct Key_Value *G_get_projunits(void)
 	}
 	return NULL;
     }
-    in_units_keys = G_read_key_value_file(path, &stat);
-    if (stat != 0) {
-	G_warning(_("ERROR in reading <%s> file for location <%s>"),
-		  UNIT_FILE, G_location());
-	return NULL;
-    }
+    in_units_keys = G_read_key_value_file(path);
 
     return in_units_keys;
 }
@@ -53,7 +47,6 @@ struct Key_Value *G_get_projunits(void)
  */
 struct Key_Value *G_get_projinfo(void)
 {
-    int stat;
     struct Key_Value *in_proj_keys;
     char path[GPATH_MAX];
 
@@ -65,11 +58,7 @@ struct Key_Value *G_get_projinfo(void)
 	}
 	return NULL;
     }
-    in_proj_keys = G_read_key_value_file(path, &stat);
-    if (stat != 0) {
-	G_warning(_("ERROR in reading <%s> file for location <%s>"),
-		  PROJECTION_FILE, G_location());
-	return NULL;
-    }
+    in_proj_keys = G_read_key_value_file(path);
+
     return in_proj_keys;
 }

+ 23 - 60
lib/gis/key_value1.c

@@ -24,16 +24,12 @@
  */
 struct Key_Value *G_create_key_value(void)
 {
-    struct Key_Value *kv;
-
-    kv = (struct Key_Value *)G_malloc(sizeof(struct Key_Value));
-    if (kv == NULL)
-	return NULL;
+    struct Key_Value *kv = G_malloc(sizeof(struct Key_Value));
 
     kv->nitems = 0;
     kv->nalloc = 0;
-    kv->key = (char **)NULL;
-    kv->value = (char **)NULL;
+    kv->key = NULL;
+    kv->value = NULL;
 
     return kv;
 }
@@ -41,24 +37,17 @@ struct Key_Value *G_create_key_value(void)
 /*!
    \brief Set value for given key
 
-   If key has spaces in it, this will break the logic 
-   so rule is: NO SPACES IN key.
-
    \param[in] key key to be set up
    \param[in] value value for given key
    \param[in,out] kv Key_value structure to be modified
-
-   \return 0 no memory
-   \return 1 ok, but key was NULL or "" so ignored
-   \return 2 ok
  */
-int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
+
+void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
 {
     int n;
-    int size;
 
-    if (key == NULL || key == 0)
-	return 1;
+    if (!key)
+	return;
 
     for (n = 0; n < kv->nitems; n++)
 	if (strcmp(key, kv->key[n]) == 0)
@@ -66,54 +55,28 @@ int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
 
     if (n == kv->nitems) {
 	if (n >= kv->nalloc) {
-	    if (kv->nalloc <= 0) {
+	    size_t size;
+
+	    if (kv->nalloc <= 0)
 		kv->nalloc = 8;
-		size = kv->nalloc * sizeof(char *);
-		kv->key = (char **)G_malloc(size);
-		kv->value = (char **)G_malloc(size);
-	    }
-	    else {
+	    else
 		kv->nalloc *= 2;
-		size = kv->nalloc * sizeof(char *);
-		kv->key = (char **)G_realloc(kv->key, size);
-		kv->value = (char **)G_realloc(kv->value, size);
-	    }
-
-	    if (kv->key == NULL || kv->value == NULL) {
-		if (kv->key) {
-		    G_free(kv->key);
-		    kv->key = NULL;
-		}
-		if (kv->value) {
-		    G_free(kv->value);
-		    kv->value = NULL;
-		}
-		kv->nitems = kv->nalloc = 0;
-		return 0;
-	    }
+
+	    size = kv->nalloc * sizeof(char *);
+	    kv->key = G_realloc(kv->key, size);
+	    kv->value = G_realloc(kv->value, size);
 	}
-	kv->value[n] = NULL;
-	kv->key[n] = G_malloc(strlen(key) + 1);
-	if (kv->key[n] == NULL)
-	    return 0;
-	strcpy(kv->key[n], key);
+
+	kv->key[n] = G_store(key);
+	kv->value[n] = G_store(value);
 	kv->nitems++;
+	return;
     }
-    if (value == NULL)
-	size = 0;
-    else
-	size = strlen(value);
-    if (kv->value[n] != NULL)
+
+    if (kv->value[n])
 	G_free(kv->value[n]);
-    if (size > 0) {
-	kv->value[n] = G_malloc(size + 1);
-	if (kv->value[n] == NULL)
-	    return 0;
-	strcpy(kv->value[n], value);
-    }
-    else
-	kv->value[n] = NULL;
-    return 2;
+
+    kv->value[n] = value ? G_store(value) : NULL;
 }
 
 /*!

+ 1 - 4
lib/gis/key_value2.c

@@ -66,10 +66,7 @@ struct Key_Value *G_fread_key_value(FILE * fd)
 	*value++ = 0;
 	G_strip(key);
 	G_strip(value);
-	if (!G_set_key_value(key, value, kv)) {
-	    G_free_key_value(kv);
-	    return NULL;
-	}
+	G_set_key_value(key, value, kv);
     }
     return kv;
 }

+ 25 - 24
lib/gis/key_value3.c

@@ -14,30 +14,30 @@
  */
 
 #include <grass/gis.h>
+#include <grass/glocale.h>
 
 /*!
    \brief Write key/value pairs to file
 
    \param[in]  file filename for writing
    \param[in]  kv   Key_Value structure
-   \param[out] stat status (0 ok, -3 cannot open file, -4 error writing key/value)
 
    \return 0 success
    \return 1 error writing
  */
 
-int G_write_key_value_file(const char *file,
-			   const struct Key_Value *kv, int *stat)
+void G_write_key_value_file(const char *file,
+			    const struct Key_Value *kv)
 {
-    FILE *fd;
-
-    *stat = 0;
-    fd = fopen(file, "w");
-    if (fd == NULL)
-	*stat = -3;
-    else if (G_fwrite_key_value(fd, kv) != 0 || fclose(fd) == EOF)
-	*stat = -4;
-    return (*stat != 0);
+    FILE *fp = fopen(file, "w");
+    if (!fp)
+	G_fatal_error(_("Unable to open output file <%s>"), file);
+
+    if (G_fwrite_key_value(fp, kv) != 0)
+	G_fatal_error(_("Error writing file <%s>"), file);
+
+    if (fclose(fp) != 0)
+	G_fatal_error(_("Error closing output file <%s>"), file);
 }
 
 /*!
@@ -51,20 +51,21 @@ int G_write_key_value_file(const char *file,
    \return poiter to allocated Key_Value structure
    \return NULL on error
  */
-struct Key_Value *G_read_key_value_file(const char *file, int *stat)
+struct Key_Value *G_read_key_value_file(const char *file)
 {
-    FILE *fd;
+    FILE *fp;
     struct Key_Value *kv;
 
-    *stat = 0;
-    fd = fopen(file, "r");
-    if (fd == NULL) {
-	*stat = -1;
-	return NULL;
-    }
-    kv = G_fread_key_value(fd);
-    fclose(fd);
-    if (kv == NULL)
-	*stat = -2;
+    fp = fopen(file, "r");
+    if (!fp)
+	G_fatal_error(_("Unable to open input file <%s>"), file);
+
+    kv = G_fread_key_value(fp);
+    if (!kv)
+	G_fatal_error(_("Error reading file <%s>"), file);
+
+    if (fclose(fp) != 0)
+	G_fatal_error(_("Error closing input file <%s>"), file);
+
     return kv;
 }

+ 12 - 31
lib/gis/key_value4.c

@@ -22,31 +22,16 @@
    \param[in] file  filename to be updated
    \param[in] key   key value
    \param[in] value value to be updated
-
-   \return -1 can't open file for reading
-   \return -2 no memory for key,value info, file not modified
-   \return -3 can't open file for re-write
-   \return -4 error writing the file (might be damaged)
  */
-int G_update_key_value_file(const char *file, const char *key,
-			    const char *value)
+void G_update_key_value_file(const char *file,
+			     const char *key, const char *value)
 {
     struct Key_Value *kv;
-    int stat;
-
-    kv = G_read_key_value_file(file, &stat);
-    if (stat != 0)
-	return stat;
-
-    if (!G_set_key_value(key, value, kv)) {
-	G_free_key_value(kv);
-	return -2;
-    }
 
-    G_write_key_value_file(file, kv, &stat);
+    kv = G_read_key_value_file(file);
+    G_set_key_value(key, value, kv);
+    G_write_key_value_file(file, kv);
     G_free_key_value(kv);
-
-    return stat;
 }
 
 /*!
@@ -57,7 +42,6 @@ int G_update_key_value_file(const char *file, const char *key,
    \param[out] value value for key
    \param[in]  n     number of characters to be copied
 
-   \return <0 are file/memory errors
    \return 0 not found
    \return 1 ok
  */
@@ -65,22 +49,19 @@ int G_lookup_key_value_from_file(const char *file,
 				 const char *key, char value[], int n)
 {
     struct Key_Value *kv;
-    int stat;
     const char *v;
 
-    *value = 0;
-    kv = G_read_key_value_file(file, &stat);
-    if (stat != 0)
-	return stat;
+    *value = '\0';
+    kv = G_read_key_value_file(file);
 
     v = G_find_key_value(key, kv);
+
     if (v) {
 	strncpy(value, v, n);
-	value[n - 1] = 0;
-	stat = 1;
+	value[n - 1] = '\0';
     }
-    else
-	stat = 0;
+
     G_free_key_value(kv);
-    return stat;
+
+    return v ? 1 : 0;
 }

+ 2 - 7
lib/gis/make_loc.c

@@ -47,7 +47,6 @@ int G__make_location(const char *location_name,
 		     struct Key_Value *proj_units, FILE * report_file)
 {
     char path[GPATH_MAX];
-    int out_stat;
 
     /* Try to create the location directory, under the gisdbase. */
     sprintf(path, "%s/%s", G_gisdbase(), location_name);
@@ -70,16 +69,12 @@ int G__make_location(const char *location_name,
     /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
     if (proj_info != NULL) {
 	G__file_name(path, "", "PROJ_INFO", "PERMANENT");
-	G_write_key_value_file(path, proj_info, &out_stat);
-	if (out_stat != 0)
-	    return -2;
+	G_write_key_value_file(path, proj_info);
     }
 
     if (proj_units != NULL) {
 	G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
-	G_write_key_value_file(path, proj_units, &out_stat);
-	if (out_stat != 0)
-	    return -2;
+	G_write_key_value_file(path, proj_units);
     }
 
     return 0;

+ 6 - 17
lib/raster/closecell.c

@@ -36,7 +36,7 @@
 static int close_old(int);
 static int close_new(int, int);
 
-static int write_fp_format(int fd);
+static void write_fp_format(int fd);
 
 /*!
  * \brief Close a raster map
@@ -287,11 +287,7 @@ static int close_new_gdal(int fd, int ok)
 	close(cell_fd);
 
 	if (fcb->map_type != CELL_TYPE) {	/* floating point map */
-	    if (write_fp_format(fd) != 0) {
-		G_warning(_("Error writing floating point format file for map %s"),
-			  fcb->name);
-		stat = -1;
-	    }
+	    write_fp_format(fd);
 
 	    /* write 0-length fcell file */
 	    G__make_mapset_element("fcell");
@@ -399,11 +395,7 @@ static int close_new(int fd, int ok)
 	if (fcb->map_type != CELL_TYPE) {	/* floating point map */
 	    int cell_fd;
 
-	    if (write_fp_format(fd) != 0) {
-		G_warning(_("Error writing floating point format file for map <%s>"),
-			  fcb->name);
-		stat = -1;
-	    }
+	    write_fp_format(fd);
 
 	    /* now write 0-length cell file */
 	    G__make_mapset_element("cell");
@@ -476,16 +468,15 @@ static int close_new(int fd, int ok)
 }
 
 /* returns 0 on success, 1 on failure */
-static int write_fp_format(int fd)
+static void write_fp_format(int fd)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
     struct Key_Value *format_kv;
     char path[GPATH_MAX];
-    int stat;
 
     if (fcb->map_type == CELL_TYPE) {
 	G_warning(_("unable to write f_format file for CELL maps"));
-	return 0;
+	return;
     }
     format_kv = G_create_key_value();
     if (fcb->map_type == FCELL_TYPE)
@@ -500,9 +491,7 @@ static int write_fp_format(int fd)
 
     G__make_mapset_element_misc("cell_misc", fcb->name);
     G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset);
-    G_write_key_value_file(path, format_kv, &stat);
+    G_write_key_value_file(path, format_kv);
 
     G_free_key_value(format_kv);
-
-    return stat;
 }

+ 1 - 4
lib/raster/opencell.c

@@ -823,7 +823,6 @@ RASTER_MAP_TYPE Rast__check_fp_type(const char *name, const char *mapset)
 {
     char path[GPATH_MAX];
     struct Key_Value *format_keys;
-    int in_stat;
     const char *str, *str1;
     RASTER_MAP_TYPE map_type;
     const char *xmapset;
@@ -838,9 +837,7 @@ RASTER_MAP_TYPE Rast__check_fp_type(const char *name, const char *mapset)
     if (access(path, 0) != 0)
 	G_fatal_error(_("Unable to find '%s'"), path);
 
-    format_keys = G_read_key_value_file(path, &in_stat);
-    if (in_stat != 0)
-	G_fatal_error(_("Unable to open '%s'"), path);
+    format_keys = G_read_key_value_file(path);
 
     if ((str = G_find_key_value("type", format_keys)) != NULL) {
 	if (strcmp(str, "double") == 0)

+ 2 - 3
raster/r.colors/main.c

@@ -81,12 +81,11 @@ static char *rules_descriptions(void)
     int result_len = 0;
     int result_max = 2000;
     char *result = G_malloc(result_max);
-    int stat;
     int i;
 
     sprintf(path, "%s/etc/colors.desc", G_gisbase());
-    kv = G_read_key_value_file(path, &stat);
-    if (!kv || stat < 0)
+    kv = G_read_key_value_file(path);
+    if (!kv)
 	return NULL;
 
     for (i = 0; i < nrules; i++) {