GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
map_list.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * MODULE: gis library
4  *
5  * AUTHOR(S): Original author CERL, probably Dave Gerdes.
6  * Update to GRASS 5.7 Radim Blazek.
7  *
8  * PURPOSE: Lower level functions for reading and manipulating integer list
9  *
10  * COPYRIGHT: (C) 2001 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 
18 #include <stdlib.h>
19 #include <grass/gis.h>
20 #include <grass/temporal.h>
21 
22 /**
23  * \brief Free allocated memory of an integer list
24  *
25  * \param list The pointer to an integer list
26  *
27  * */
29 {
30  if (list->values) {
31  int i;
32 
33  for (i = 0; i < list->n_values; i++) {
34  if (list->values[i]->name)
35  G_free(list->values[i]->name);
36  if (list->values[i]->mapset)
37  G_free(list->values[i]->mapset);
38  G_free(list->values[i]);
39  }
40  G_free(list->values);
41  }
42  G_free(list);
43 }
44 
45 /**
46  * \brief Return a new integer list.
47  *
48  * G_fatal_error() will be invoked by the
49  * allocation function.
50  *
51  * \return list The pointer to a new allocated integer list
52  *
53  * */
55 {
57 
58  list->values = NULL;
60  return list;
61 }
62 
63 /**
64  * \brief Init a tgisMapList and free allocated memory
65  *
66  * \param list The pointer to a tgisMapList
67  *
68  * */
70 {
71  if (list->values) {
72  int i;
73 
74  for (i = 0; i < list->n_values; i++) {
75  if (list->values[i]->name)
76  G_free(list->values[i]->name);
77  if (list->values[i]->mapset)
78  G_free(list->values[i]->mapset);
79  G_free(list->values[i]);
80  }
81  G_free(list->values);
82  }
83 
84  list->values = NULL;
85  list->n_values = 0;
86  list->alloc_values = 0;
87 }
88 
89 /**
90  * \brief Add a map to tgisMapList
91  *
92  * This function adds a tgisMap to the list but does not check for duplicates.
93  * In case reallocation fails, G_fatal_error() will be invoked by the
94  * allocation function.
95  *
96  * The content of the map will not be copied, the pointer
97  *to the map will be stored.
98  *
99  * \param list The tgisMapList pointer
100  * \param map A pointer to a tgisMap struct that should be added
101  *
102  * */
104 {
105  if (list->n_values == list->alloc_values) {
106  size_t size = (list->n_values + 1000) * sizeof(tgisMap *);
107  void *p = G_realloc((void *)list->values, size);
108 
109  list->values = (tgisMap **)p;
110  list->alloc_values = list->n_values + 1000;
111  }
112 
113  list->values[list->n_values] = map;
114  list->n_values++;
115 }
116 
117 /**
118  * \brief Insert map information into tgisMapList
119  *
120  * This function allocates a tgisMap, fills it with the provided information
121  * and adds it to the list.
122  * In case allocation fails, G_fatal_error() will be invoked by the
123  * allocation function.
124  *
125  * All arguments are deep copied to the new allocated tgisMap struct.
126  *
127  * \param list The tgisMapList pointer
128  * \param name The name of the map
129  * \param mapset The name of the mapset
130  * \param ts A pointer to the timestamp of the map
131  *
132  * */
133 void tgis_map_list_insert(tgisMapList *list, char *name, char *mapset,
134  struct TimeStamp *ts)
135 {
136  tgisMap *map = G_calloc(1, sizeof(tgisMap));
137 
138  map->name = G_store(name);
139  map->mapset = G_store(mapset);
140 
141  if (ts->count == 1)
142  G_set_timestamp(&(map->ts), &(ts->dt[0]));
143  if (ts->count == 2)
144  G_set_timestamp_range(&(map->ts), &(ts->dt[0]), &(ts->dt[1]));
145 
146  tgis_map_list_add(list, map);
147 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
void G_set_timestamp(struct TimeStamp *, const struct DateTime *)
#define G_calloc(m, n)
Definition: defs/gis.h:95
#define G_malloc(n)
Definition: defs/gis.h:94
void G_set_timestamp_range(struct TimeStamp *, const struct DateTime *, const struct DateTime *)
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
tgisMapList * tgis_new_map_list(void)
Return a new integer list.
Definition: map_list.c:54
void tgis_free_map_list(tgisMapList *list)
Free allocated memory of an integer list.
Definition: map_list.c:28
void tgis_init_map_list(tgisMapList *list)
Init a tgisMapList and free allocated memory.
Definition: map_list.c:69
void tgis_map_list_add(tgisMapList *list, tgisMap *map)
Add a map to tgisMapList.
Definition: map_list.c:103
void tgis_map_list_insert(tgisMapList *list, char *name, char *mapset, struct TimeStamp *ts)
Insert map information into tgisMapList.
Definition: map_list.c:133
const char * name
Definition: named_colr.c:6
struct list * list
Definition: read_list.c:24
Definition: gis.h:614
DateTime dt[2]
Definition: gis.h:615
int count
Definition: gis.h:616
List of tgisMap struct's.
Definition: temporal.h:54
char * mapset
Definition: temporal.h:44
char * name
Definition: temporal.h:43
struct TimeStamp ts
Definition: temporal.h:45
Definition: manage.h:4