1919#include <grass/glocale.h>
2020
2121static const char NULL_STRING [] = "null" ;
22- static int reclass_type (FILE * , char * * , char * * );
22+ static int reclass_type (FILE * , char * * , char * * , char * * );
2323static FILE * fopen_cellhd_old (const char * , const char * );
2424static FILE * fopen_cellhd_new (const char * );
2525static int get_reclass_table (FILE * , struct Reclass * , char * * );
@@ -51,7 +51,7 @@ int Rast_is_reclass(const char *name, const char *mapset, char *rname,
5151 if (fd == NULL )
5252 return -1 ;
5353
54- type = reclass_type (fd , & rname , & rmapset );
54+ type = reclass_type (fd , & rname , & rmapset , NULL );
5555 fclose (fd );
5656 if (type < 0 )
5757 return -1 ;
@@ -135,8 +135,7 @@ int Rast_is_reclassed_to(const char *name, const char *mapset, int *nrmaps,
135135 \param mapset mapset name
136136 \param[out] reclass pointer to Reclass structure
137137
138- \return -1 on error
139- \return type code
138+ \return type code (>=1), 0 if no reclass, -1 on error
140139 */
141140int Rast_get_reclass (const char * name , const char * mapset ,
142141 struct Reclass * reclass )
@@ -149,13 +148,24 @@ int Rast_get_reclass(const char *name, const char *mapset,
149148 return -1 ;
150149 reclass -> name = NULL ;
151150 reclass -> mapset = NULL ;
152- reclass -> type = reclass_type (fd , & reclass -> name , & reclass -> mapset );
153- if (reclass -> type <= 0 ) {
151+ char * error_message = NULL ;
152+ reclass -> type =
153+ reclass_type (fd , & reclass -> name , & reclass -> mapset , & error_message );
154+ if (reclass -> type == 0 ) {
155+ // no reclass
154156 fclose (fd );
155157 return reclass -> type ;
156158 }
159+ if (reclass -> type < 0 ) {
160+ // error
161+ fclose (fd );
162+ G_warning (_ ("Error reading beginning of header file for <%s@%s>: %s" ),
163+ name , mapset , error_message );
164+ if (error_message != NULL )
165+ G_free (error_message );
166+ return reclass -> type ;
167+ }
157168
158- char * error_message = NULL ;
159169 switch (reclass -> type ) {
160170 case RECLASS_TABLE :
161171 stat = get_reclass_table (fd , reclass , & error_message );
@@ -204,10 +214,22 @@ void Rast_free_reclass(struct Reclass *reclass)
204214 }
205215}
206216
207- static int reclass_type (FILE * fd , char * * rname , char * * rmapset )
217+ /**
218+ * \brief Get reclass type if it is a reclass file
219+ *
220+ * \param fd[in] file descriptor
221+ * \param rname[out] name of the reclass from raster
222+ * \param rmapset[out] name of the mapset of the raster
223+ * \param error_message[out] will be assigned a newly error message if not NULL
224+ *
225+ * \returns RECLASS_TABLE if reclass, 0 if not, -1 on error
226+ */
227+ static int reclass_type (FILE * fd , char * * rname , char * * rmapset ,
228+ char * * error_message )
208229{
209- char buf [128 ];
210- char label [128 ], arg [128 ];
230+ char
231+ buf [GNAME_MAX + 128 + 1 ]; // name or mapset plus the label and separator
232+ char label [128 ], arg [GNAME_MAX ];
211233 int i ;
212234 int type ;
213235
@@ -225,10 +247,26 @@ static int reclass_type(FILE *fd, char **rname, char **rmapset)
225247 if (* rmapset )
226248 * * rmapset = '\0' ;
227249 for (i = 0 ; i < 2 ; i ++ ) {
228- if (fgets (buf , sizeof buf , fd ) == NULL )
250+ if (fgets (buf , sizeof buf , fd ) == NULL ) {
251+ if (error_message != NULL ) {
252+ G_asprintf (error_message , _ ("File too short, reading line %d" ),
253+ i + 1 );
254+ }
255+ return -1 ;
256+ }
257+ if (buf [strlen (buf ) - 1 ] != '\n' ) {
258+ if (error_message != NULL ) {
259+ G_asprintf (error_message , _ ("Line too long: %s..." ), buf );
260+ }
229261 return -1 ;
230- if (sscanf (buf , "%[^:]:%s" , label , arg ) != 2 )
262+ }
263+ if (sscanf (buf , "%[^:]:%s" , label , arg ) != 2 ) {
264+ if (error_message != NULL ) {
265+ G_asprintf (error_message , _ ("Format is not key:value: %s" ),
266+ buf );
267+ }
231268 return -1 ;
269+ }
232270 if (strncmp (label , "maps" , 4 ) == 0 ) {
233271 if (* rmapset )
234272 strcpy (* rmapset , arg );
@@ -241,13 +279,30 @@ static int reclass_type(FILE *fd, char **rname, char **rmapset)
241279 else
242280 * rname = G_store (arg );
243281 }
244- else
282+ else {
283+ if (error_message != NULL ) {
284+ G_asprintf (error_message , _ ("Unknown key at line: %s" ), buf );
285+ }
245286 return -1 ;
287+ }
246288 }
247289 if (* * rmapset && * * rname )
248290 return type ;
249- else
291+ else {
292+ // If they do not occur in the two lines we expect them.
293+ if (* * rname && error_message != NULL ) {
294+ G_asprintf (error_message ,
295+ _ ("Mapset not read, only raster name: %s" ), * rname );
296+ }
297+ else if (* * rmapset && error_message != NULL ) {
298+ G_asprintf (error_message ,
299+ _ ("Raster name not read, only mapset: %s" ), * rmapset );
300+ }
301+ else if (error_message != NULL ) {
302+ * error_message = G_store (_ ("Raster name and mapset not read" ));
303+ }
250304 return -1 ;
305+ }
251306}
252307
253308static FILE * fopen_cellhd_old (const char * name , const char * mapset )
0 commit comments