libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00038 #include "thumbnail.h" 00039 00040 00068 DxfThumbnail * 00069 dxf_thumbnail_new () 00070 { 00071 #if DEBUG 00072 DXF_DEBUG_BEGIN 00073 #endif 00074 DxfThumbnail *thumbnail = NULL; 00075 size_t size; 00076 00077 size = sizeof (DxfThumbnail); 00078 /* avoid malloc of 0 bytes */ 00079 if (size == 0) size = 1; 00080 if ((thumbnail = malloc (size)) == NULL) 00081 { 00082 fprintf (stderr, 00083 (_("Error in %s () could not allocate memory for a DxfThumbnail struct.\n")), 00084 __FUNCTION__); 00085 thumbnail = NULL; 00086 } 00087 else 00088 { 00089 memset (thumbnail, 0, size); 00090 } 00091 #if DEBUG 00092 DXF_DEBUG_END 00093 #endif 00094 return (thumbnail); 00095 } 00096 00097 00124 DxfThumbnail * 00125 dxf_thumbnail_init 00126 ( 00127 DxfThumbnail *thumbnail 00129 ) 00130 { 00131 #if DEBUG 00132 DXF_DEBUG_BEGIN 00133 #endif 00134 int i; 00135 00136 /* Do some basic checks. */ 00137 if (thumbnail == NULL) 00138 { 00139 fprintf (stderr, 00140 (_("Warning in %s () a NULL pointer was passed.\n")), 00141 __FUNCTION__); 00142 thumbnail = dxf_thumbnail_new (); 00143 } 00144 if (thumbnail == NULL) 00145 { 00146 fprintf (stderr, 00147 (_("Error in %s () could not allocate memory for a DxfThumbnail struct.\n")), 00148 __FUNCTION__); 00149 return (NULL); 00150 } 00151 thumbnail->number_of_bytes = 0; 00152 for (i = 0; i < DXF_MAX_PARAM; i++) 00153 { 00154 thumbnail->preview_image_data[i] = strdup (""); 00155 } 00156 #if DEBUG 00157 DXF_DEBUG_END 00158 #endif 00159 return (thumbnail); 00160 } 00161 00162 00193 DxfThumbnail * 00194 dxf_thumbnail_read 00195 ( 00196 DxfFile *fp, 00198 DxfThumbnail *thumbnail 00200 ) 00201 { 00202 #if DEBUG 00203 DXF_DEBUG_BEGIN 00204 #endif 00205 char *temp_string = NULL; 00206 int i; 00207 int preview_data_length = 0; 00208 00209 /* Do some basic checks. */ 00210 if (fp == NULL) 00211 { 00212 fprintf (stderr, 00213 (_("Error in %s () a NULL file pointer was passed.\n")), 00214 __FUNCTION__); 00215 /* Clean up. */ 00216 free (temp_string); 00217 return (NULL); 00218 } 00219 if (thumbnail == NULL) 00220 { 00221 fprintf (stderr, 00222 (_("Warning in %s () a NULL pointer was passed.\n")), 00223 __FUNCTION__); 00224 thumbnail = dxf_thumbnail_new (); 00225 thumbnail = dxf_thumbnail_init (thumbnail); 00226 } 00227 if (fp->acad_version_number < AutoCAD_2000) 00228 { 00229 fprintf (stderr, 00230 (_("Warning in %s () illegal DXF version for this entity.\n")), 00231 __FUNCTION__); 00232 } 00233 i = 0; 00234 (fp->line_number)++; 00235 fscanf (fp->fp, "%[^\n]", temp_string); 00236 while (strcmp (temp_string, "0") != 0) 00237 { 00238 if (ferror (fp->fp)) 00239 { 00240 fprintf (stderr, 00241 (_("Error in %s () while reading from: %s in line: %d.\n")), 00242 __FUNCTION__, fp->filename, fp->line_number); 00243 fclose (fp->fp); 00244 /* Clean up. */ 00245 free (temp_string); 00246 return (NULL); 00247 } 00248 else if (strcmp (temp_string, "90") == 0) 00249 { 00250 /* Now follows a string containing the 00251 * number of bytes value. */ 00252 (fp->line_number)++; 00253 fscanf (fp->fp, "%d\n", &thumbnail->number_of_bytes); 00254 } 00255 else if (strcmp (temp_string, "310") == 0) 00256 { 00257 /* Now follows a string containing additional 00258 * proprietary data. */ 00259 (fp->line_number)++; 00260 fscanf (fp->fp, "%s\n", thumbnail->preview_image_data[i]); 00261 preview_data_length = preview_data_length + strlen (thumbnail->preview_image_data[i]); 00262 i++; 00263 } 00264 else if (strcmp (temp_string, "999") == 0) 00265 { 00266 /* Now follows a string containing a comment. */ 00267 (fp->line_number)++; 00268 fscanf (fp->fp, "%s\n", temp_string); 00269 fprintf (stdout, "DXF comment: %s\n", temp_string); 00270 } 00271 else 00272 { 00273 fprintf (stderr, 00274 (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")), 00275 __FUNCTION__, fp->filename, fp->line_number); 00276 } 00277 } 00278 /* Handle omitted members and/or illegal values. */ 00279 if (preview_data_length != thumbnail->number_of_bytes) 00280 { 00281 fprintf (stderr, 00282 (_("Warning in %s () read %d preview data bytes from %s while %d were expected.\n")), 00283 __FUNCTION__, preview_data_length, 00284 fp->filename, thumbnail->number_of_bytes); 00285 } 00286 /* Clean up. */ 00287 free (temp_string); 00288 #if DEBUG 00289 DXF_DEBUG_END 00290 #endif 00291 return (thumbnail); 00292 } 00293 00294 00317 int 00318 dxf_thumbnail_write 00319 ( 00320 DxfFile *fp, 00322 DxfThumbnail *thumbnail 00324 ) 00325 { 00326 #if DEBUG 00327 DXF_DEBUG_BEGIN 00328 #endif 00329 char *dxf_entity_name = strdup ("THUMBNAILIMAGE"); 00330 int i; 00331 00332 /* Do some basic checks. */ 00333 if (fp == NULL) 00334 { 00335 fprintf (stderr, 00336 (_("Error in %s () a NULL file pointer was passed.\n")), 00337 __FUNCTION__); 00338 /* Clean up. */ 00339 free (dxf_entity_name); 00340 return (EXIT_FAILURE); 00341 } 00342 if (thumbnail == NULL) 00343 { 00344 fprintf (stderr, 00345 (_("Error in %s () a NULL pointer was passed.\n")), 00346 __FUNCTION__); 00347 /* Clean up. */ 00348 free (dxf_entity_name); 00349 return (EXIT_FAILURE); 00350 } 00351 if (thumbnail->number_of_bytes < 1) 00352 { 00353 fprintf (stderr, 00354 (_("Error in %s () number of bytes was 0 or less.\n")), 00355 __FUNCTION__); 00356 /* Clean up. */ 00357 free (dxf_entity_name); 00358 return (EXIT_FAILURE); 00359 } 00360 if (fp->acad_version_number < AutoCAD_2000) 00361 { 00362 fprintf (stderr, 00363 (_("Warning in %s () illegal DXF version for this entity.\n")), 00364 __FUNCTION__); 00365 } 00366 /* Start writing output. */ 00367 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00368 fprintf (fp->fp, " 90\n%d\n", thumbnail->number_of_bytes); 00369 i = 0; 00370 while (strlen (thumbnail->preview_image_data[i]) > 0) 00371 { 00372 fprintf (fp->fp, "310\n%s\n", thumbnail->preview_image_data[i]); 00373 i++; 00374 } 00375 /* Clean up. */ 00376 free (dxf_entity_name); 00377 #if DEBUG 00378 DXF_DEBUG_END 00379 #endif 00380 return (EXIT_SUCCESS); 00381 } 00382 00383 00410 int 00411 dxf_thumbnail_free 00412 ( 00413 DxfThumbnail *thumbnail 00416 ) 00417 { 00418 #if DEBUG 00419 DXF_DEBUG_BEGIN 00420 #endif 00421 int i; 00422 00423 /* Do some basic checks. */ 00424 if (thumbnail == NULL) 00425 { 00426 fprintf (stderr, 00427 (_("Error in %s () a NULL pointer was passed.\n")), 00428 __FUNCTION__); 00429 return (EXIT_FAILURE); 00430 } 00431 for (i = 0; i < DXF_MAX_PARAM; i++) 00432 { 00433 free (thumbnail->preview_image_data[i]); 00434 } 00435 free (thumbnail); 00436 thumbnail = NULL; 00437 #if DEBUG 00438 DXF_DEBUG_END 00439 #endif 00440 return (EXIT_SUCCESS); 00441 } 00442 00443 00444 /* EOF */