GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
get_projname.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: GRASS 5 gis library, get_projname.c
5  * AUTHOR(S): unknown
6  * PURPOSE: Get projection name from user
7  * COPYRIGHT: (C) 2000 by the GRASS Development Team
8  *
9  * This program is free software under the GNU General Public
10  * License (>=v2). Read the file COPYING that comes with GRASS
11  * for details.
12  *
13  *****************************************************************************/
14 
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
21 int G_ask_proj_name(char *proj_id, char *proj_name)
22 {
23  char path[GPATH_MAX], buff[GPATH_MAX], answer[50], *a;
24  struct Key_Value *in_proj_keys;
25  char *Tmp_file;
26  FILE *Tmp_fd = NULL;
27  int in_stat, i, npr;
28 
29  sprintf(path, "%s/etc/projections", G_gisbase());
30  while (access(path, 0) != 0)
31  G_fatal_error(_("%s not found"), path);
32  in_proj_keys = G_read_key_value_file(path, &in_stat);
33  if (in_stat != 0)
34  G_fatal_error(_("ERROR in reading %s"), path);
35  npr = in_proj_keys->nitems;
36  Tmp_file = G_tempfile();
37  if (NULL == (Tmp_fd = fopen(Tmp_file, "w"))) {
38  G_fatal_error(_("Cannot open temp file"));
39  }
40  for (i = 0; i < npr; i++) {
41  fprintf(Tmp_fd, "%s -- %s\n", in_proj_keys->key[i],
42  in_proj_keys->value[i]);
43  }
44  fclose(Tmp_fd);
45 
46  for (;;) {
47 
48  do {
49  fprintf(stderr, _("\n\nPlease specify projection name\n"));
50  fprintf(stderr,
51  _("Enter 'list' for the list of available projections\n"));
52  fprintf(stderr, _("Hit RETURN to cancel request\n"));
53  fprintf(stderr, ">");
54  } while (!G_gets(answer));
55 
56  G_strip(answer);
57  if (strlen(answer) == 0)
58  return -1;
59  if (strcmp(answer, "list") == 0) {
60  char *pager;
61 
62  pager = getenv("GRASS_PAGER");
63  if (!pager || strlen(pager) == 0)
64  pager = "cat";
65 
66  /* Always print interactive output to stderr */
67  sprintf(buff, "%s \"%s\" 1>&2", pager,
68  G_convert_dirseps_to_host(Tmp_file));
69  G_system(buff);
70  }
71  else {
72  a = G_find_key_value(answer, in_proj_keys);
73  if (a == NULL) {
74  fprintf(stderr, _("\ninvalid projection\n"));
75  }
76  else
77  break;
78  }
79  }
80 
81  sprintf(proj_id, "%s", answer);
82  sprintf(proj_name, "%s", a);
83  remove(Tmp_file);
84  return 1;
85 }
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_gets(char *buf)
Definition: gets.c:39
char * G_convert_dirseps_to_host(char *path)
Converts directory separator characters in a string to the native host separator character (/ on Unix...
Definition: paths.c:73
int G_ask_proj_name(char *proj_id, char *proj_name)
Definition: get_projname.c:21
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:47
char * getenv()
char buff[1024]
Definition: g3dcats.c:89
struct Key_Value * G_read_key_value_file(const char *file, int *stat)
Read key/values pairs from file.
Definition: key_value3.c:54
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
return NULL
Definition: dbfopen.c:1394
fclose(fd)
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
int G_system(const char *command)
Run a shell level command.
Definition: system.c:51
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.