00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include <string.h>
00056 #include <grass/gis.h>
00057 #include <grass/glocale.h>
00058
00059
00075 int G_read_history(const char *name, const char *mapset, struct History *hist)
00076 {
00077 FILE *fd;
00078
00079 G_zero(hist, sizeof(struct History));
00080 fd = G_fopen_old("hist", name, mapset);
00081 if (!fd)
00082 goto error;
00083
00084
00085 if (!G_getl(hist->mapid, sizeof(hist->mapid), fd))
00086 goto error;
00087 G_ascii_check(hist->mapid);
00088
00089 if (!G_getl(hist->title, sizeof(hist->title), fd))
00090 goto error;
00091 G_ascii_check(hist->title);
00092
00093 if (!G_getl(hist->mapset, sizeof(hist->mapset), fd))
00094 goto error;
00095 G_ascii_check(hist->mapset);
00096
00097 if (!G_getl(hist->creator, sizeof(hist->creator), fd))
00098 goto error;
00099 G_ascii_check(hist->creator);
00100
00101 if (!G_getl(hist->maptype, sizeof(hist->maptype), fd))
00102 goto error;
00103 G_ascii_check(hist->maptype);
00104
00105 if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd))
00106 goto error;
00107 G_ascii_check(hist->datsrc_1);
00108
00109 if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd))
00110 goto error;
00111 G_ascii_check(hist->datsrc_2);
00112
00113 if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd))
00114 goto error;
00115 G_ascii_check(hist->keywrd);
00116
00117 hist->edlinecnt = 0;
00118 while ((hist->edlinecnt < MAXEDLINES) &&
00119 (G_getl
00120 (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
00121 G_ascii_check(hist->edhist[hist->edlinecnt]);
00122 hist->edlinecnt++;
00123 }
00124
00125
00126 fclose(fd);
00127 return 0;
00128
00129 error:
00130 if (fd != NULL)
00131 fclose(fd);
00132 G_warning(_("can't get history information for [%s] in mapset [%s]"),
00133 name, mapset);
00134 return -1;
00135 }
00136
00137
00153 int G_write_history(const char *name, struct History *hist)
00154 {
00155 FILE *fd;
00156 int i;
00157
00158 fd = G_fopen_new("hist", name);
00159 if (!fd)
00160 goto error;
00161
00162 fprintf(fd, "%s\n", hist->mapid);
00163 fprintf(fd, "%s\n", hist->title);
00164 fprintf(fd, "%s\n", hist->mapset);
00165 fprintf(fd, "%s\n", hist->creator);
00166 fprintf(fd, "%s\n", hist->maptype);
00167 fprintf(fd, "%s\n", hist->datsrc_1);
00168 fprintf(fd, "%s\n", hist->datsrc_2);
00169 fprintf(fd, "%s\n", hist->keywrd);
00170
00171 for (i = 0; i < hist->edlinecnt; i++)
00172 fprintf(fd, "%s\n", hist->edhist[i]);
00173
00174 fclose(fd);
00175 return 0;
00176
00177 error:
00178 if (fd)
00179 fclose(fd);
00180 G_warning(_("can't write history information for [%s]"), name);
00181 return -1;
00182 }
00183
00184
00185
00202 int G_short_history(const char *name, const char *type, struct History *hist)
00203 {
00204 strncpy(hist->mapid, G_date(), RECORD_LEN);
00205 strncpy(hist->title, name, RECORD_LEN);
00206 strncpy(hist->mapset, G_mapset(), RECORD_LEN);
00207 strncpy(hist->creator, G_whoami(), RECORD_LEN);
00208 strncpy(hist->maptype, type, RECORD_LEN);
00209
00210 sprintf(hist->keywrd, "generated by %s", G_program_name());
00211 strcpy(hist->datsrc_1, "");
00212 strcpy(hist->datsrc_2, "");
00213 hist->edlinecnt = 0;
00214
00215 return 1;
00216 }
00217
00254 int G_command_history(struct History *hist)
00255 {
00256 int j, cmdlen;
00257 char *cmdlin;
00258
00259 cmdlin = G_recreate_command();
00260 cmdlen = strlen(cmdlin);
00261
00262 if (hist->edlinecnt > MAXEDLINES - 2) {
00263 G_warning(_("Not enough room in history file to record command line."));
00264 return 1;
00265 }
00266
00267 if (hist->edlinecnt > 0) {
00268 strcpy(hist->edhist[hist->edlinecnt], "");
00269 hist->edlinecnt++;
00270 }
00271
00272 if (cmdlen < 70) {
00273 sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
00274 hist->edlinecnt++;
00275 }
00276 else {
00277 j = 0;
00278 while ((cmdlen - j) > 70) {
00279 strncpy(hist->edhist[hist->edlinecnt], &cmdlin[j], 68);
00280 hist->edhist[hist->edlinecnt][68] = '\0';
00281 strcat(hist->edhist[hist->edlinecnt], "\\");
00282 j += 68;
00283 hist->edlinecnt++;
00284 if (hist->edlinecnt > MAXEDLINES - 2) {
00285 G_warning(_("Not enough room in history file for command line (truncated)."));
00286 return 2;
00287 }
00288 }
00289 if ((cmdlen - j) > 0) {
00290 strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);
00291 hist->edlinecnt++;
00292 }
00293 }
00294 return 0;
00295 }