GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 }