GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
hist.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/hist.c
3
4 \brief Vector library - history manipulation
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Radim Blazek
12 */
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include <grass/vector.h>
18
19/*!
20 \brief Write command info to history file
21
22 \param Map pointer to Map_info structure
23
24 \return 0 on success
25 \return -1 error
26 */
28{
29 char *cmd, buf[GPATH_MAX];
30
31 G_debug(3, "Vect_hist_command()");
32
34
35 if (0 > Vect_hist_write(Map, "COMMAND: "))
36 return -1;
37 if (0 > Vect_hist_write(Map, cmd))
38 return -1;
39 if (0 > Vect_hist_write(Map, "\n"))
40 return -1;
41
42 snprintf(buf, sizeof(buf), "GISDBASE: %s\n", G_gisdbase()); /* Needed ? */
43 if (0 > Vect_hist_write(Map, buf))
44 return -1;
45
46 snprintf(buf, sizeof(buf), "LOCATION: %s MAPSET: %s USER: %s DATE: %s\n",
47 G_location(), G_mapset(), G_whoami(), G_date()); /* Needed ? */
48 if (0 > Vect_hist_write(Map, buf))
49 return -1;
50
51 return 0;
52}
53
54/*!
55 \brief Write string to history file
56
57 \param Map pointer to Map_info structure
58 \param str string to write
59
60 \return the number of characters printed
61 \return -1 on error
62 */
63int Vect_hist_write(struct Map_info *Map, const char *str)
64{
65 int ret;
66
67 G_debug(5, "Vect_hist_write(): %s", str);
68 ret = 0;
69 if (Map->hist_fp) {
70 ret = fprintf(Map->hist_fp, "%s", str);
71 fflush(Map->hist_fp);
72 }
73
74 return ret;
75}
76
77/*!
78 \brief Reads one line from history file without newline character
79
80 \param[out] s buffer, allocated space must be size+1
81 \param size maximum number of character
82 \param Map vector map
83
84 \return return s on success
85 \return NULL on error
86 \return EOF end of file
87 */
88char *Vect_hist_read(char *s, int size, struct Map_info *Map)
89{
90 int ret;
91
92 G_debug(5, "Vect_hist_read()");
93
94 if (Map->hist_fp == NULL)
95 return NULL; /* OK for shapefile etc. */
96
97 ret = G_getl2(s, size, Map->hist_fp);
98
99 if (ret == 1)
100 return s;
101
102 return NULL;
103}
104
105/*!
106 \brief Rewind history file
107
108 \param Map vector map
109
110 \return void
111 */
113{
114 G_debug(3, "Vect_hist_rewind()");
115
116 if (Map->hist_fp != NULL)
117 rewind(Map->hist_fp);
118}
119
120/*!
121 \brief Copy history from one map to another
122
123 \param In input vector map
124 \param[out] Out output vector map
125
126 \return 0 on success
127 \return -1 on error
128 */
129int Vect_hist_copy(struct Map_info *In, struct Map_info *Out)
130{
131 size_t red, ret;
132 char buf[1000];
133
134 G_debug(3, "Vect_hist_copy()");
135
136 if (In->hist_fp == NULL)
137 return 0; /* This is correct (old hist doesn't exist) */
138 if (Out->hist_fp == NULL)
139 return -1;
140
141 /* skip empty old hist */
142 G_fseek(In->hist_fp, (long)0, SEEK_END);
143 if (G_ftell(In->hist_fp) == 0)
144 return 0;
145
146 G_fseek(Out->hist_fp, (long)0, SEEK_END);
147 rewind(In->hist_fp);
148
149 while ((red = fread(buf, sizeof(char), sizeof(char) * 1000, In->hist_fp))) {
150 if (!(ret = fwrite(buf, sizeof(char), red, Out->hist_fp))) {
151 return (-1);
152 }
153 fflush(Out->hist_fp);
154 }
155
156 /* In ends with \n ? */
157 G_fseek(In->hist_fp, (long)-1, SEEK_END);
158 if (fread(buf, sizeof(char), sizeof(char), In->hist_fp) != 1) {
159 return -1;
160 }
161
162 if (buf[0] != '\n') {
163 Vect_hist_write(Out, "\n");
164 }
165
166 /* Separator */
167 Vect_hist_write(Out, "-----------------------------------------------------"
168 "----------------------------\n");
169 return (0);
170}
#define NULL
Definition ccmath.h:32
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition getl.c:58
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition gis/seek.c:48
const char * G_gisdbase(void)
Get name of top level database directory.
Definition gisdbase.c:22
off_t G_ftell(FILE *)
Get the current file position of the stream.
Definition gis/seek.c:27
char * G_recreate_command(void)
Creates command to run non-interactive.
Definition parser.c:838
const char * G_whoami(void)
Gets user's name.
Definition gis/whoami.c:32
const char * G_location(void)
Get current location name.
Definition location.c:30
const char * G_date(void)
Current date and time.
Definition date.c:24
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
#define GPATH_MAX
Definition gis.h:196
int Vect_hist_write(struct Map_info *Map, const char *str)
Write string to history file.
Definition hist.c:63
char * Vect_hist_read(char *s, int size, struct Map_info *Map)
Reads one line from history file without newline character.
Definition hist.c:88
int Vect_hist_copy(struct Map_info *In, struct Map_info *Out)
Copy history from one map to another.
Definition hist.c:129
void Vect_hist_rewind(struct Map_info *Map)
Rewind history file.
Definition hist.c:112
int Vect_hist_command(struct Map_info *Map)
Write command info to history file.
Definition hist.c:27
Vector map info.
FILE * hist_fp
History file.