/*! * \brief Create tempfile from process id. * * See G_tempfile(). * * \param pid * \return pointer to string path */ char *G__tempfile(int pid) { char path[GPATH_MAX]; char name[GNAME_MAX]; char element[100]; if (pid <= 0) pid = getpid(); G__temp_element(element); G_init_tempfile(); do { int uniq = G_counter_next(&unique); sprintf(name, "%d.%d", pid, uniq); G_file_name(path, element, name, G_mapset()); } while (access(path, F_OK) == 0); return G_store(path); }
int save_table(int t) { int i, j, ncols, nrows, ret, field, rec; char name[2000], fname[20], element[100]; DBFHandle dbf; ROW *rows; VALUE *val; int dbftype, width, decimals; G_debug(2, "save_table %d", t); /* Note: because if driver is killed during the time the table is written, the process * is not completed and DATA ARE LOST. To minimize this, data are first written * to temporary file and then this file is renamed to 'database/table.dbf'. * Hopefully both file are on the same disk/partition */ if (!(db.tables[t].alive) || !(db.tables[t].updated)) return DB_OK; /* Construct our temp name because shapelib doesn't like '.' in name */ G__temp_element(element); sprintf(fname, "%d.dbf", getpid()); G_file_name(name, element, fname, G_mapset()); G_debug(2, "Write table to tempfile: '%s'", name); dbf = DBFCreate(name); if (dbf == NULL) return DB_FAILED; ncols = db.tables[t].ncols; rows = db.tables[t].rows; nrows = db.tables[t].nrows; for (i = 0; i < ncols; i++) { switch (db.tables[t].cols[i].type) { case DBF_INT: dbftype = FTInteger; break; case DBF_CHAR: dbftype = FTString; break; case DBF_DOUBLE: dbftype = FTDouble; break; } width = db.tables[t].cols[i].width; decimals = db.tables[t].cols[i].decimals; DBFAddField(dbf, db.tables[t].cols[i].name, dbftype, width, decimals); } G_debug(2, "Write %d rows", nrows); rec = 0; for (i = 0; i < nrows; i++) { if (rows[i].alive == FALSE) continue; for (j = 0; j < ncols; j++) { field = j; val = &(rows[i].values[j]); if (val->is_null) { DBFWriteNULLAttribute(dbf, rec, field); } else { switch (db.tables[t].cols[j].type) { case DBF_INT: ret = DBFWriteIntegerAttribute(dbf, rec, field, val->i); break; case DBF_CHAR: if (val->c != NULL) ret = DBFWriteStringAttribute(dbf, rec, field, val->c); else ret = DBFWriteStringAttribute(dbf, rec, field, ""); break; case DBF_DOUBLE: ret = DBFWriteDoubleAttribute(dbf, rec, field, val->d); break; } } } rec++; } G_debug(2, "Written %d records", rec); DBFClose(dbf); /* Copy */ if (G_rename_file(name, db.tables[t].file)) { append_error("Cannot move %s\nto %s\n", name, db.tables[t].file); return DB_FAILED; }; return DB_OK; }