GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /********************************************************************** 00003 * 00004 * G3d_readHistory (name, mapset, hist) 00005 * char *name name of map 00006 * char *mapset mapset that map belongs to 00007 * struct History *hist structure to hold history info 00008 * 00009 * Reads the history information associated with map layer "map" 00010 * in mapset "mapset" into the structure "hist". 00011 * 00012 * returns: 0 if successful 00013 * -1 on fail 00014 * 00015 * note: a warning message is printed if the file is incorrect 00016 * 00017 ********************************************************************** 00018 * 00019 * G3d_writeHistory (name, hist) 00020 * char *name name of map 00021 * struct History *hist structure holding history info 00022 * 00023 * Writes the history information associated with map layer "map" 00024 * into current from the structure "hist". 00025 * 00026 * returns: 0 if successful 00027 * -1 on fail 00028 **********************************************************************/ 00029 00030 #include <string.h> 00031 #include <stdio.h> 00032 #include <stdlib.h> 00033 #include <grass/glocale.h> 00034 #include "G3d_intern.h" 00035 00036 /*simple error message */ 00037 void SimpleErrorMessage(FILE * fd, const char *name, const char *mapset) 00038 { 00039 if (fd != NULL) 00040 fclose(fd); 00041 00042 G_warning(_("can't get history information for [%s] in mapset [%s]"), 00043 name, mapset); 00044 return; 00045 } 00046 00063 int G3d_readHistory(const char *name, const char *mapset, 00064 struct History *hist) 00065 /* This function is adapted from G_read_history */ 00066 { 00067 FILE *fd; 00068 char buff[1024], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 00069 00070 G_zero(hist, sizeof(struct History)); 00071 00072 /*this construct takes care of the correct history file path */ 00073 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00074 sprintf(buff, "%s/%s", G3D_DIRECTORY, xname); 00075 sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist@mapset */ 00076 } 00077 else { 00078 sprintf(buff, "%s/%s", G3D_DIRECTORY, name); 00079 sprintf(buf2, "%s", G3D_HISTORY_ELEMENT); 00080 } 00081 00082 if (!(fd = G_fopen_old(buff, buf2, mapset))) 00083 return -2; 00084 00085 00086 if (!G_getl(hist->mapid, sizeof(hist->mapid), fd)) { 00087 SimpleErrorMessage(fd, name, mapset); 00088 return -1; 00089 } 00090 G_ascii_check(hist->mapid); 00091 00092 if (!G_getl(hist->title, sizeof(hist->title), fd)) { 00093 SimpleErrorMessage(fd, name, mapset); 00094 return -1; 00095 } 00096 G_ascii_check(hist->title); 00097 00098 if (!G_getl(hist->mapset, sizeof(hist->mapset), fd)) { 00099 SimpleErrorMessage(fd, name, mapset); 00100 return -1; 00101 } 00102 G_ascii_check(hist->mapset); 00103 00104 if (!G_getl(hist->creator, sizeof(hist->creator), fd)) { 00105 SimpleErrorMessage(fd, name, mapset); 00106 return -1; 00107 } 00108 G_ascii_check(hist->creator); 00109 00110 if (!G_getl(hist->maptype, sizeof(hist->maptype), fd)) { 00111 SimpleErrorMessage(fd, name, mapset); 00112 return -1; 00113 } 00114 G_ascii_check(hist->maptype); 00115 00116 if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd)) { 00117 SimpleErrorMessage(fd, name, mapset); 00118 return -1; 00119 } 00120 G_ascii_check(hist->datsrc_1); 00121 00122 if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd)) { 00123 SimpleErrorMessage(fd, name, mapset); 00124 return -1; 00125 } 00126 G_ascii_check(hist->datsrc_2); 00127 00128 if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd)) { 00129 SimpleErrorMessage(fd, name, mapset); 00130 return -1; 00131 } 00132 G_ascii_check(hist->keywrd); 00133 00134 hist->edlinecnt = 0; 00135 while ((hist->edlinecnt < MAXEDLINES) && 00136 (G_getl 00137 (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) { 00138 G_ascii_check(hist->edhist[hist->edlinecnt]); 00139 hist->edlinecnt++; 00140 } 00141 00142 fclose(fd); 00143 return 0; 00144 } 00145 00146 00162 int G3d_writeHistory(const char *name, struct History *hist) 00163 /* This function is adapted from G_write_history */ 00164 { 00165 FILE *fd; 00166 int i; 00167 char buf[200], buf2[200], xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 00168 00169 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00170 sprintf(buf, "%s/%s", G3D_DIRECTORY, xname); 00171 sprintf(buf2, "%s@%s", G3D_HISTORY_ELEMENT, xmapset); /* == hist@mapset */ 00172 } 00173 else { 00174 sprintf(buf, "%s/%s", G3D_DIRECTORY, name); 00175 sprintf(buf2, "%s", G3D_HISTORY_ELEMENT); 00176 } 00177 00178 if (!(fd = G_fopen_new(buf, buf2))) 00179 return -1; 00180 00181 fprintf(fd, "%s\n", hist->mapid); 00182 fprintf(fd, "%s\n", hist->title); 00183 fprintf(fd, "%s\n", hist->mapset); 00184 fprintf(fd, "%s\n", hist->creator); 00185 fprintf(fd, "%s\n", hist->maptype); 00186 fprintf(fd, "%s\n", hist->datsrc_1); 00187 fprintf(fd, "%s\n", hist->datsrc_2); 00188 fprintf(fd, "%s\n", hist->keywrd); 00189 00190 for (i = 0; i < hist->edlinecnt; i++) 00191 fprintf(fd, "%s\n", hist->edhist[i]); 00192 00193 fclose(fd); 00194 return 0; 00195 }