~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/gis/history.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/**********************************************************************
3
 
 *
4
 
 *  G_read_history (name, mapset, phist)
5
 
 *      char *name                   name of map
6
 
 *      char *mapset                 mapset that map belongs to
7
 
 *      struct History *phist        structure to hold history info
8
 
 *
9
 
 *  Reads the history information associated with map layer "map"
10
 
 *  in mapset "mapset" into the structure "phist".
11
 
 *
12
 
 *   returns:    0  if successful
13
 
 *              -1  on fail
14
 
 *
15
 
 *  note:   a warning message is printed if the file is incorrect
16
 
 *
17
 
 **********************************************************************
18
 
 *
19
 
 *  G_write_history (name, phist)
20
 
 *      char *name                   name of map
21
 
 *      struct History *phist        structure holding history info
22
 
 *
23
 
 *  Writes the history information associated with map layer "map"
24
 
 *  into current from the structure "phist".
25
 
 *
26
 
 *   returns:    0  if successful
27
 
 *              -1  on fail
28
 
 ***********************************************************************
29
 
 *
30
 
 *  G_short_history (name, type, hist)
31
 
 *     char *name             name of cell file
32
 
 *     char *type             type of cell file
33
 
 *     struct History *hist   History structure to be filled in
34
 
 *
35
 
 *  Puts local information like time and date, user's name, map name,
36
 
 *  and current mapset name into the hist structure
37
 
 *
38
 
 *  NOTE: use G_write_history() to write the structure.
39
 
 **********************************************************************
40
 
 *
41
 
 *  G_command_history (hist)
42
 
 *     struct History *hist   History structure to be filled in
43
 
 *
44
 
 *  Appends (parsed) command line to history structure's comments
45
 
 *
46
 
 * Returns:
47
 
 *     0 success
48
 
 *     1 failure (history file full, no change)
49
 
 *     2 failure (history file full, added as much as we could)
50
 
 *
51
 
 *  NOTE: initialize structure with G_short_history() first.
52
 
 *  NOTE: use G_write_history() to write the structure.
53
 
 **********************************************************************/
54
 
 
55
 
#include <string.h>
56
 
#include <grass/gis.h>
57
 
#include <grass/glocale.h>
58
 
 
59
 
 
60
 
/*!
61
 
 * \brief read raster history file
62
 
 *
63
 
 * This routine reads the history file for
64
 
 * the raster map <b>name</b> in <b>mapset</b> into the <b>history</b>
65
 
 * structure.
66
 
 * A diagnostic message is printed and -1 is returned if there is an error
67
 
 * reading the history file. Otherwise, 0 is returned.
68
 
 *
69
 
 *  \param name
70
 
 *  \param mapset
71
 
 *  \param history
72
 
 *  \return int
73
 
 */
74
 
 
75
 
int G_read_history(const char *name, const char *mapset, struct History *hist)
76
 
{
77
 
    FILE *fd;
78
 
 
79
 
    G_zero(hist, sizeof(struct History));
80
 
    fd = G_fopen_old("hist", name, mapset);
81
 
    if (!fd)
82
 
        goto error;
83
 
 
84
 
 
85
 
    if (!G_getl(hist->mapid, sizeof(hist->mapid), fd))
86
 
        goto error;
87
 
    G_ascii_check(hist->mapid);
88
 
 
89
 
    if (!G_getl(hist->title, sizeof(hist->title), fd))
90
 
        goto error;
91
 
    G_ascii_check(hist->title);
92
 
 
93
 
    if (!G_getl(hist->mapset, sizeof(hist->mapset), fd))
94
 
        goto error;
95
 
    G_ascii_check(hist->mapset);
96
 
 
97
 
    if (!G_getl(hist->creator, sizeof(hist->creator), fd))
98
 
        goto error;
99
 
    G_ascii_check(hist->creator);
100
 
 
101
 
    if (!G_getl(hist->maptype, sizeof(hist->maptype), fd))
102
 
        goto error;
103
 
    G_ascii_check(hist->maptype);
104
 
 
105
 
    if (!G_getl(hist->datsrc_1, sizeof(hist->datsrc_1), fd))
106
 
        goto error;
107
 
    G_ascii_check(hist->datsrc_1);
108
 
 
109
 
    if (!G_getl(hist->datsrc_2, sizeof(hist->datsrc_2), fd))
110
 
        goto error;
111
 
    G_ascii_check(hist->datsrc_2);
112
 
 
113
 
    if (!G_getl(hist->keywrd, sizeof(hist->keywrd), fd))
114
 
        goto error;
115
 
    G_ascii_check(hist->keywrd);
116
 
 
117
 
    hist->edlinecnt = 0;
118
 
    while ((hist->edlinecnt < MAXEDLINES) &&
119
 
           (G_getl
120
 
            (hist->edhist[hist->edlinecnt], sizeof(hist->edhist[0]), fd))) {
121
 
        G_ascii_check(hist->edhist[hist->edlinecnt]);
122
 
        hist->edlinecnt++;
123
 
    }
124
 
 
125
 
 
126
 
    fclose(fd);
127
 
    return 0;
128
 
 
129
 
  error:
130
 
    if (fd != NULL)
131
 
        fclose(fd);
132
 
    G_warning(_("can't get history information for [%s] in mapset [%s]"),
133
 
              name, mapset);
134
 
    return -1;
135
 
}
136
 
 
137
 
 
138
 
/*!
139
 
 * \brief write raster history file
140
 
 *
141
 
 * This routine writes the history file for the raster map
142
 
 * <b>name</b> in the current mapset from the <b>history</b> structure.
143
 
 * A diagnostic message is printed and -1 is returned if there is an error
144
 
 * writing the history file. Otherwise, 0 is returned.
145
 
 * <b>Note.</b> The <b>history</b> structure should first be initialized
146
 
 * using <i>G_short_history.</i>
147
 
 *
148
 
 *  \param name
149
 
 *  \param history
150
 
 *  \return int
151
 
 */
152
 
 
153
 
int G_write_history(const char *name, struct History *hist)
154
 
{
155
 
    FILE *fd;
156
 
    int i;
157
 
 
158
 
    fd = G_fopen_new("hist", name);
159
 
    if (!fd)
160
 
        goto error;
161
 
 
162
 
    fprintf(fd, "%s\n", hist->mapid);
163
 
    fprintf(fd, "%s\n", hist->title);
164
 
    fprintf(fd, "%s\n", hist->mapset);
165
 
    fprintf(fd, "%s\n", hist->creator);
166
 
    fprintf(fd, "%s\n", hist->maptype);
167
 
    fprintf(fd, "%s\n", hist->datsrc_1);
168
 
    fprintf(fd, "%s\n", hist->datsrc_2);
169
 
    fprintf(fd, "%s\n", hist->keywrd);
170
 
 
171
 
    for (i = 0; i < hist->edlinecnt; i++)
172
 
        fprintf(fd, "%s\n", hist->edhist[i]);
173
 
 
174
 
    fclose(fd);
175
 
    return 0;
176
 
 
177
 
  error:
178
 
    if (fd)
179
 
        fclose(fd);
180
 
    G_warning(_("can't write history information for [%s]"), name);
181
 
    return -1;
182
 
}
183
 
 
184
 
 
185
 
 
186
 
/*!
187
 
 * \brief initialize history structure
188
 
 *
189
 
 * This routine initializes the
190
 
 * <b>history</b> structure, recording the date, user, module name and the
191
 
 * raster map <b>name</b> structure. The <b>type</b> is an anachronism from
192
 
 * earlier versions of GRASS and should be specified as "raster".
193
 
 * <b>Note.</b> This routine only initializes the data structure. It does not
194
 
 * write the history file.
195
 
 *
196
 
 *  \param name
197
 
 *  \param type
198
 
 *  \param history
199
 
 *  \return int
200
 
 */
201
 
 
202
 
int G_short_history(const char *name, const char *type, struct History *hist)
203
 
{
204
 
    strncpy(hist->mapid, G_date(), RECORD_LEN);
205
 
    strncpy(hist->title, name, RECORD_LEN);
206
 
    strncpy(hist->mapset, G_mapset(), RECORD_LEN);
207
 
    strncpy(hist->creator, G_whoami(), RECORD_LEN);
208
 
    strncpy(hist->maptype, type, RECORD_LEN);
209
 
 
210
 
    sprintf(hist->keywrd, "generated by %s", G_program_name());
211
 
    strcpy(hist->datsrc_1, "");
212
 
    strcpy(hist->datsrc_2, "");
213
 
    hist->edlinecnt = 0;
214
 
 
215
 
    return 1;
216
 
}
217
 
 
218
 
/*!
219
 
 * \brief Save command line to raster history structure
220
 
 *
221
 
 * This routine takes an existing (run <i>G_short_history first</i>) history
222
 
 *  structure and adds the command line to the end of the comments array, as
223
 
 *  cleaned & expanded by the parser.
224
 
 *
225
 
 * History file is limited to [80]x[50], as defined in include/gis.h
226
 
 *
227
 
 * * First version had for loops of [i][j] character assignments and ending
228
 
 *   nulls, but using the string libraries is cleaner and less bug prone.
229
 
 * * Second version had white space detection, intelligent wrapping, and
230
 
 *   indentation of continued lines, but this proved a pain in the neck for 
231
 
 *   things like r.patch which can have long strings without any
232
 
 *   parser-acceptable breaks.
233
 
 * * This is MK-III, simplified, but that's good: it's cut & paste-able.
234
 
 *
235
 
 *  NOTE: use G_write_history() to write the structure.
236
 
 *
237
 
 * Sample Usage:
238
 
 *
239
 
 *   struct History history;
240
 
 *   G_short_history(rasterfile, "raster", &history);
241
 
 *   G_command_history(&history);
242
 
 *   G_write_history(rasterfile, &history);
243
 
 *
244
 
 * Returns:
245
 
 *     0 success
246
 
 *     1 failure (history file full, no change)
247
 
 *     2 failure (history file full, added as much as we could)
248
 
 *
249
 
 * \param history
250
 
 * \return int
251
 
 *
252
 
 */
253
 
 
254
 
int G_command_history(struct History *hist)
255
 
{
256
 
    int j, cmdlen;
257
 
    char *cmdlin;
258
 
 
259
 
    cmdlin = G_recreate_command();
260
 
    cmdlen = strlen(cmdlin);
261
 
 
262
 
    if (hist->edlinecnt > MAXEDLINES - 2) {
263
 
        G_warning(_("Not enough room in history file to record command line."));
264
 
        return 1;
265
 
    }
266
 
 
267
 
    if (hist->edlinecnt > 0) {  /* add a blank line if preceding history exists */
268
 
        strcpy(hist->edhist[hist->edlinecnt], "");
269
 
        hist->edlinecnt++;
270
 
    }
271
 
 
272
 
    if (cmdlen < 70) {          /* ie if it will fit on a single line */
273
 
        sprintf(hist->edhist[hist->edlinecnt], G_recreate_command());
274
 
        hist->edlinecnt++;
275
 
    }
276
 
    else {                      /* multi-line required */
277
 
        j = 0;                  /* j is the current position in the command line string */
278
 
        while ((cmdlen - j) > 70) {
279
 
            strncpy(hist->edhist[hist->edlinecnt], &cmdlin[j], 68);
280
 
            hist->edhist[hist->edlinecnt][68] = '\0';
281
 
            strcat(hist->edhist[hist->edlinecnt], "\\");
282
 
            j += 68;
283
 
            hist->edlinecnt++;
284
 
            if (hist->edlinecnt > MAXEDLINES - 2) {
285
 
                G_warning(_("Not enough room in history file for command line (truncated)."));
286
 
                return 2;
287
 
            }
288
 
        }
289
 
        if ((cmdlen - j) > 0) { /* ie anything left */
290
 
            strcpy(hist->edhist[hist->edlinecnt], &cmdlin[j]);
291
 
            hist->edlinecnt++;
292
 
        }
293
 
    }
294
 
    return 0;
295
 
}