Skip to content

Commit

Permalink
Imagery: move signatures to subdirs (#1850)
Browse files Browse the repository at this point in the history
Imagery: move signature files to a <name>/<element> type directory structure (a.k.a. _misc)

Full path to signature file now is:
location/mapset/signatures/<type>/<name>/<element>

This new structure will allow to store arbitrary number of files for each signature.
None of existing signature types utilizes the new functionality.
  • Loading branch information
marisn committed Sep 23, 2021
1 parent dca9364 commit fb4a975
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 351 deletions.
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/gselect.py
Expand Up @@ -2835,7 +2835,7 @@ def _append_mapset_signatures(self, mapset, sig_type, items):
count = I_signatures_list_by_type(sig_type, mapset, ctypes.byref(sig_list))
for n in range(count):
items.append(decode(sig_list[n]))
I_free_signatures_list(count, sig_list)
I_free_signatures_list(count, ctypes.byref(sig_list))


class SeparatorSelect(wx.ComboBox):
Expand Down
4 changes: 2 additions & 2 deletions include/grass/defs/imagery.h
Expand Up @@ -148,8 +148,8 @@ int I_apply_colormap(unsigned char *, unsigned char *, unsigned, unsigned char
int I_rasterize(double *, int, unsigned char, struct Cell_head *, unsigned char *);

/* manage_signatures.c */
void I__get_signatures_element(char *, I_SIGFILE_TYPE);
void I__make_signatures_element(I_SIGFILE_TYPE);
void I_get_signatures_dir(char *, I_SIGFILE_TYPE);
void I_make_signatures_dir(I_SIGFILE_TYPE);
int I_signatures_remove(I_SIGFILE_TYPE, const char *);
int I_signatures_copy(I_SIGFILE_TYPE, const char *, const char *, const char *);
int I_signatures_rename(I_SIGFILE_TYPE, const char *, const char *);
Expand Down
41 changes: 26 additions & 15 deletions lib/imagery/find.c
Expand Up @@ -67,7 +67,8 @@ int I_find_group_file(const char *group, const char *file)
* \param file
* \return int 1 if file was found, 0 otherwise
*/
int I_find_group_file2(const char *group, const char *mapset, const char *file)
int I_find_group_file2(const char *group, const char *mapset,
const char *file)
{
if (!I_find_group2(group, mapset))
return 0;
Expand Down Expand Up @@ -107,7 +108,8 @@ int I_find_subgroup(const char *group, const char *subgroup)
* \param mapset
* \return int 1 if subrgoup was found, 0 otherwise
*/
int I_find_subgroup2(const char *group, const char *subgroup, const char *mapset)
int I_find_subgroup2(const char *group, const char *subgroup,
const char *mapset)
{
char element[GNAME_MAX];

Expand Down Expand Up @@ -142,7 +144,8 @@ int I_find_subgroup_file(const char *group, const char *subgroup,
if (file == NULL || *file == 0)
return 0;

sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP, file);
sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP,
file);
G_debug(5, "I_find_subgroup_file() element: %s", element);

return G_find_file2_misc("group", element, group, G_mapset()) != NULL;
Expand All @@ -158,7 +161,7 @@ int I_find_subgroup_file(const char *group, const char *subgroup,
* \return int 1 if file was found, 0 otherwise
*/
int I_find_subgroup_file2(const char *group, const char *subgroup,
const char *mapset, const char *file)
const char *mapset, const char *file)
{
char element[GNAME_MAX * 2];

Expand All @@ -169,7 +172,8 @@ int I_find_subgroup_file2(const char *group, const char *subgroup,
if (file == NULL || *file == 0)
return 0;

sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP, file);
sprintf(element, "subgroup%c%s%c%s", HOST_DIRSEP, subgroup, HOST_DIRSEP,
file);
G_debug(5, "I_find_subgroup_file2() element: %s", element);

return G_find_file2_misc("group", element, group, mapset) != NULL;
Expand Down Expand Up @@ -198,14 +202,18 @@ int I_find_subgroup_file2(const char *group, const char *subgroup,
* \param mapset set NULL to search in all mapsets
* \return mapset or NULL
*/
const char *I_find_signature(I_SIGFILE_TYPE type, char *name, const char *mapset) {
char selem[GNAME_MAX]; /* 'signatures/type\0' */
const char *I_find_signature(I_SIGFILE_TYPE type, char *name,
const char *mapset)
{
char sdir[GNAME_MAX]; /* 'signatures/type\0' */

G_debug(1, "I_find_signature(): type=%d name=%s mapset=%s", type, name, mapset);
G_debug(1, "I_find_signature(): type=%d name=%s mapset=%s", type, name,
mapset);

I__get_signatures_element(selem, type);
I_get_signatures_dir(sdir, type);

return G_find_file(selem, name, mapset);
/* We do not search for a specific file as file name is up to signature type */
return G_find_file(sdir, name, mapset);
}

/*!
Expand All @@ -229,12 +237,15 @@ const char *I_find_signature(I_SIGFILE_TYPE type, char *name, const char *mapset
* \param mapset set NULL to search in all mapsets
* \return mapset or NULL
*/
const char *I_find_signature2(I_SIGFILE_TYPE type, const char *name, const char *mapset) {
char selem[GNAME_MAX]; /* 'signatures/type\0' */
const char *I_find_signature2(I_SIGFILE_TYPE type, const char *name,
const char *mapset)
{
char sdir[GNAME_MAX]; /* 'signatures/type\0' */

G_debug(1, "I_find_signature2(): type=%d name=%s mapset=%s", type, name, mapset);
G_debug(1, "I_find_signature2(): type=%d name=%s mapset=%s", type, name,
mapset);

I__get_signatures_element(selem, type);
I_get_signatures_dir(sdir, type);

return G_find_file2(selem, name, mapset);
return G_find_file2(sdir, name, mapset);
}
72 changes: 42 additions & 30 deletions lib/imagery/manage_signatures.c
Expand Up @@ -19,35 +19,41 @@
#include <grass/glocale.h>

/*!
\brief Get signature element (internal use only)
\brief Get signature directory
\param element [GNAME_MAX] allocated string buffer
\param type I_SIGFILE_TYPE
*/
void I__get_signatures_element(char *element, I_SIGFILE_TYPE type)
The directory will be in a form "signatures/<type>".
\param dir [GNAME_MAX] allocated string buffer
\param type I_SIGFILE_TYPE
*/
void I_get_signatures_dir(char *dir, I_SIGFILE_TYPE type)
{
if (type == I_SIGFILE_TYPE_SIG) {
sprintf(element, "signatures%csig", HOST_DIRSEP);
sprintf(dir, "signatures%csig", HOST_DIRSEP);
}
else if (type == I_SIGFILE_TYPE_SIGSET) {
sprintf(element, "signatures%csigset", HOST_DIRSEP);
sprintf(dir, "signatures%csigset", HOST_DIRSEP);
}
else {
G_fatal_error("Programming error: unknown signature file type");
}
}

/*!
\brief Make signature element (internal use only)
\brief Make signature dir
Creates directories for storage of signature files of specified type.
E.g. "<location>/<mapset>/signatures/<type>/"
\param type I_SIGFILE_TYPE
*/
void I__make_signatures_element(I_SIGFILE_TYPE type)
\param type I_SIGFILE_TYPE
*/
void I_make_signatures_dir(I_SIGFILE_TYPE type)
{
char element[GNAME_MAX];
char dir[GNAME_MAX];

G_make_mapset_object_group("signatures");
I__get_signatures_element(element, type);
G_make_mapset_object_group(element);
I_get_signatures_dir(dir, type);
G_make_mapset_object_group(dir);
}

static int list_by_type(I_SIGFILE_TYPE, const char *, int, char ***);
Expand All @@ -66,7 +72,7 @@ static int list_by_type(I_SIGFILE_TYPE, const char *, int, char ***);
int I_signatures_remove(I_SIGFILE_TYPE type, const char *name)
{
char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
char element[GNAME_MAX];
char dir[GNAME_MAX];
int ret = 0;

G_debug(1, "I_signatures_remove(%d, %s);", type, name);
Expand All @@ -79,8 +85,8 @@ int I_signatures_remove(I_SIGFILE_TYPE type, const char *name)
return 1;
}
if (I_find_signature2(type, name, G_mapset())) {
I__get_signatures_element(element, type);
if (G_remove(element, name) == 1) {
I_get_signatures_dir(dir, type);
if (G_remove(dir, name) == 1) {
G_verbose_message(_("%s removed"), name);
return 0;
}
Expand Down Expand Up @@ -109,7 +115,7 @@ int I_signatures_copy(I_SIGFILE_TYPE type, const char *old_name,
{
char sname[GNAME_MAX], tname[GNAME_MAX], tmapset[GMAPSET_MAX],
xmapset[GMAPSET_MAX];
char element[GNAME_MAX];
char dir[GNAME_MAX];
const char *smapset;
char old_path[GPATH_MAX], new_path[GPATH_MAX];

Expand All @@ -134,13 +140,15 @@ int I_signatures_copy(I_SIGFILE_TYPE type, const char *old_name,
}
G_unqualified_name(old_name, NULL, sname, xmapset);

I__make_signatures_element(type);
I_make_signatures_dir(type);

I__get_signatures_element(element, type);
G_file_name(old_path, element, sname, smapset);
G_file_name(new_path, element, tname, G_mapset());
I_get_signatures_dir(dir, type);
/* Note – we need whole directory not just an element in it thus
G_file_name and not G_file_name_misc */
G_file_name(old_path, dir, sname, smapset);
G_file_name(new_path, dir, tname, G_mapset());

if (G_copy_file(old_path, new_path) != 1) {
if (G_recursive_copy(old_path, new_path) != 0) {
G_warning(_("Unable to copy <%s> to current mapset as <%s>"),
G_fully_qualified_name(old_name, smapset), tname);
return 1;
Expand All @@ -164,7 +172,7 @@ int I_signatures_rename(I_SIGFILE_TYPE type, const char *old_name,
const char *new_name)
{
char sname[GNAME_MAX], tname[GNAME_MAX], tmapset[GMAPSET_MAX];
char element[GNAME_MAX];
char dir[GNAME_MAX];
const char *smapset;
char old_path[GPATH_MAX], new_path[GPATH_MAX];

Expand Down Expand Up @@ -196,9 +204,11 @@ int I_signatures_rename(I_SIGFILE_TYPE type, const char *old_name,
return 1;
}

I__get_signatures_element(element, type);
G_file_name(old_path, element, sname, tmapset);
G_file_name(new_path, element, tname, tmapset);
I_get_signatures_dir(dir, type);
/* Note – we need whole directory not just an element in it thus
G_file_name and not G_file_name_misc */
G_file_name(old_path, dir, sname, tmapset);
G_file_name(new_path, dir, tname, tmapset);

if (G_rename_file(old_path, new_path) != 0) {
G_warning(_("Unable to rename <%s> to <%s>"), old_name, new_name);
Expand Down Expand Up @@ -245,6 +255,7 @@ int I_signatures_list_by_type(I_SIGFILE_TYPE type, const char *mapset,
* \brief Free memory allocated by I_signatures_list_by_type
*
* Calls G_free for all list items returned by I_signatures_list_by_type()
* Sets pointer to NULL to prevent accidental use after free.
*
* \param int Return value of I_signatures_list_by_type()
* \param pointer to array filled by I_signatures_list_by_type()
Expand All @@ -255,18 +266,19 @@ void I_free_signatures_list(int count, char ***list)
G_free((*list)[n]);
}
G_free(*list);
*list = NULL;
}

static int list_by_type(I_SIGFILE_TYPE type, const char *mapset, int base,
char ***out_list)
{
int count = 0;
char path[GPATH_MAX];
char element[GNAME_MAX];
char dir[GNAME_MAX];
char **dirlist;

I__get_signatures_element(element, type);
G_file_name(path, element, "", mapset);
I_get_signatures_dir(dir, type);
G_file_name(path, dir, "", mapset);

if (access(path, 0) != 0) {
return count;
Expand Down
23 changes: 14 additions & 9 deletions lib/imagery/sigfile.c
Expand Up @@ -17,29 +17,34 @@
/*!
\brief Create signature file
Returns a pointer to FILE for writing signature file.
Use fclose on the pointer to close after use.
\param name signature filename
\return pointer to FILE
\return NULL on error
*/
FILE *I_fopen_signature_file_new(const char *name)
{
char element[GNAME_MAX];
char dir[GNAME_MAX];
FILE *fd;

/* create sig directory */
I__make_signatures_element(I_SIGFILE_TYPE_SIG);

I__get_signatures_element(element, I_SIGFILE_TYPE_SIG);
fd = G_fopen_new(element, name);
I_make_signatures_dir(I_SIGFILE_TYPE_SIG);
I_get_signatures_dir(dir, I_SIGFILE_TYPE_SIG);
fd = G_fopen_new_misc(dir, "sig", name);

return fd;
}

/*!
\brief Open existing signature file
Use fully qualified names for signatures from other mapsets
Use fully qualified names for signatures from other mapsets.
Returns a pointer to FILE with signature. Use fclose on the pointer
after use.
\param name signature filename
Expand All @@ -49,14 +54,14 @@ FILE *I_fopen_signature_file_new(const char *name)
FILE *I_fopen_signature_file_old(const char *name)
{
char sig_name[GNAME_MAX], sig_mapset[GMAPSET_MAX];
char element[GNAME_MAX];
char dir[GNAME_MAX];
FILE *fd;

if (G_unqualified_name(name, NULL, sig_name, sig_mapset) == 0)
strcpy(sig_mapset, G_mapset());

I__get_signatures_element(element, I_SIGFILE_TYPE_SIG);
fd = G_fopen_old(element, sig_name, sig_mapset);
I_get_signatures_dir(dir, I_SIGFILE_TYPE_SIG);
fd = G_fopen_old_misc(dir, "sig", sig_name, sig_mapset);

return fd;
}

0 comments on commit fb4a975

Please sign in to comment.