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