Skip to content

Commit

Permalink
docs: Indent examples with current style (#2500)
Browse files Browse the repository at this point in the history
This indents C code in v.example and r.example using the current indent script.
  • Loading branch information
wenzeslaus authored Jul 26, 2022
1 parent 228e3a7 commit 65cc33c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 124 deletions.
98 changes: 49 additions & 49 deletions doc/raster/r.example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ DCELL d_calc(DCELL x)
*/
int main(int argc, char *argv[])
{
struct Cell_head cellhd; /* it stores region information,
and header information of rasters */
char *name; /* input raster name */
char *result; /* output raster name */
char *mapset; /* mapset name */
void *inrast; /* input buffer */
unsigned char *outrast; /* output buffer */
struct Cell_head cellhd; /* it stores region information,
and header information of rasters */
char *name; /* input raster name */
char *result; /* output raster name */
char *mapset; /* mapset name */
void *inrast; /* input buffer */
unsigned char *outrast; /* output buffer */
int nrows, ncols;
int row, col;
int infd, outfd; /* file descriptor */
RASTER_MAP_TYPE data_type; /* type of the map (CELL/DCELL/...) */
struct History history; /* holds meta-data (title, comments,..) */
int infd, outfd; /* file descriptor */
RASTER_MAP_TYPE data_type; /* type of the map (CELL/DCELL/...) */
struct History history; /* holds meta-data (title, comments,..) */

struct GModule *module; /* GRASS module for parsing arguments */
struct GModule *module; /* GRASS module for parsing arguments */

struct Option *input, *output; /* options */
struct Option *input, *output; /* options */

/* initialize GIS environment */
G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */

/* initialize module */
module = G_define_module();
Expand All @@ -92,17 +92,17 @@ int main(int argc, char *argv[])

/* options and flags parser */
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);

/* stores options and flags to variables */
name = input->answer;
result = output->answer;

/* returns NULL if the map was not found in any mapset,
* mapset name otherwise */
mapset = (char *) G_find_raster2(name, "");
mapset = (char *)G_find_raster2(name, "");
if (mapset == NULL)
G_fatal_error(_("Raster map <%s> not found"), name);
G_fatal_error(_("Raster map <%s> not found"), name);

/* determine the inputmap type (CELL/FCELL/DCELL) */
data_type = Rast_map_type(name, mapset);
Expand All @@ -128,39 +128,39 @@ int main(int argc, char *argv[])

/* for each row */
for (row = 0; row < nrows; row++) {
CELL c;
FCELL f;
DCELL d;

G_percent(row, nrows, 2);

/* read input map */
Rast_get_row(infd, inrast, row, data_type);

/* process the data */
for (col = 0; col < ncols; col++) {
/* use different function for each data type */
switch (data_type) {
case CELL_TYPE:
c = ((CELL *) inrast)[col];
c = c_calc(c); /* calculate */
((CELL *) outrast)[col] = c;
break;
case FCELL_TYPE:
f = ((FCELL *) inrast)[col];
f = f_calc(f); /* calculate */
((FCELL *) outrast)[col] = f;
break;
case DCELL_TYPE:
d = ((DCELL *) inrast)[col];
d = d_calc(d); /* calculate */
((DCELL *) outrast)[col] = d;
break;
}
}

/* write raster row to output raster map */
Rast_put_row(outfd, outrast, data_type);
CELL c;
FCELL f;
DCELL d;

G_percent(row, nrows, 2);

/* read input map */
Rast_get_row(infd, inrast, row, data_type);

/* process the data */
for (col = 0; col < ncols; col++) {
/* use different function for each data type */
switch (data_type) {
case CELL_TYPE:
c = ((CELL *) inrast)[col];
c = c_calc(c); /* calculate */
((CELL *) outrast)[col] = c;
break;
case FCELL_TYPE:
f = ((FCELL *) inrast)[col];
f = f_calc(f); /* calculate */
((FCELL *) outrast)[col] = f;
break;
case DCELL_TYPE:
d = ((DCELL *) inrast)[col];
d = d_calc(d); /* calculate */
((DCELL *) outrast)[col] = d;
break;
}
}

/* write raster row to output raster map */
Rast_put_row(outfd, outrast, data_type);
}

/* memory cleanup */
Expand Down
150 changes: 75 additions & 75 deletions doc/vector/v.example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main(int argc, char *argv[])
struct line_cats *Cats;
int i, type, cat, ncols, nrows, col, more, open3d;
char *mapset, sql[200];
struct GModule *module; /* GRASS module for parsing arguments */
struct GModule *module; /* GRASS module for parsing arguments */
struct Option *old, *new;
dbDriver *driver;
dbHandle handle;
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char *argv[])

/* options and flags parser */
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);

/* Create and initialize struct's where to store points/lines and categories */
Points = Vect_new_line_struct();
Expand All @@ -72,32 +72,32 @@ int main(int argc, char *argv[])
Vect_check_input_output_name(old->answer, new->answer, G_FATAL_EXIT);

if ((mapset = (char *)G_find_vector2(old->answer, "")) == NULL)
G_fatal_error(_("Vector map <%s> not found"), old->answer);
G_fatal_error(_("Vector map <%s> not found"), old->answer);

/* Predetermine level at which a map will be opened for reading
lib/vector/Vlib/open.c
*/
if (Vect_set_open_level(2))
G_fatal_error(_("Unable to set predetermined vector open level"));
G_fatal_error(_("Unable to set predetermined vector open level"));

/* Open existing vector for reading
lib/vector/Vlib/open.c
*/
if (1 > Vect_open_old(&In, old->answer, mapset))
G_fatal_error(_("Unable to open vector map <%s>"), old->answer);
G_fatal_error(_("Unable to open vector map <%s>"), old->answer);

/* Check if old vector is 3D. We should preserve 3D data. */
if (Vect_is_3d(&In))
open3d = WITH_Z;
open3d = WITH_Z;
else
open3d = WITHOUT_Z;
open3d = WITHOUT_Z;

/* Set error handler for input vector map */
Vect_set_error_handler_io(&In, NULL);

/* Open new vector for reading/writing */
if (0 > Vect_open_new(&Out, new->answer, open3d)) {
G_fatal_error(_("Unable to create vector map <%s>"), new->answer);
G_fatal_error(_("Unable to create vector map <%s>"), new->answer);
}

/* Set error handler for output vector map */
Expand All @@ -106,16 +106,16 @@ int main(int argc, char *argv[])
/* Let's get vector layers db connections information */
Fi = Vect_get_field(&In, 1);
if (!Fi) {
G_fatal_error(_("Database connection not defined for layer %d"), 1);
G_fatal_error(_("Database connection not defined for layer %d"), 1);
}

/* Output information useful for debuging
include/vect/dig_structs.h
*/
G_debug(1,
"Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
Fi->number, Fi->name, Fi->driver, Fi->database, Fi->table,
Fi->key);
"Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
Fi->number, Fi->name, Fi->driver, Fi->database, Fi->table,
Fi->key);

/* Prepeare strings for use in db_* calls */
db_init_string(&dbsql);
Expand All @@ -126,20 +126,20 @@ int main(int argc, char *argv[])
/* Prepare database for use */
driver = db_start_driver(Fi->driver);
if (driver == NULL) {
G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
}

/* Set error handler for DB driver */
db_set_error_handler_driver(driver);

db_set_handle(&handle, Fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK) {
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
Fi->database, Fi->driver);
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
Fi->database, Fi->driver);
}
db_set_string(&table_name, Fi->table);
if (db_describe_table(driver, &table_name, &table) != DB_OK) {
G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
}
ncols = db_get_table_number_of_columns(table);

Expand All @@ -152,73 +152,73 @@ int main(int argc, char *argv[])
/* Let's do something with every vector feature in input map... */
/* Read in single line and get it's type */
while ((type = Vect_read_next_line(&In, Points, Cats)) > 0) {
/* If Points contain line... */
if (type == GV_LINE || type == GV_POINT || type == GV_CENTROID) {
if (Vect_cat_get(Cats, 1, &cat) == 0) {
Vect_cat_set(Cats, 1, i);
i++;
}
}
if (type == GV_POINT) {
/* Print out point coordinates */
printf("No:%d\tX:%f\tY:%f\tZ:%f\tCAT:%d\n", i, *Points->x,
*Points->y, *Points->z, cat);

/* Prepeare SQL query to get point attribute data */
sprintf(sql, "select * from %s where %s=%d", Fi->table, Fi->key,
cat);
G_debug(1, "SQL: \"%s\"", sql);
db_set_string(&dbsql, sql);
/* Now execute query */
if (db_open_select_cursor(driver, &dbsql, &cursor, DB_SEQUENTIAL)
!= DB_OK)
G_warning(_("Unable to get attribute data for cat %d"), cat);
else {
/* Result count */
nrows = db_get_num_rows(&cursor);
G_debug(1, "Result count: %d", nrows);
table = db_get_cursor_table(&cursor);
/* Let's output every columns name and value */
while (1) {
if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
G_warning(_("Error while retrieving database record for cat %d"),
cat);
break;
}
if (!more)
break;
for (col = 0; col < ncols; col++) {
column = db_get_table_column(table, col);
db_convert_column_value_to_string(column, &valstr);
printf("%s: %s\t", db_get_column_name(column),
db_get_string(&valstr));
}
printf("\n");
}
db_close_cursor(&cursor);
}
}
/* Only now we write data into new vector */
Vect_write_line(&Out, type, Points, Cats);
/* If Points contain line... */
if (type == GV_LINE || type == GV_POINT || type == GV_CENTROID) {
if (Vect_cat_get(Cats, 1, &cat) == 0) {
Vect_cat_set(Cats, 1, i);
i++;
}
}
if (type == GV_POINT) {
/* Print out point coordinates */
printf("No:%d\tX:%f\tY:%f\tZ:%f\tCAT:%d\n", i, *Points->x,
*Points->y, *Points->z, cat);

/* Prepeare SQL query to get point attribute data */
sprintf(sql, "select * from %s where %s=%d", Fi->table, Fi->key,
cat);
G_debug(1, "SQL: \"%s\"", sql);
db_set_string(&dbsql, sql);
/* Now execute query */
if (db_open_select_cursor(driver, &dbsql, &cursor, DB_SEQUENTIAL)
!= DB_OK)
G_warning(_("Unable to get attribute data for cat %d"), cat);
else {
/* Result count */
nrows = db_get_num_rows(&cursor);
G_debug(1, "Result count: %d", nrows);
table = db_get_cursor_table(&cursor);
/* Let's output every columns name and value */
while (1) {
if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
G_warning(_("Error while retrieving database record for cat %d"),
cat);
break;
}
if (!more)
break;
for (col = 0; col < ncols; col++) {
column = db_get_table_column(table, col);
db_convert_column_value_to_string(column, &valstr);
printf("%s: %s\t", db_get_column_name(column),
db_get_string(&valstr));
}
printf("\n");
}
db_close_cursor(&cursor);
}
}
/* Only now we write data into new vector */
Vect_write_line(&Out, type, Points, Cats);
}

/* Create database for new vector map */
Fin = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
driver = db_start_driver_open_database(Fin->driver, Fin->database);
G_debug(1,
"Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
Fin->number, Fin->name, Fin->driver, Fin->database, Fin->table,
Fin->key);
"Field number:%d; Name:<%s>; Driver:<%s>; Database:<%s>; Table:<%s>; Key:<%s>;\n",
Fin->number, Fin->name, Fin->driver, Fin->database, Fin->table,
Fin->key);

/* Let's copy attribute table data */
if (db_copy_table(Fi->driver, Fi->database, Fi->table,
Fin->driver, Vect_subst_var(Fin->database, &Out),
Fin->table) == DB_FAILED)
G_warning(_("Unable to copy attribute table to vector map <%s>"),
new->answer);
Fin->driver, Vect_subst_var(Fin->database, &Out),
Fin->table) == DB_FAILED)
G_warning(_("Unable to copy attribute table to vector map <%s>"),
new->answer);
else
Vect_map_add_dblink(&Out, Fin->number, Fin->name, Fin->table, Fi->key,
Fin->database, Fin->driver);
Vect_map_add_dblink(&Out, Fin->number, Fin->name, Fin->table, Fi->key,
Fin->database, Fin->driver);

/* Build topology for vector map and close them */
Vect_build(&Out);
Expand Down

0 comments on commit 65cc33c

Please sign in to comment.