GRASS Programmer's Manual
6.4.2(2012)
|
00001 #ifdef __MINGW32__ 00002 # include <windows.h> 00003 #endif 00004 #include <stdio.h> 00005 #include <stdlib.h> 00006 #include <sys/types.h> 00007 #include <unistd.h> 00008 #include "G3d_intern.h" 00009 00010 /*---------------------------------------------------------------------------*/ 00011 00012 static int G3d_closeNew(G3D_Map * map) 00013 { 00014 char path[GPATH_MAX]; 00015 struct Categories cats; 00016 struct History hist; 00017 00018 G3d_removeColor(map->fileName); 00019 00020 /* create empty cats file */ 00021 G_init_raster_cats(NULL, &cats); 00022 G3d_writeCats(map->fileName, &cats); 00023 G_free_cats(&cats); 00024 00025 /*genrate the history file, use the normal G_ functions */ 00026 G_short_history(map->fileName, "raster3d", &hist); 00027 G_command_history(&hist); 00028 /*Use the G3d function to write the history file, 00029 * otherwise the path is wrong */ 00030 if (!G3d_writeHistory(map->fileName, &hist)) { 00031 G3d_error("G3d_closeNew: can't write raster3d history"); 00032 } 00033 00034 00035 G3d_range_write(map); 00036 00037 close(map->data_fd); 00038 00039 /* finally move tempfile to data file */ 00040 G3d_filename(path, G3D_CELL_ELEMENT, map->fileName, map->mapset); 00041 #ifdef __MINGW32__ 00042 if (CopyFile(map->tempName, path, FALSE) == 0) { 00043 #else 00044 if (link(map->tempName, path) < 0) { 00045 #endif 00046 if (rename(map->tempName, path)) { 00047 G3d_error 00048 ("G3d_closeNew: can't move temp raster map %s\nto 3d data file %s", 00049 map->tempName, path); 00050 return 0; 00051 } 00052 } 00053 else 00054 remove(map->tempName); 00055 00056 return 1; 00057 } 00058 00059 /*---------------------------------------------------------------------------*/ 00060 00061 static int G3d_closeCellNew(G3D_Map * map) 00062 { 00063 long ltmp; 00064 00065 if (map->useCache) 00066 if (!G3d_flushAllTiles(map)) { 00067 G3d_error("G3d_closeCellNew: error in G3d_flushAllTiles"); 00068 return 0; 00069 } 00070 00071 if (!G3d_flushIndex(map)) { 00072 G3d_error("G3d_closeCellNew: error in G3d_flushIndex"); 00073 return 0; 00074 } 00075 00076 /* write the header info which was filled with dummy values at the */ 00077 /* opening time */ 00078 00079 if (lseek(map->data_fd, 00080 (long)(map->offset - sizeof(int) - sizeof(long)), 00081 SEEK_SET) == -1) { 00082 G3d_error("G3d_closeCellNew: can't position file"); 00083 return 0; 00084 } 00085 00086 if (!G3d_writeInts(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1)) { 00087 G3d_error("G3d_closeCellNew: can't write header"); 00088 return 0; 00089 } 00090 00091 G3d_longEncode(&(map->indexOffset), (unsigned char *)<mp, 1); 00092 if (write(map->data_fd, <mp, sizeof(long)) != sizeof(long)) { 00093 G3d_error("G3d_closeCellNew: can't write header"); 00094 return 0; 00095 } 00096 00097 if (!G3d_closeNew(map) != 0) { 00098 G3d_error("G3d_closeCellNew: error in G3d_closeNew"); 00099 return 0; 00100 } 00101 00102 return 1; 00103 } 00104 00105 /*---------------------------------------------------------------------------*/ 00106 00107 static int G3d_closeOld(G3D_Map * map) 00108 { 00109 if (close(map->data_fd) != 0) { 00110 G3d_error("G3d_closeOld: could not close file"); 00111 return 0; 00112 } 00113 00114 return 1; 00115 } 00116 00117 /*---------------------------------------------------------------------------*/ 00118 00119 static int G3d_closeCellOld(G3D_Map * map) 00120 { 00121 if (!G3d_closeOld(map) != 0) { 00122 G3d_error("G3d_closeCellOld: error in G3d_closeOld"); 00123 return 0; 00124 } 00125 00126 return 1; 00127 } 00128 00129 /*---------------------------------------------------------------------------*/ 00130 00131 00144 int G3d_closeCell(G3D_Map * map) 00145 { 00146 if (map->operation == G3D_WRITE_DATA) { 00147 if (!G3d_closeCellNew(map)) { 00148 G3d_error("G3d_closeCell: error in G3d_closeCellNew"); 00149 return 0; 00150 } 00151 } 00152 else { 00153 if (!G3d_closeCellOld(map) != 0) { 00154 G3d_error("G3d_closeCell: error in G3d_closeCellOld"); 00155 return 0; 00156 } 00157 } 00158 00159 G3d_free(map->index); 00160 G3d_free(map->tileLength); 00161 00162 if (map->useCache) { 00163 if (!G3d_disposeCache(map)) { 00164 G3d_error("G3d_closeCell: error in G3d_disposeCache"); 00165 return 0; 00166 } 00167 } 00168 else 00169 G3d_free(map->data); 00170 00171 if (map->operation == G3D_WRITE_DATA) 00172 if (!G3d_writeHeader(map, 00173 map->region.proj, map->region.zone, 00174 map->region.north, map->region.south, 00175 map->region.east, map->region.west, 00176 map->region.top, map->region.bottom, 00177 map->region.rows, map->region.cols, 00178 map->region.depths, 00179 map->region.ew_res, map->region.ns_res, 00180 map->region.tb_res, 00181 map->tileX, map->tileY, map->tileZ, 00182 map->type, 00183 map->compression, map->useRle, map->useLzw, 00184 map->precision, map->offset, map->useXdr, 00185 map->hasIndex, map->unit)) { 00186 G3d_error("G3d_closeCell: error in G3d_writeHeader"); 00187 return 0; 00188 } 00189 00190 G3d_free(map->unit); 00191 G3d_free(map); 00192 return 1; 00193 }