66#include <grass/gjson.h>
77
88int report (struct Map_info * In , int afield , int nfield , int action ,
9- const char * format )
9+ enum OutputFormat format )
1010{
1111 int i , j , line , nlines , ltype , node , nnodes ;
1212 int cat_line , cat_node [2 ];
@@ -25,9 +25,11 @@ int report(struct Map_info *In, int afield, int nfield, int action,
2525
2626 if (action == TOOL_REPORT ) {
2727 struct boxlist * List ;
28+
2829 G_JSON_Value * root_value = NULL ;
2930 G_JSON_Array * root_array = NULL ;
30- if (format && strcmp (format , "json" ) == 0 ) {
31+
32+ if (format == FORMAT_JSON ) {
3133 root_value = G_json_value_init_array ();
3234 root_array = G_json_array (root_value );
3335 }
@@ -75,7 +77,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
7577 G_warning (_ ("%d points found: %g %g %g line category: %d" ),
7678 nnodes , x , y , z , cat_line );
7779 }
78- if (root_array ) {
80+ if (format == FORMAT_JSON ) {
7981 G_JSON_Value * item_value = G_json_value_init_object ();
8082 G_JSON_Object * item_obj = G_json_object (item_value );
8183
@@ -92,7 +94,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
9294 }
9395 }
9496
95- if (root_value ) {
97+ if (format == FORMAT_JSON ) {
9698 char * json_str = G_json_serialize_to_string_pretty (root_value );
9799 if (json_str ) {
98100 fprintf (stdout , "%s\n" , json_str );
@@ -109,10 +111,13 @@ int report(struct Map_info *In, int afield, int nfield, int action,
109111
110112 List = Vect_new_list ();
111113
112- G_JSON_Value * root_val = (format && strcmp (format , "json" ) == 0 )
113- ? G_json_value_init_array ()
114- : NULL ;
115- G_JSON_Array * root_arr = G_json_array (root_val );
114+ G_JSON_Value * root_val = NULL ;
115+ G_JSON_Array * root_arr = NULL ;
116+
117+ if (format == FORMAT_JSON ) {
118+ root_val = G_json_value_init_array ();
119+ root_arr = G_json_array (root_val );
120+ }
116121
117122 for (i = 1 ; i <= nlines ; i ++ ) {
118123
@@ -144,7 +149,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
144149 G_JSON_Value * lines_val =
145150 root_arr ? G_json_value_init_array () : NULL ;
146151
147- if (root_arr ) {
152+ if (format == FORMAT_JSON ) {
148153 G_json_object_set_number (
149154 G_json_value_get_object (item_val ), "node_cat" ,
150155 Cats -> cat [j ]);
@@ -162,7 +167,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
162167 /* Loop through all cats of line */
163168 for (l = 0 ; l < Cats2 -> n_cats ; l ++ ) {
164169 if (Cats2 -> field [l ] == afield ) {
165- if (root_arr )
170+ if (format == FORMAT_JSON )
166171 G_json_array_append_number (
167172 G_json_array (lines_val ),
168173 Cats2 -> cat [l ]);
@@ -175,7 +180,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
175180 }
176181 }
177182 }
178- if (root_arr ) {
183+ if (format == FORMAT_JSON ) {
179184 G_json_object_set_value (
180185 G_json_value_get_object (item_val ), "lines" ,
181186 lines_val );
@@ -187,7 +192,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
187192 }
188193 }
189194 }
190- if (root_val ) {
195+ if (format == FORMAT_JSON ) {
191196 char * s = G_json_serialize_to_string_pretty (root_val );
192197 if (s ) {
193198 fprintf (stdout , "%s\n" , s );
0 commit comments