Skip to content

Commit

Permalink
d.redraw: Rewritten in C (OSGeo#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuidaeCho authored May 21, 2024
1 parent b02a48c commit 75375c9
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 99 deletions.
1 change: 1 addition & 0 deletions display/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SUBDIRS = \
d.rast \
d.rast.arrow \
d.rast.num \
d.redraw \
d.rgb \
d.rhumbline \
d.text \
Expand Down
11 changes: 11 additions & 0 deletions display/d.redraw/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

MODULE_TOPDIR = ../..

PGM = d.redraw

LIBES = $(DISPLAYLIB) $(GISLIB)
DEPENDENCIES = $(DISPLAYDEP) $(GISDEP)

include $(MODULE_TOPDIR)/include/Make/Module.make

default: cmd
18 changes: 18 additions & 0 deletions display/d.redraw/d.redraw.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>DESCRIPTION</h2>

<em>d.redraw</em> redraws the content of the currently selected monitor. The
active monitor can be selected with <em>d.mon</em>.

<h2>SEE ALSO</h2>

<em>
<a href="d.erase.html">d.erase</a>,
<a href="d.rast.html">d.rast</a>,
<a href="d.vect.html">d.vect</a>,
<a href="d.mon.html">d.mon</a>
</em>

<h2>AUTHOR</h2>

Huidae Cho, New Mexico State University<br>
Based on the <em>d.redraw</em> script by Martin Landa, Czech Republic
108 changes: 108 additions & 0 deletions display/d.redraw/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/****************************************************************************
*
* MODULE: d.redraw
* AUTHOR(S): Huidae Cho <grass4u gmail.com>
* Based on scripts/d.redraw/d.redraw.py by Martin Landa
* PURPOSE: Redraws the content of currently selected monitor
* COPYRIGHT: (C) 2024 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 <string.h>
#include <grass/gis.h>
#include <grass/spawn.h>
#include <grass/display.h>
#include <grass/glocale.h>

#define LINES_SIZE_INC 1024
#define LINE_LEN 1024

int main(int argc, char **argv)
{
struct GModule *module;
const char *mon;
char element[GPATH_MAX], cmd_file[GPATH_MAX];
FILE *fp;
int lines_size, num_lines, num_comment_lines;
char **lines, line[LINE_LEN];
char **cmd_argv;
int i;

G_gisinit(argv[0]);

module = G_define_module();
G_add_keyword(_("display"));
G_add_keyword(_("graphics"));
G_add_keyword(_("monitors"));
module->description =
_("Redraws the content of currently selected monitor.");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

if (!(mon = G_getenv_nofatal("MONITOR")))
G_fatal_error(_("No graphics device selected. Use d.mon to select "
"graphics device."));

D_open_driver();
D_close_driver();

G_temp_element(element);
strcat(element, "/MONITORS/");
strcat(element, mon);
G_file_name(cmd_file, element, "cmd", G_mapset());

if (!(fp = fopen(cmd_file, "r")))
G_fatal_error(_("Unable to open file '%s' for reading."), cmd_file);

lines_size = num_lines = num_comment_lines = 0;
lines = NULL;

/* read and save cmd lines; run display commands now */
while (G_getl2(line, LINE_LEN, fp)) {
/* don't add d.redraw this time */
if (strcmp(line, "d.redraw") == 0) {
/* remove its comment lines above */
num_lines -= num_comment_lines;
continue;
}
if (lines_size == num_lines) {
lines_size += LINES_SIZE_INC;
lines = G_realloc(lines, sizeof(char *) * lines_size);
}
lines[num_lines++] = G_store(line);

if (*line == '#') {
num_comment_lines++;
/* render next command into the same existing file */
if (strstr(line, "# GRASS_RENDER_") == line)
putenv(G_store(line + 2));
continue;
}
num_comment_lines = 0;

/* split line by space; double-quote delimiters protect spaces */
cmd_argv = G_tokenize2(line, " ", "\"");
/* run display command */
G_vspawn_ex(cmd_argv[0], (const char **)cmd_argv);
G_free_tokens(cmd_argv);
}

fclose(fp);

/* write out cmd file without d.redraw */
if (!(fp = fopen(cmd_file, "w")))
G_fatal_error(_("Unable to open file '%s' for writing."), cmd_file);

for (i = 0; i < num_lines; i++)
fprintf(fp, "%s\n", lines[i]);

fclose(fp);

exit(EXIT_SUCCESS);
}
1 change: 0 additions & 1 deletion scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ SUBDIRS = \
d.polar \
d.rast.edit \
d.rast.leg \
d.redraw \
d.shade \
d.what.rast \
d.what.vect \
Expand Down
7 changes: 0 additions & 7 deletions scripts/d.redraw/Makefile

This file was deleted.

19 changes: 0 additions & 19 deletions scripts/d.redraw/d.redraw.html

This file was deleted.

72 changes: 0 additions & 72 deletions scripts/d.redraw/d.redraw.py

This file was deleted.

0 comments on commit 75375c9

Please sign in to comment.