[GRASS-dev] adding units and vertical datum metadata to raster
maps
Hamish
hamish_nospam at yahoo.com
Wed May 9 22:31:49 EDT 2007
Hamish wrote:
> > TODO: create a fn in r.info to take care of G_asprint() work, right
> > now the "fancy output" section of the module is an ugly mess.
> > [non-functional code, due to me being lost WRT passing pointers]
Paul Kelly wrote:
> G_asprintf() takes a pointer to a pointer for the first argument, and
> the compose_line() function takes a pointer for the first argument.
> But below compose_line is being passed a pointer to a pointer (&
> operator used on something that was already a pointer) and
> G_asprintf() is only being passed a pointer. I think changing it so
> "line" is passed to compose_line and "&line" to G_asprintf would
> work.
Nope, still doesn't work. main()'s "line" does not get updated, ie
pointer is not returned from compose_line(). In the output I see the
previous "Mapset:" line twice.
(new diff attached)
how/where to use G_free(line)? need to move printline(line) into
compose_line() to avoid having to repeat G_free() in main() for every
call to compose_line()?
Trying that approach (2.diff) and passing the output stream to
compose_line() mostly works, but then I get va_* stdarg errors:
| Location: ðàÿ¿ .... |
thanks,
Hamish
-------------- next part --------------
Index: main.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/raster/r.info/main.c,v
retrieving revision 2.19
diff -u -r2.19 main.c
--- main.c 9 May 2007 15:51:25 -0000 2.19
+++ main.c 10 May 2007 02:11:54 -0000
@@ -16,9 +16,10 @@
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
#include <grass/gis.h>
-#include "local_proto.h"
#include <grass/glocale.h>
+#include "local_proto.h"
#define printline(x) fprintf (out," | %-74.74s |\n",x)
#define divider(x) \
@@ -29,12 +30,12 @@
/*local prototype */
int format_double(double value, char *buf);
-static char *name;
+void compose_line(char *, const char *, ...);
/**************************************************************************/
int main(int argc, char *argv[])
{
- char *mapset;
+ char *name, *mapset;
char *line = NULL;
char tmp1[100], tmp2[100], tmp3[100];
char timebuff[256];
@@ -151,10 +152,14 @@
else
G_fatal_error(_("Cannot allocate memory for string"));
- if (G_asprintf(&line, "Location: %s", G_location()) > 0)
+/* if (G_asprintf(&line, "Location: %s", G_location()) > 0)
printline(line);
else
G_fatal_error(_("Cannot allocate memory for string"));
+*/
+ compose_line( line, "Location: %s", G_location() );
+ printline(line);
+
if (G_asprintf(&line, "DataBase: %s", G_gisdbase()) > 0)
printline(line);
@@ -467,3 +472,16 @@
return 0;
}
+
+void compose_line(char *line, const char *fmt, ...)
+{
+ va_list ap;
+
+ line = NULL;
+ va_start(ap, fmt);
+
+ if( G_asprintf(&line, fmt, ap) <= 0 )
+ G_fatal_error(_("Cannot allocate memory for string"));
+
+ va_end(ap);
+}
-------------- next part --------------
Index: main.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/raster/r.info/main.c,v
retrieving revision 2.19
diff -u -r2.19 main.c
--- main.c 9 May 2007 15:51:25 -0000 2.19
+++ main.c 10 May 2007 02:27:01 -0000
@@ -16,9 +16,10 @@
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
#include <grass/gis.h>
-#include "local_proto.h"
#include <grass/glocale.h>
+#include "local_proto.h"
#define printline(x) fprintf (out," | %-74.74s |\n",x)
#define divider(x) \
@@ -29,12 +30,12 @@
/*local prototype */
int format_double(double value, char *buf);
-static char *name;
+void compose_line(FILE *,const char *, ...);
/**************************************************************************/
int main(int argc, char *argv[])
{
- char *mapset;
+ char *name, *mapset;
char *line = NULL;
char tmp1[100], tmp2[100], tmp3[100];
char timebuff[256];
@@ -151,10 +152,12 @@
else
G_fatal_error(_("Cannot allocate memory for string"));
- if (G_asprintf(&line, "Location: %s", G_location()) > 0)
+/* if (G_asprintf(&line, "Location: %s", G_location()) > 0)
printline(line);
else
G_fatal_error(_("Cannot allocate memory for string"));
+*/
+ compose_line(out, "Location: %s", G_location());
if (G_asprintf(&line, "DataBase: %s", G_gisdbase()) > 0)
printline(line);
@@ -467,3 +470,21 @@
return 0;
}
+
+void compose_line(FILE *out, const char *fmt, ...)
+{
+ char *line = NULL;
+ va_list ap;
+
+ line = NULL;
+ va_start(ap, fmt);
+
+ if( G_asprintf(&line, fmt, ap) <= 0 )
+ G_fatal_error(_("Cannot allocate memory for string"));
+
+ va_end(ap);
+
+ printline(line);
+ G_free(line);
+
+}
More information about the grass-dev
mailing list