GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /************************************************************* 00003 * I_list_groups (full) 00004 * I_list_subgroups (group, full) 00005 *************************************************************/ 00006 #include <stdio.h> 00007 #include <string.h> 00008 #include <unistd.h> 00009 #include <stdlib.h> 00010 #include <grass/imagery.h> 00011 #include <grass/spawn.h> 00012 00013 static char *tempfile = NULL; 00014 00015 00016 int I_list_groups(int full) 00017 { 00018 char *element; 00019 int i; 00020 00021 char buf[GPATH_MAX]; 00022 char title[50]; 00023 FILE *ls, *temp; 00024 struct Ref ref; 00025 int any; 00026 00027 if (tempfile == NULL) 00028 tempfile = G_tempfile(); 00029 00030 element = "group"; 00031 G__make_mapset_element(element); 00032 00033 temp = fopen(tempfile, "w"); 00034 if (temp == NULL) 00035 G_fatal_error("can't open any temp files"); 00036 fprintf(temp, "Available groups\n"); 00037 fprintf(temp, "---------------------------------\n"); 00038 00039 any = 0; 00040 strcpy(buf, "cd "); 00041 G__file_name(buf + strlen(buf), element, "", G_mapset()); 00042 strcat(buf, ";ls"); 00043 if (!full) 00044 strcat(buf, " -C"); 00045 /* FIXME: use G__ls() */ 00046 if ((ls = popen(buf, "r"))) { 00047 while (G_getl2(buf, sizeof(buf), ls)) { 00048 any = 1; 00049 fprintf(temp, "%s", buf); 00050 if (full) { 00051 I_get_group_title(buf, title, sizeof(title)); 00052 if (*title) 00053 fprintf(temp, " (%s)", title); 00054 fprintf(temp, "\n"); 00055 I_get_group_ref(buf, &ref); 00056 for (i = 0; i < ref.nfiles; i++) 00057 fprintf(temp, "\t%s in %s\n", ref.file[i].name, 00058 ref.file[i].mapset); 00059 if (ref.nfiles <= 0) 00060 fprintf(temp, "\t** empty **\n"); 00061 I_free_group_ref(&ref); 00062 } 00063 else 00064 fprintf(temp, "\n"); 00065 } 00066 pclose(ls); 00067 } 00068 if (!any) 00069 fprintf(temp, "no group files available\n"); 00070 fprintf(temp, "---------------------------------\n"); 00071 fclose(temp); 00072 G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL); 00073 remove(tempfile); 00074 fprintf(stdout, "hit RETURN to continue -->"); 00075 fflush(stdout); 00076 G_gets(buf); 00077 00078 return 0; 00079 } 00080 00081 int I_list_subgroups(const char *group, int full) 00082 { 00083 char element[GNAME_MAX + 15]; 00084 int i; 00085 00086 char buf[GPATH_MAX]; 00087 FILE *ls, *temp; 00088 struct Ref ref; 00089 int any; 00090 00091 if (tempfile == NULL) 00092 tempfile = G_tempfile(); 00093 00094 sprintf(element, "group/%s/subgroup", group); 00095 G__make_mapset_element(element); 00096 00097 temp = fopen(tempfile, "w"); 00098 if (temp == NULL) 00099 G_fatal_error("Unable to open any temporary file"); 00100 fprintf(temp, "Available subgroups in group %s\n", group); 00101 fprintf(temp, "---------------------------------\n"); 00102 00103 any = 0; 00104 strcpy(buf, "cd "); 00105 G__file_name(buf + strlen(buf), element, "", G_mapset()); 00106 strcat(buf, ";ls"); 00107 if (!full) 00108 strcat(buf, " -C"); 00109 /* FIXME: use G__ls() */ 00110 if ((ls = popen(buf, "r"))) { 00111 while (G_getl2(buf, sizeof(buf), ls)) { 00112 any = 1; 00113 fprintf(temp, "%s\n", buf); 00114 if (full) { 00115 I_get_subgroup_ref(group, buf, &ref); 00116 for (i = 0; i < ref.nfiles; i++) 00117 fprintf(temp, "\t%s in %s\n", ref.file[i].name, 00118 ref.file[i].mapset); 00119 if (ref.nfiles <= 0) 00120 fprintf(temp, "\t** empty **\n"); 00121 I_free_group_ref(&ref); 00122 } 00123 } 00124 pclose(ls); 00125 } 00126 if (!any) 00127 fprintf(temp, "no subgroup files available\n"); 00128 fprintf(temp, "---------------------------------\n"); 00129 fclose(temp); 00130 G_spawn(getenv("GRASS_PAGER"), getenv("GRASS_PAGER"), tempfile, NULL); 00131 remove(tempfile); 00132 fprintf(stdout, "hit RETURN to continue -->"); 00133 fflush(stdout); 00134 G_gets(buf); 00135 00136 return 0; 00137 }