[GRASS-SVN] r33421 - in grass/branches/develbranch_6/general: .
g.mlist g.mremove
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Sep 13 02:01:58 EDT 2008
Author: neteler
Date: 2008-09-13 02:01:57 -0400 (Sat, 13 Sep 2008)
New Revision: 33421
Added:
grass/branches/develbranch_6/general/g.mlist/
grass/branches/develbranch_6/general/g.mlist/Makefile
grass/branches/develbranch_6/general/g.mlist/description.html
grass/branches/develbranch_6/general/g.mlist/global.h
grass/branches/develbranch_6/general/g.mlist/main.c
grass/branches/develbranch_6/general/g.mlist/read_list.c
grass/branches/develbranch_6/general/g.mlist/wc2regex.c
grass/branches/develbranch_6/general/g.mremove/
grass/branches/develbranch_6/general/g.mremove/Makefile
grass/branches/develbranch_6/general/g.mremove/check_reclass.c
grass/branches/develbranch_6/general/g.mremove/description.html
grass/branches/develbranch_6/general/g.mremove/do_remove.c
grass/branches/develbranch_6/general/g.mremove/global.h
grass/branches/develbranch_6/general/g.mremove/main.c
grass/branches/develbranch_6/general/g.mremove/read_list.c
grass/branches/develbranch_6/general/g.mremove/wc2regex.c
Log:
Add g.mlist, g.mremove written my Huidae Cho (from rev 33262, grass-addons/general/g.xlist and g.xremove)
Added: grass/branches/develbranch_6/general/g.mlist/Makefile
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/Makefile (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/Makefile 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,14 @@
+MODULE_TOPDIR = ../..
+
+PGM = g.mlist
+
+LIBES = $(GISLIB)
+DEPENDENCIES = $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+ifneq ($(USE_REGEX),)
+default: cmd
+else
+default:
+endif
Property changes on: grass/branches/develbranch_6/general/g.mlist/Makefile
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mlist/description.html
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/description.html (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/description.html 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,65 @@
+<h2>DESCRIPTION</h2>
+
+<em>g.mlist</em> searches for data files matching a pattern given by wildcards or POSIX Extended Regular Expressions.
+
+<h2>EXAMPLES</h2>
+
+List all available GRASS data base files:
+<div class="code"><pre>
+ g.mlist type=all
+</pre></div>
+
+List all raster and vector maps:
+<div class="code"><pre>
+ g.mlist type=rast,vect
+</pre></div>
+
+<h3>Wildcards:</h3>
+
+List all vector maps starting with letter "r":
+<div class="code"><pre>
+ g.mlist type=vect pattern="r*"
+</pre></div>
+
+List certain raster maps with one variable character/number:
+<div class="code"><pre>
+ g.mlist type=rast pattern="N45E00?.meters"
+</pre></div>
+
+<h3>Regular expressions:</h3>
+
+Print out all soils map with "soils" in their name:
+<div class="code"><pre>
+ g.mlist -r type=rast pattern='^soils'
+</pre></div>
+
+Print out "tmp" if "tmp" raster map exists:
+<div class="code"><pre>
+ g.mlist -r pattern='^tmp$'
+</pre></div>
+
+Print out "tmp0" ..."tmp9" if corresponding vector map exists (each map name linewise):
+<div class="code"><pre>
+ g.mlist -r type=vect pattern='^tmp[0-9]$'
+</pre></div>
+
+Print out "tmp0" ..."tmp9" if corresponding vector map exists (each map name comma separated):
+<div class="code"><pre>
+ g.mlist -r type=vect separator=, pattern='^tmp[0-9]$'
+</pre></div>
+
+This may be useful for other programs' parameter input
+(e.g. <em><a href="r.series.html">r.series</a></em>).
+
+<h2>SEE ALSO</h2>
+
+<em><a href="g.list.html">g.list</a></em>
+<p>
+<a href="http://en.wikipedia.org/wiki/Regular_expression">Regular expression</a> (from Wikipedia, the free encyclopedia)
+
+<h2>AUTHOR</h2>
+
+Huidae Cho<br>
+grass4u at gmail.com
+
+<p><i>Last changed: $Date: 2008-06-28 04:37:22 -0500 (Sat, 28 Jun 2008) $</i>
Added: grass/branches/develbranch_6/general/g.mlist/global.h
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/global.h (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/global.h 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,23 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+struct list
+{
+ char **element; /* list of related elements */
+ char *alias; /* element alias */
+ char **desc; /* description of elements */
+ char *text; /* menu text */
+ int nelem; /* number of elements */
+ char status;
+ char *mainelem; /* main element */
+ char *maindesc; /* main element description */
+};
+
+/* read_list.c */
+int read_list(int);
+
+/* wc2regex.c */
+char *wc2regex(const char *);
+
+extern int nlist;
+extern struct list *list;
Property changes on: grass/branches/develbranch_6/general/g.mlist/global.h
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mlist/main.c
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/main.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/main.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,292 @@
+
+/****************************************************************************
+ *
+ * MODULE: g.xlist
+ *
+ * AUTHOR(S): Huidae Cho
+ * Based on general/manage/cmd/list.c by Michael Shapiro.
+ *
+ * PURPOSE: Lists available GRASS data base files of the
+ * user-specified data type to standard output
+ *
+ * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <regex.h>
+#include <grass/spawn.h>
+#include "global.h"
+
+int nlist;
+struct list *list;
+
+static int any;
+
+static void do_list(const struct list *,
+ const char *, const char *, const char *,
+ int, int, int);
+static int parse(const char *);
+static int ls_filter(const char *, void *);
+
+int main(int argc, char *argv[])
+{
+ struct GModule *module;
+ struct
+ {
+ struct Option *type;
+ struct Option *pattern;
+ struct Option *separator;
+ struct Option *mapset;
+ } opt;
+ struct
+ {
+ struct Flag *regex;
+ struct Flag *extended;
+ struct Flag *type;
+ struct Flag *mapset;
+ struct Flag *pretty;
+ struct Flag *full;
+ } flag;
+ int i, n, all, num_types;
+ char *pattern = NULL, separator[2], *buf;
+ regex_t regex;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("general, map management");
+ module->description =
+ _("Lists available GRASS data base files "
+ "of the user-specified data type to standard output.");
+
+ read_list(0);
+
+ opt.type = G_define_option();
+ opt.type->key = "type";
+ opt.type->key_desc = "datatype";
+ opt.type->type = TYPE_STRING;
+ opt.type->required = YES;
+ opt.type->multiple = YES;
+ opt.type->answer = "rast";
+ opt.type->description = "Data type";
+ for (i = 0, n = 0; n < nlist; n++)
+ i += strlen(list[n].alias) + 1;
+ buf = G_malloc(i + 4);
+
+ buf[0] = 0;
+ for (n = 0; n < nlist; n++) {
+ strcat(buf, list[n].alias);
+ strcat(buf, ",");
+ }
+ strcat(buf, "all");
+ opt.type->options = buf;
+
+ opt.pattern = G_define_option();
+ opt.pattern->key = "pattern";
+ opt.pattern->type = TYPE_STRING;
+ opt.pattern->required = NO;
+ opt.pattern->multiple = NO;
+ opt.pattern->answer = "*";
+ opt.pattern->description = _("Map name search pattern (default: all)");
+
+ opt.separator = G_define_option();
+ opt.separator->key = "separator";
+ opt.separator->type = TYPE_STRING;
+ opt.separator->required = NO;
+ opt.separator->multiple = NO;
+ opt.separator->answer = "newline";
+ opt.separator->description =
+ _("One-character output separator, newline, space, or tab");
+
+ opt.mapset = G_define_option();
+ opt.mapset->key = "mapset";
+ opt.mapset->type = TYPE_STRING;
+ opt.mapset->required = NO;
+ opt.mapset->multiple = NO;
+ opt.mapset->description =
+ _("Mapset to list (default: current search path)");
+
+ flag.regex = G_define_flag();
+ flag.regex->key = 'r';
+ flag.regex->description =
+ _("Use basic regular expressions instead of wildcards");
+
+ flag.extended = G_define_flag();
+ flag.extended->key = 'e';
+ flag.extended->description =
+ _("Use extended regular expressions instead of wildcards");
+
+ flag.type = G_define_flag();
+ flag.type->key = 't';
+ flag.type->description = _("Print data types");
+
+ flag.mapset = G_define_flag();
+ flag.mapset->key = 'm';
+ flag.mapset->description = _("Print mapset names");
+
+ flag.pretty = G_define_flag();
+ flag.pretty->key = 'p';
+ flag.pretty->description = _("Pretty printing in human readable format");
+
+ flag.full = G_define_flag();
+ flag.full->key = 'f';
+ flag.full->description = _("Verbose listing (also list map titles)");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ G_free(buf);
+
+ if (flag.regex->answer && flag.extended->answer)
+ G_fatal_error(_("-r and -e are mutually exclusive"));
+
+ if (flag.regex->answer || flag.extended->answer)
+ pattern = opt.pattern->answer;
+ else
+ pattern = wc2regex(opt.pattern->answer);
+
+ if (regcomp(®ex, pattern, (flag.regex->answer ? 0 : REG_EXTENDED) | REG_NOSUB))
+ G_fatal_error(_("Unable to compile regular expression %s"), pattern);
+ G_set_ls_filter(ls_filter, ®ex);
+
+ if (strcmp(opt.separator->answer, "newline") == 0)
+ separator[0] = '\n';
+ else if (strcmp(opt.separator->answer, "space") == 0)
+ separator[0] = ' ';
+ else if (strcmp(opt.separator->answer, "tab") == 0)
+ separator[0] = '\t';
+ else
+ separator[0] = opt.separator->answer[0];
+ separator[1] = 0;
+
+ if (opt.mapset->answer == NULL)
+ opt.mapset->answer = "";
+
+ if (G_strcasecmp(opt.mapset->answer, ".") == 0)
+ opt.mapset->answer = G_mapset();
+
+ for (i = 0; opt.type->answers[i]; i++) {
+ if (strcmp(opt.type->answers[i], "all") == 0)
+ break;
+ }
+ if (opt.type->answers[i]) {
+ all = 1;
+ num_types = nlist;
+ }
+ else {
+ all = 0;
+ num_types = i;
+ }
+
+ for (i = 0; i < num_types; i++) {
+ n = all ? i : parse(opt.type->answers[i]);
+
+ if (flag.full->answer) {
+ char lister[GPATH_MAX];
+
+ sprintf(lister, "%s/etc/lister/%s", G_gisbase(),
+ list[n].element[0]);
+
+ G_debug(3, "lister CMD: %s", lister);
+
+ if (access(lister, 1) == 0) { /* execute permission? */
+ G_spawn(lister, lister, opt.mapset->answer, NULL);
+ continue;
+ }
+ }
+ else
+ do_list(&list[n], pattern, opt.mapset->answer, separator,
+ flag.pretty->answer, flag.type->answer,
+ flag.mapset->answer);
+ }
+
+ if (!flag.pretty->answer && any)
+ fprintf(stdout, "\n");
+
+ if (!flag.regex->answer)
+ G_free(pattern);
+
+ regfree(®ex);
+
+ exit(EXIT_SUCCESS);
+}
+
+static void do_list(
+ const struct list *elem,
+ const char *pattern, const char *mapset, const char *separator,
+ int pretty, int add_type, int add_mapset)
+{
+ char path[GPATH_MAX];
+ const char *element = elem->element[0];
+ const char *alias = elem->alias;
+ char **list;
+ int count;
+ int i;
+
+ if (pretty) {
+ G_list_element(element, alias, mapset, NULL);
+ return;
+ }
+
+ if (!mapset || !*mapset) {
+ int n;
+ for (n = 0; mapset = G__mapset_name(n), mapset; n++)
+ do_list(elem, pattern, mapset, separator,
+ pretty, add_type, add_mapset);
+ return;
+ }
+
+ G__file_name(path, element, "", mapset);
+ if (access(path, 0) != 0)
+ return;
+
+ list = G__ls(path, &count);
+ if (!list)
+ return;
+
+ for (i = 0; i < count; i++) {
+ char *name = list[i];
+
+ if (any)
+ fprintf(stdout, "%s", separator);
+
+ if (add_type)
+ fprintf(stdout, "%s/", alias);
+
+ fprintf(stdout, "%s", name);
+
+ if (add_mapset)
+ fprintf(stdout, "@%s", mapset);
+
+ G_free(name);
+
+ any++;
+ }
+
+ G_free(list);
+}
+
+static int parse(const char *data_type)
+{
+ int n;
+
+ for (n = 0; n < nlist; n++) {
+ if (G_strcasecmp(list[n].alias, data_type) == 0)
+ break;
+ }
+
+ return n;
+}
+
+static int ls_filter(const char *filename, void *closure)
+{
+ return filename[0] != '.' &&
+ regexec((regex_t *) closure, filename, 0, NULL, 0) == 0;
+}
Property changes on: grass/branches/develbranch_6/general/g.mlist/main.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mlist/read_list.c
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/read_list.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/read_list.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,148 @@
+/* Copied from general/manage */
+
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dirent.h>
+#include "global.h"
+
+/*******************************************************************
+read the element list file
+
+ format is:
+
+ # ... comments
+ main element:alias:description:menu text
+ sub element:description
+ sub element:description
+ .
+ .
+ .
+******************************************************************/
+
+static int add_element(const char *, const char *);
+static int empty(const char *);
+static int format_error(const char *, int, const char *);
+
+int read_list(int check_if_empty)
+{
+ FILE *fd;
+ char element_list[600], buf[1024], elem[100], alias[100], desc[100],
+ text[100], *env;
+ int any, line;
+
+ nlist = 0;
+ list = 0;
+ any = 0;
+
+ if ((env = G__getenv("ELEMENT_LIST")))
+ strcpy(element_list, env);
+ else
+ sprintf(element_list, "%s/etc/element_list", G_gisbase());
+ fd = fopen(element_list, "r");
+
+ if (!fd)
+ G_fatal_error("can't open database element list <%s>", element_list);
+
+ line = 0;
+ while (G_getl(buf, sizeof(buf), fd)) {
+ line++;
+ if (*buf == '#')
+ continue;
+ if (*buf == ' ' || *buf == '\t') { /* support element */
+ *desc = 0;
+ if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
+ continue;
+ if (*elem == '#')
+ continue;
+ if (nlist == 0)
+ format_error(element_list, line, buf);
+
+ G_strip(elem);
+ G_strip(desc);
+ add_element(elem, desc);
+ }
+ else { /* main element */
+
+ if (sscanf
+ (buf, "%[^:]:%[^:]:%[^:]:%[^\n]", elem, alias, desc,
+ text) != 4)
+ format_error(element_list, line, buf);
+
+ G_strip(elem);
+ G_strip(alias);
+ G_strip(desc);
+ G_strip(text);
+
+ list =
+ (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
+ list[nlist].mainelem = G_store(elem);
+ list[nlist].alias = G_store(alias);
+ list[nlist].maindesc = G_store(desc);
+ list[nlist].text = G_store(text);
+ list[nlist].nelem = 0;
+ list[nlist].element = 0;
+ list[nlist].desc = 0;
+ list[nlist].status = 0;
+ if (!check_if_empty || !empty(elem)) {
+ list[nlist].status = 1;
+ any = 1;
+ }
+ nlist++;
+ add_element(elem, desc);
+ }
+ }
+
+ fclose(fd);
+
+ return any;
+}
+
+static int add_element(const char *elem, const char *desc)
+{
+ int n;
+ int nelem;
+
+ if (*desc == 0)
+ desc = elem;
+
+ n = nlist - 1;
+ nelem = list[n].nelem++;
+ list[n].element =
+ (char **)G_realloc(list[n].element, (nelem + 1) * sizeof(char *));
+ list[n].element[nelem] = G_store(elem);
+ list[n].desc =
+ (char **)G_realloc(list[n].desc, (nelem + 1) * sizeof(char *));
+ list[n].desc[nelem] = G_store(desc);
+
+ return 0;
+}
+
+static int empty(const char *elem)
+{
+ DIR *dirp;
+ struct dirent *dp;
+ char dir[1024];
+ int any;
+
+ G__file_name(dir, elem, "", G_mapset());
+
+ any = 0;
+ if ((dirp = opendir(dir)) != NULL) {
+ while (!any && (dp = readdir(dirp)) != NULL) {
+ if (dp->d_name[0] != '.')
+ any = 1;
+ }
+ closedir(dirp);
+ }
+
+ return any == 0;
+}
+
+static int format_error(const char *element_list, int line, const char *buf)
+{
+ G_fatal_error(_("Format error: <%s>\nLine: %d\n%s"), element_list, line,
+ buf);
+
+ return 1;
+}
Property changes on: grass/branches/develbranch_6/general/g.mlist/read_list.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mlist/wc2regex.c
===================================================================
--- grass/branches/develbranch_6/general/g.mlist/wc2regex.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mlist/wc2regex.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,46 @@
+#include <grass/gis.h>
+
+char *wc2regex(const char *wc)
+{
+ int i, j;
+ char *regex;
+
+ for (i = 0, j = 2; wc[i]; i++, j++) {
+ switch (wc[i]) {
+ case '.':
+ case '*':
+ j++;
+ break;
+ }
+ }
+ regex = (char *)G_malloc(j + 1);
+ j = 0;
+ regex[j++] = '^';
+ for (i = 0; wc[i]; i++) {
+ switch (wc[i]) {
+ case '.':
+ regex[j++] = '\\';
+ break;
+ case '*':
+ regex[j++] = '.';
+ break;
+ case '?':
+ regex[j++] = '.';
+ continue;
+ case '{':
+ regex[j++] = '(';
+ continue;
+ case '}':
+ regex[j++] = ')';
+ continue;
+ case ',':
+ regex[j++] = '|';
+ continue;
+ }
+ regex[j++] = wc[i];
+ }
+ regex[j++] = '$';
+ regex[j] = 0;
+
+ return regex;
+}
Property changes on: grass/branches/develbranch_6/general/g.mlist/wc2regex.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/Makefile
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/Makefile (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/Makefile 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,14 @@
+MODULE_TOPDIR = ../..
+
+PGM = g.mremove
+
+LIBES = $(GISLIB) $(VECTLIB) $(G3DLIB)
+DEPENDENCIES = $(GISDEP) $(VECTDEP) $(G3DDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+ifneq ($(USE_REGEX),)
+default: cmd
+else
+default:
+endif
Property changes on: grass/branches/develbranch_6/general/g.mremove/Makefile
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/check_reclass.c
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/check_reclass.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/check_reclass.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,62 @@
+#include <string.h>
+#include "global.h"
+
+int check_reclass(const char *name, const char *mapset, int force)
+{
+ char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
+ char **rmaps;
+ int nrmaps;
+
+ if (G_is_reclassed_to(name, mapset, &nrmaps, &rmaps) > 0) {
+ for (; *rmaps; rmaps++) {
+ /* force remove */
+ if (force)
+ G_warning(_("[%s@%s] is a base map for [%s]. Remove forced."),
+ name, mapset, *rmaps);
+ else
+ G_warning(_
+ ("[%s@%s] is a base map. Remove reclassed map first: %s"),
+ name, mapset, *rmaps);
+ }
+
+ if (!force)
+ return 1;
+ }
+
+ if (G_is_reclass(name, mapset, rname, rmapset) > 0 &&
+ G_is_reclassed_to(rname, rmapset, &nrmaps, &rmaps) > 0) {
+ char path[GPATH_MAX];
+ char *p = strchr(rname, '@');
+ char *qname = G_fully_qualified_name(name, mapset);
+
+ if (p)
+ *p = '\0';
+
+ G__file_name_misc(path, "cell_misc", "reclassed_to", rname, rmapset);
+
+ if (nrmaps == 1 && !G_strcasecmp(rmaps[0], qname)) {
+
+ if (remove(path) < 0)
+ G_warning(_
+ ("Removing information about reclassed map from [%s@%s] failed"),
+ rname, rmapset);
+ }
+ else {
+ FILE *fp = fopen(path, "w");
+
+ if (fp) {
+ for (; *rmaps; rmaps++)
+ if (G_strcasecmp(*rmaps, qname))
+ fprintf(fp, "%s\n", *rmaps);
+ fclose(fp);
+ }
+ else
+ G_warning(_
+ ("Removing information about reclassed map from [%s@%s] failed"),
+ rname, rmapset);
+
+ }
+ }
+
+ return 0;
+}
Property changes on: grass/branches/develbranch_6/general/g.mremove/check_reclass.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/description.html
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/description.html (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/description.html 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,26 @@
+<h2>DESCRIPTION</h2>
+
+<em>g.mremove</em> removes data files matching a pattern given by wildcards or POSIX Extended Regular Expressions.
+If the <b>-f</b> force flag is not given then nothing is removed, instead
+the list of selected file names is printed to <tt>stdout</tt>
+as a preview of the files to be deleted.
+
+<h2>EXAMPLE</h2>
+
+Delete all raster maps starting with "<tt>tmp_</tt>" in the current mapset:
+<div class="code"><pre>
+ g.mremove -f "tmp_*"
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em><a HREF="g.remove.html">g.remove</a></em>
+<p>
+<a href="http://en.wikipedia.org/wiki/Regular_expression">Regular expression</a> (from Wikipedia, the free encyclopedia)
+
+<h2>AUTHOR</h2>
+
+Huidae Cho<br>
+grass4u at gmail.com
+
+<p><i>Last changed: $Date: 2008-06-28 03:23:07 -0500 (Sat, 28 Jun 2008) $</i>
Added: grass/branches/develbranch_6/general/g.mremove/do_remove.c
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/do_remove.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/do_remove.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,102 @@
+#include <string.h>
+#include <grass/Vect.h>
+#include <grass/G3d.h>
+#include "global.h"
+
+/*
+ * returns 0 - success
+ * 1 - error
+ */
+int do_remove(int n, char *old)
+{
+ int i, ret;
+
+ /* int len; */
+ char *mapset;
+ int result = 0;
+ int removed = 0;
+ char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+
+ G_message(_("Removing %s <%s>"), list[n].maindesc, old);
+
+ /* len = get_description_len(n); */
+
+ if (G__name_is_fully_qualified(old, xname, xmapset)) {
+ if (strcmp(xmapset, G_mapset()) != 0)
+ G_fatal_error("%s is not in the current mapset (%s)", old,
+ G_mapset());
+ old = xname;
+ }
+
+ if (G_strcasecmp(list[n].alias, "vect") == 0) {
+ if ((mapset = G_find_vector2(old, "")) == NULL) {
+ G_warning(_("Vector map <%s> not found"), old);
+ }
+ else {
+ ret = Vect_delete(old);
+ if (ret != -1) {
+ removed = 1;
+ }
+ else {
+ G_warning(_("couldn't be removed"));
+ result = 1;
+ }
+ }
+ }
+ else {
+ if (G_strcasecmp(list[n].alias, "rast") == 0) {
+ if ((mapset = G_find_cell2(old, "")) == NULL)
+ G_warning(_("Raster map <%s> not found"), old);
+ }
+
+ if (G_strcasecmp(list[n].alias, "rast3d") == 0) {
+ if ((mapset = G_find_grid3(old, "")) == NULL)
+ G_warning(_("3D raster map <%s> not found"), old);
+ }
+
+ for (i = 0; i < list[n].nelem; i++) {
+
+ switch (G_remove(list[n].element[i], old)) {
+ case -1:
+ G_warning(_("%s: couldn't be removed"), list[n].desc[i]);
+ result = 1;
+ break;
+ case 0:
+ if (G_verbose() == G_verbose_max())
+ G_message(_("%s: missing"), list[n].desc[i]);
+ break;
+ case 1:
+ if (G_verbose() == G_verbose_max())
+ G_message(_("%s: removed"), list[n].desc[i]);
+ removed = 1;
+ break;
+ }
+ }
+ }
+
+ if (G_strcasecmp(list[n].element[0], "cell") == 0) {
+ char colr2[GMAPSET_MAX + 6];
+
+ sprintf(colr2, "colr2/%s", G_mapset());
+ switch (G_remove(colr2, old)) {
+ case -1:
+ G_warning("%s: %s", colr2, _("couldn't be removed"));
+ result = 1;
+ break;
+ case 0:
+ if (G_verbose() == G_verbose_max())
+ G_message(_("%s: missing"), colr2);
+ break;
+ case 1:
+ if (G_verbose() == G_verbose_max())
+ G_message(_("%s: removed"), colr2);
+ removed = 1;
+ break;
+ }
+ }
+
+ if (!removed)
+ G_warning(_("<%s> nothing removed"), old);
+
+ return result;
+}
Property changes on: grass/branches/develbranch_6/general/g.mremove/do_remove.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/global.h
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/global.h (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/global.h 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,29 @@
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+struct list
+{
+ char **element; /* list of related elements */
+ char *alias; /* element alias */
+ char **desc; /* description of elements */
+ char *text; /* menu text */
+ int nelem; /* number of elements */
+ char status;
+ char *mainelem; /* main element */
+ char *maindesc; /* main element description */
+};
+
+/* read_list.c */
+int read_list(int);
+
+/* wc2regex.c */
+char *wc2regex(const char *);
+
+/* check_reclass.c */
+int check_reclass(const char *, const char *, int);
+
+/* do_remove.c */
+int do_remove(int, char *);
+
+extern int nlist;
+extern struct list *list;
Property changes on: grass/branches/develbranch_6/general/g.mremove/global.h
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/main.c
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/main.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/main.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,160 @@
+
+/****************************************************************************
+ *
+ * MODULE: g.xremove
+ *
+ * AUTHOR(S): Huidae Cho <grass4u gmail.com>
+ *
+ * Based on general/manage/cmd/remove.c by
+ * CERL (original contributor),
+ * Radim Blazek <radim.blazek gmail.com>,
+ * Cedric Shock <cedricgrass shockfamily.net>,
+ * Huidae Cho <grass4u gmail.com>,
+ * Glynn Clements <glynn gclements.plus.com>,
+ * Jachym Cepicky <jachym les-ejk.cz>,
+ * Markus Neteler <neteler itc.it>,
+ * Martin Landa <landa.martin gmail.com>
+ *
+ * PURPOSE: lets users remove GRASS database files
+ *
+ * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <regex.h>
+#include "global.h"
+
+int nlist;
+struct list *list;
+
+static int ls_filter(const char *, void *);
+
+int main(int argc, char *argv[])
+{
+ struct GModule *module;
+ struct Option **opt, *o;
+ struct
+ {
+ struct Flag *regex;
+ struct Flag *extended;
+ struct Flag *force;
+ struct Flag *basemap;
+ } flag;
+ char *name, *mapset, *location_path, path[GPATH_MAX], **files;
+ char *buf, *buf2;
+ int num_files, rast, result = EXIT_SUCCESS;
+ int i, j, n;
+ regex_t regex;
+
+ G_gisinit(argv[0]);
+
+ module = G_define_module();
+ module->keywords = _("general, map management");
+ module->description =
+ _("Removes data base element files from "
+ "the user's current mapset.");
+
+ flag.regex = G_define_flag();
+ flag.regex->key = 'r';
+ flag.regex->description =
+ _("Use basic regular expressions instead of wildcards");
+
+ flag.extended = G_define_flag();
+ flag.extended->key = 'e';
+ flag.extended->description =
+ _("Use extended regular expressions instead of wildcards");
+
+ flag.force = G_define_flag();
+ flag.force->key = 'f';
+ flag.force->description =
+ _("Force removal (required for actual deletion of files)");
+
+ flag.basemap = G_define_flag();
+ flag.basemap->key = 'b';
+ flag.basemap->description = _("Remove base maps");
+
+ read_list(0);
+
+ opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
+
+ for (n = 0; n < nlist; n++) {
+ o = opt[n] = G_define_option();
+ o->key = list[n].alias;
+ o->type = TYPE_STRING;
+ o->required = NO;
+ o->multiple = YES;
+ buf = G_malloc(64);
+ sprintf(buf, "old,%s,%s", list[n].mainelem, list[n].maindesc);
+ o->gisprompt = buf;
+ buf2 = G_malloc(64);
+ sprintf(buf2, _("%s file(s) to be removed"), list[n].alias);
+ o->description = buf2;
+ }
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ if (flag.regex->answer && flag.extended->answer)
+ G_fatal_error(_("-r and -e are mutually exclusive"));
+
+ for (n = 0; n < nlist; n++) {
+ o = opt[n];
+ G_free((char *)o->gisprompt);
+ G_free((char *)o->description);
+ }
+
+ location_path = G_location_path();
+ mapset = G_mapset();
+
+ for (n = 0; n < nlist; n++) {
+ if (opt[n]->answers) {
+ G__file_name(path, list[n].element[0], "", mapset);
+ if (access(path, 0) != 0)
+ continue;
+ rast = !G_strcasecmp(list[n].alias, "rast");
+ for (i = 0; (name = opt[n]->answers[i]); i++) {
+ if (!flag.regex->answer && !flag.extended->answer)
+ name = wc2regex(name);
+ if (regcomp(®ex, name,
+ (flag.regex->answer ? 0 : REG_EXTENDED) | REG_NOSUB))
+ G_fatal_error(_
+ ("Unable to compile regular expression %s"),
+ name);
+ if (!flag.regex->answer && !flag.extended->answer)
+ G_free(name);
+
+ G_set_ls_filter(ls_filter, ®ex);
+ files = G__ls(path, &num_files);
+ regfree(®ex);
+
+ for (j = 0; j < num_files; j++) {
+ if (!flag.force->answer) {
+ fprintf(stdout, "%s/%s@%s\n", list[n].alias, files[j],
+ mapset);
+ continue;
+ }
+ if (rast &&
+ check_reclass(files[j], mapset, flag.basemap->answer))
+ continue;
+
+ if (do_remove(n, (char *)files[j]) == 1)
+ result = EXIT_FAILURE;
+ }
+ }
+ }
+ }
+
+ exit(result);
+}
+
+static int ls_filter(const char *filename, void *closure)
+{
+ return filename[0] != '.' &&
+ regexec((regex_t *) closure, filename, 0, NULL, 0) == 0;
+}
Property changes on: grass/branches/develbranch_6/general/g.mremove/main.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/read_list.c
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/read_list.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/read_list.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,148 @@
+/* Copied from general/manage */
+
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dirent.h>
+#include "global.h"
+
+/*******************************************************************
+read the element list file
+
+ format is:
+
+ # ... comments
+ main element:alias:description:menu text
+ sub element:description
+ sub element:description
+ .
+ .
+ .
+******************************************************************/
+
+static int add_element(const char *, const char *);
+static int empty(const char *);
+static int format_error(const char *, int, const char *);
+
+int read_list(int check_if_empty)
+{
+ FILE *fd;
+ char element_list[600], buf[1024], elem[100], alias[100], desc[100],
+ text[100], *env;
+ int any, line;
+
+ nlist = 0;
+ list = 0;
+ any = 0;
+
+ if ((env = G__getenv("ELEMENT_LIST")))
+ strcpy(element_list, env);
+ else
+ sprintf(element_list, "%s/etc/element_list", G_gisbase());
+ fd = fopen(element_list, "r");
+
+ if (!fd)
+ G_fatal_error("can't open database element list <%s>", element_list);
+
+ line = 0;
+ while (G_getl(buf, sizeof(buf), fd)) {
+ line++;
+ if (*buf == '#')
+ continue;
+ if (*buf == ' ' || *buf == '\t') { /* support element */
+ *desc = 0;
+ if (sscanf(buf, "%[^:]:%[^\n]", elem, desc) < 1)
+ continue;
+ if (*elem == '#')
+ continue;
+ if (nlist == 0)
+ format_error(element_list, line, buf);
+
+ G_strip(elem);
+ G_strip(desc);
+ add_element(elem, desc);
+ }
+ else { /* main element */
+
+ if (sscanf
+ (buf, "%[^:]:%[^:]:%[^:]:%[^\n]", elem, alias, desc,
+ text) != 4)
+ format_error(element_list, line, buf);
+
+ G_strip(elem);
+ G_strip(alias);
+ G_strip(desc);
+ G_strip(text);
+
+ list =
+ (struct list *)G_realloc(list, (nlist + 1) * sizeof(*list));
+ list[nlist].mainelem = G_store(elem);
+ list[nlist].alias = G_store(alias);
+ list[nlist].maindesc = G_store(desc);
+ list[nlist].text = G_store(text);
+ list[nlist].nelem = 0;
+ list[nlist].element = 0;
+ list[nlist].desc = 0;
+ list[nlist].status = 0;
+ if (!check_if_empty || !empty(elem)) {
+ list[nlist].status = 1;
+ any = 1;
+ }
+ nlist++;
+ add_element(elem, desc);
+ }
+ }
+
+ fclose(fd);
+
+ return any;
+}
+
+static int add_element(const char *elem, const char *desc)
+{
+ int n;
+ int nelem;
+
+ if (*desc == 0)
+ desc = elem;
+
+ n = nlist - 1;
+ nelem = list[n].nelem++;
+ list[n].element =
+ (char **)G_realloc(list[n].element, (nelem + 1) * sizeof(char *));
+ list[n].element[nelem] = G_store(elem);
+ list[n].desc =
+ (char **)G_realloc(list[n].desc, (nelem + 1) * sizeof(char *));
+ list[n].desc[nelem] = G_store(desc);
+
+ return 0;
+}
+
+static int empty(const char *elem)
+{
+ DIR *dirp;
+ struct dirent *dp;
+ char dir[1024];
+ int any;
+
+ G__file_name(dir, elem, "", G_mapset());
+
+ any = 0;
+ if ((dirp = opendir(dir)) != NULL) {
+ while (!any && (dp = readdir(dirp)) != NULL) {
+ if (dp->d_name[0] != '.')
+ any = 1;
+ }
+ closedir(dirp);
+ }
+
+ return any == 0;
+}
+
+static int format_error(const char *element_list, int line, const char *buf)
+{
+ G_fatal_error(_("Format error: <%s>\nLine: %d\n%s"), element_list, line,
+ buf);
+
+ return 1;
+}
Property changes on: grass/branches/develbranch_6/general/g.mremove/read_list.c
___________________________________________________________________
Name: svn:eol-style
+ native
Added: grass/branches/develbranch_6/general/g.mremove/wc2regex.c
===================================================================
--- grass/branches/develbranch_6/general/g.mremove/wc2regex.c (rev 0)
+++ grass/branches/develbranch_6/general/g.mremove/wc2regex.c 2008-09-13 06:01:57 UTC (rev 33421)
@@ -0,0 +1,46 @@
+#include <grass/gis.h>
+
+char *wc2regex(const char *wc)
+{
+ int i, j;
+ char *regex;
+
+ for (i = 0, j = 2; wc[i]; i++, j++) {
+ switch (wc[i]) {
+ case '.':
+ case '*':
+ j++;
+ break;
+ }
+ }
+ regex = (char *)G_malloc(j + 1);
+ j = 0;
+ regex[j++] = '^';
+ for (i = 0; wc[i]; i++) {
+ switch (wc[i]) {
+ case '.':
+ regex[j++] = '\\';
+ break;
+ case '*':
+ regex[j++] = '.';
+ break;
+ case '?':
+ regex[j++] = '.';
+ continue;
+ case '{':
+ regex[j++] = '(';
+ continue;
+ case '}':
+ regex[j++] = ')';
+ continue;
+ case ',':
+ regex[j++] = '|';
+ continue;
+ }
+ regex[j++] = wc[i];
+ }
+ regex[j++] = '$';
+ regex[j] = 0;
+
+ return regex;
+}
Property changes on: grass/branches/develbranch_6/general/g.mremove/wc2regex.c
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the grass-commit
mailing list