GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dclose.c
Go to the documentation of this file.
1 #ifdef __MINGW32__
2 # include <windows.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include "G3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 static int G3d_closeNew(G3D_Map * map)
13 {
14  char path[GPATH_MAX];
15  struct Categories cats;
16  struct History hist;
17 
18  G3d_removeColor(map->fileName);
19 
20  /* create empty cats file */
21  G_init_raster_cats(NULL, &cats);
22  G3d_writeCats(map->fileName, &cats);
23  G_free_cats(&cats);
24 
25  /*genrate the history file, use the normal G_ functions */
26  G_short_history(map->fileName, "raster3d", &hist);
27  G_command_history(&hist);
28  /*Use the G3d function to write the history file,
29  * otherwise the path is wrong */
30  if (!G3d_writeHistory(map->fileName, &hist)) {
31  G3d_error("G3d_closeNew: can't write raster3d history");
32  }
33 
34 
35  G3d_range_write(map);
36 
37  close(map->data_fd);
38 
39  /* finally move tempfile to data file */
40  G3d_filename(path, G3D_CELL_ELEMENT, map->fileName, map->mapset);
41 #ifdef __MINGW32__
42  if (CopyFile(map->tempName, path, FALSE) == 0) {
43 #else
44  if (link(map->tempName, path) < 0) {
45 #endif
46  if (rename(map->tempName, path)) {
47  G3d_error
48  ("G3d_closeNew: can't move temp raster map %s\nto 3d data file %s",
49  map->tempName, path);
50  return 0;
51  }
52  }
53  else
54  remove(map->tempName);
55 
56  return 1;
57 }
58 
59 /*---------------------------------------------------------------------------*/
60 
61 static int G3d_closeCellNew(G3D_Map * map)
62 {
63  long ltmp;
64 
65  if (map->useCache)
66  if (!G3d_flushAllTiles(map)) {
67  G3d_error("G3d_closeCellNew: error in G3d_flushAllTiles");
68  return 0;
69  }
70 
71  if (!G3d_flushIndex(map)) {
72  G3d_error("G3d_closeCellNew: error in G3d_flushIndex");
73  return 0;
74  }
75 
76  /* write the header info which was filled with dummy values at the */
77  /* opening time */
78 
79  if (lseek(map->data_fd,
80  (long)(map->offset - sizeof(int) - sizeof(long)),
81  SEEK_SET) == -1) {
82  G3d_error("G3d_closeCellNew: can't position file");
83  return 0;
84  }
85 
86  if (!G3d_writeInts(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1)) {
87  G3d_error("G3d_closeCellNew: can't write header");
88  return 0;
89  }
90 
91  G3d_longEncode(&(map->indexOffset), (unsigned char *)&ltmp, 1);
92  if (write(map->data_fd, &ltmp, sizeof(long)) != sizeof(long)) {
93  G3d_error("G3d_closeCellNew: can't write header");
94  return 0;
95  }
96 
97  if (!G3d_closeNew(map) != 0) {
98  G3d_error("G3d_closeCellNew: error in G3d_closeNew");
99  return 0;
100  }
101 
102  return 1;
103 }
104 
105 /*---------------------------------------------------------------------------*/
106 
107 static int G3d_closeOld(G3D_Map * map)
108 {
109  if (close(map->data_fd) != 0) {
110  G3d_error("G3d_closeOld: could not close file");
111  return 0;
112  }
113 
114  return 1;
115 }
116 
117 /*---------------------------------------------------------------------------*/
118 
119 static int G3d_closeCellOld(G3D_Map * map)
120 {
121  if (!G3d_closeOld(map) != 0) {
122  G3d_error("G3d_closeCellOld: error in G3d_closeOld");
123  return 0;
124  }
125 
126  return 1;
127 }
128 
129 /*---------------------------------------------------------------------------*/
130 
131 
144 int G3d_closeCell(G3D_Map * map)
145 {
146  if (map->operation == G3D_WRITE_DATA) {
147  if (!G3d_closeCellNew(map)) {
148  G3d_error("G3d_closeCell: error in G3d_closeCellNew");
149  return 0;
150  }
151  }
152  else {
153  if (!G3d_closeCellOld(map) != 0) {
154  G3d_error("G3d_closeCell: error in G3d_closeCellOld");
155  return 0;
156  }
157  }
158 
159  G3d_free(map->index);
160  G3d_free(map->tileLength);
161 
162  if (map->useCache) {
163  if (!G3d_disposeCache(map)) {
164  G3d_error("G3d_closeCell: error in G3d_disposeCache");
165  return 0;
166  }
167  }
168  else
169  G3d_free(map->data);
170 
171  if (map->operation == G3D_WRITE_DATA)
172  if (!G3d_writeHeader(map,
173  map->region.proj, map->region.zone,
174  map->region.north, map->region.south,
175  map->region.east, map->region.west,
176  map->region.top, map->region.bottom,
177  map->region.rows, map->region.cols,
178  map->region.depths,
179  map->region.ew_res, map->region.ns_res,
180  map->region.tb_res,
181  map->tileX, map->tileY, map->tileZ,
182  map->type,
183  map->compression, map->useRle, map->useLzw,
184  map->precision, map->offset, map->useXdr,
185  map->hasIndex, map->unit)) {
186  G3d_error("G3d_closeCell: error in G3d_writeHeader");
187  return 0;
188  }
189 
190  G3d_free(map->unit);
191  G3d_free(map);
192  return 1;
193 }
int G_short_history(const char *name, const char *type, struct History *hist)
initialize history structure
Definition: history.c:202
int G3d_disposeCache(G3D_Map *map)
Definition: g3dcache.c:243
void G3d_free(void *buf)
Same as free (ptr).
Definition: g3dalloc.c:71
#define FALSE
Definition: dbfopen.c:117
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int G3d_flushAllTiles(G3D_Map *map)
Definition: g3dcache.c:278
int G3d_range_write(G3D_Map *map)
Writes the range which is stored in the range structure of map. (This function is invoked automatical...
Definition: g3drange.c:218
#define G3D_WRITE_DATA
Definition: G3d_intern.h:19
void G3d_filename(char *path, const char *elementName, const char *mapName, const char *mapset)
Definition: filename.c:10
int G_free_cats(struct Categories *pcats)
free category structure memory
Definition: gis/cats.c:1540
int G_command_history(struct History *hist)
Save command line to raster history structure.
Definition: history.c:254
int G3d_writeInts(int fd, int useXdr, const int *i, int nofNum)
Definition: g3dintio.c:11
int G3d_removeColor(const char *name)
Definition: g3dcolor.c:20
int G_init_raster_cats(const char *title, struct Categories *pcats)
Same as existing G_init_raster_cats() only ncats argument is missign. ncats has no meaning in new Cat...
Definition: gis/cats.c:1437
int G3d_closeCell(G3D_Map *map)
Closes g3d-file. If map is new and cache-mode is used for map then every tile which is not flushed be...
Definition: g3dclose.c:144
int G3d_longEncode(long *source, unsigned char *dst, int nofNums)
Definition: g3dlong.c:5
int G3d_flushIndex(G3D_Map *map)
Definition: g3d/index.c:79
return NULL
Definition: dbfopen.c:1394
int G3d_writeHistory(const char *name, struct History *hist)
write raster3d History file
Definition: g3dhistory.c:162
int G3d_writeCats(const char *name, struct Categories *cats)
Writes the categories stored in the cats structure into the categories file for map name in the curre...
Definition: g3dcats.c:26
int G3d_writeHeader(G3D_Map *map, int proj, int zone, double north, double south, double east, double west, double top, double bottom, int rows, int cols, int depths, double ew_res, double ns_res, double tb_res, int tileX, int tileY, int tileZ, int type, int compression, int useRle, int useLzw, int precision, int dataOffset, int useXdr, int hasIndex, char *unit)
Definition: g3d/header.c:156