Loading...
Searching...
No Matches
open_misc.c
Go to the documentation of this file.
/****************************************************************************
*
* MODULE: gis library
* AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
* COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team
*
* NOTE: Based upon open.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*****************************************************************************/
#include <grass/config.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include "gis_local_proto.h"
static int G__open_misc(const char *dir, const char *element, const char *name,
const char *mapset, int mode)
{
int fd;
char path[GPATH_MAX];
char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
G__check_gisinit();
/* READ */
if (mode == 0) {
if (G_name_is_fully_qualified(name, xname, xmapset)) {
if (*mapset && strcmp(xmapset, mapset) != 0) {
G_warning(_("G__open_misc(read): mapset <%s> doesn't match "
"xmapset <%s>"),
mapset, xmapset);
return -1;
}
name = xname;
mapset = xmapset;
}
mapset = G_find_file2_misc(dir, element, name, mapset);
if (!mapset)
return -1;
G_file_name_misc(path, dir, element, name, mapset);
if ((fd = open(path, 0)) < 0)
G_warning("G__open_misc(read): Unable to open '%s': %s", path,
strerror(errno));
return fd;
}
/* WRITE */
if (mode == 1 || mode == 2) {
mapset = G_mapset();
if (G_name_is_fully_qualified(name, xname, xmapset)) {
if (strcmp(xmapset, mapset) != 0) {
G_warning(
_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
xmapset, mapset);
return -1;
}
name = xname;
}
if (G_legal_filename(name) == -1)
return -1;
G_file_name_misc(path, dir, element, name, mapset);
if (mode == 1 || access(path, 0) != 0) {
G__make_mapset_element_misc(dir, name);
close(creat(path, 0666));
}
if ((fd = open(path, mode)) < 0)
G_warning("G__open_misc(write): Unable to open '%s': %s", path,
strerror(errno));
return fd;
}
return -1;
}
/*!
* \brief open a new database misc file
*
* The database file <b>element</b> under <b>dir/name</b> in the
* current mapset is created and opened for writing (but not reading).
* The UNIX open( ) routine is used to open the file. If the file does not
* exist, -1 is returned. Otherwise the file is positioned at the end of the
* file and the file descriptor from the open( ) is returned.
*
* \param dir
* \param element
* \param name
* \return int
*/
int G_open_new_misc(const char *dir, const char *element, const char *name)
{
return G__open_misc(dir, element, name, G_mapset(), 1);
}
/*!
* \brief open a database misc file for reading
*
* The database file <b>element</b> under
* <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
* not for writing).
* The UNIX open( ) routine is used to open the file. If the file does not
* exist, -1 is returned. Otherwise the file descriptor from the open( ) is
* returned.
*
* \param dir
* \param element
* \param name
* \param mapset
* \return int
*/
int G_open_old_misc(const char *dir, const char *element, const char *name,
const char *mapset)
{
return G__open_misc(dir, element, name, mapset, 0);
}
/*!
* \brief open a database misc file for update
*
* The database file <b>element</b> under <b>dir/name</b> in the
* current mapset is opened for reading and writing.
* The UNIX open( ) routine is used to open the file. If the file does not
* exist, -1 is returned. Otherwise the file is positioned at the end of the
* file and the file descriptor from the open( ) is returned.
*
* \param dir
* \param element
* \param name
* \return int
*/
int G_open_update_misc(const char *dir, const char *element, const char *name)
{
int fd;
fd = G__open_misc(dir, element, name, G_mapset(), 2);
if (fd >= 0)
if (lseek(fd, 0L, SEEK_END) == -1) {
int err = errno;
G_warning(_("File read/write operation failed: %s (%d)"),
strerror(err), err);
return -1;
}
return fd;
}
/*!
* \brief open a new database misc file
*
* The database file <b>element</b> under <b>dir/name</b> in the
* current mapset is created and opened for writing (but not reading).
* The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
* the file does not exist, the NULL pointer is returned. Otherwise the file is
* positioned at the end of the file and the file descriptor from the fopen( )
* is returned.
*
* \param dir
* \param element
* \param name
* \return FILE *
*/
FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
{
int fd;
fd = G__open_misc(dir, element, name, G_mapset(), 1);
if (fd < 0)
return (FILE *)0;
return fdopen(fd, "w");
}
/*!
* \brief open a database misc file for reading
*
* The database file <b>element</b> under
* <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
* not for writing).
* The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
* the file does not exist, the NULL pointer is returned. Otherwise the file
* descriptor from the fopen( ) is returned.
*
* \param dir
* \param element
* \param name
* \param mapset
* \return FILE *
*/
FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
const char *mapset)
{
int fd;
fd = G__open_misc(dir, element, name, mapset, 0);
if (fd < 0)
return (FILE *)0;
return fdopen(fd, "r");
}
FILE *G_fopen_append_misc(const char *dir, const char *element,
const char *name)
{
int fd;
fd = G__open_misc(dir, element, name, G_mapset(), 2);
if (fd < 0)
return (FILE *)0;
if (lseek(fd, 0L, SEEK_END) == -1) {
int err = errno;
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
err);
return (FILE *)-1;
}
return fdopen(fd, "a");
}
FILE *G_fopen_modify_misc(const char *dir, const char *element,
const char *name)
{
int fd;
fd = G__open_misc(dir, element, name, G_mapset(), 2);
if (fd < 0)
return (FILE *)0;
if (lseek(fd, 0L, SEEK_END) == -1) {
int err = errno;
G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
err);
return (FILE *)-1;
}
return fdopen(fd, "r+");
}
G_file_name_misc
char * G_file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Builds full path names to GIS misc data files.
Definition file_name.c:101
G_find_file2_misc
const char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Searches for a misc file from the mapset search list or in a specified mapset. (look but don't touch)...
Definition find_file.c:257
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203
G__check_gisinit
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition gisinit.c:148
G_legal_filename
int G_legal_filename(const char *s)
Check for legal database file name.
Definition legal_name.c:34
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition mapset.c:33
G__make_mapset_element_misc
int G__make_mapset_element_misc(const char *dir, const char *name)
Create misc element in the current mapset.
Definition mapset_msc.c:261
name
const char * name
Definition named_colr.c:6
G_name_is_fully_qualified
int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset).
Definition nme_in_mps.c:36
G_fopen_old_misc
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition open_misc.c:210
G_open_new_misc
int G_open_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:111
G_fopen_modify_misc
FILE * G_fopen_modify_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:240
G_open_update_misc
int G_open_update_misc(const char *dir, const char *element, const char *name)
open a database misc file for update
Definition open_misc.c:152
G_fopen_new_misc
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition open_misc.c:183
G_fopen_append_misc
FILE * G_fopen_append_misc(const char *dir, const char *element, const char *name)
Definition open_misc.c:222
G_open_old_misc
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition open_misc.c:132
element
Definition lidar.h:85
path
Definition path.h:15
err
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition symbol/read.c:216