libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00037 #include "xrecord.h" 00038 00039 00051 DxfXrecord * 00052 dxf_xrecord_new () 00053 { 00054 #if DEBUG 00055 DXF_DEBUG_BEGIN 00056 #endif 00057 DxfXrecord *xrecord = NULL; 00058 size_t size; 00059 00060 size = sizeof (DxfXrecord); 00061 /* avoid malloc of 0 bytes */ 00062 if (size == 0) size = 1; 00063 if ((xrecord = malloc (size)) == NULL) 00064 { 00065 fprintf (stderr, 00066 (_("Error in %s () could not allocate memory for a DxfXrecord struct.\n")), 00067 __FUNCTION__); 00068 xrecord = NULL; 00069 } 00070 else 00071 { 00072 memset (xrecord, 0, size); 00073 } 00074 #if DEBUG 00075 DXF_DEBUG_END 00076 #endif 00077 return (xrecord); 00078 } 00079 00080 00094 DxfXrecord * 00095 dxf_xrecord_init 00096 ( 00097 DxfXrecord *xrecord 00099 ) 00100 { 00101 #if DEBUG 00102 DXF_DEBUG_BEGIN 00103 #endif 00104 /* Do some basic checks. */ 00105 if (xrecord == NULL) 00106 { 00107 fprintf (stderr, 00108 (_("Warning in %s () a NULL pointer was passed.\n")), 00109 __FUNCTION__); 00110 xrecord = dxf_xrecord_new (); 00111 } 00112 if (xrecord == NULL) 00113 { 00114 fprintf (stderr, 00115 (_("Error in %s () could not allocate memory for a DxfXrecord struct.\n")), 00116 __FUNCTION__); 00117 return (NULL); 00118 } 00119 xrecord->id_code = 0; 00120 xrecord->dictionary_owner_soft = strdup (""); 00121 xrecord->dictionary_owner_hard = strdup (""); 00122 xrecord->I8 = 0; 00123 xrecord->I16 = 0; 00124 xrecord->I32 = 0; 00125 xrecord->D = 0.0; 00126 xrecord->F = 0.0; 00127 xrecord->next = NULL; 00128 #if DEBUG 00129 DXF_DEBUG_END 00130 #endif 00131 return (xrecord); 00132 } 00133 00134 00152 DxfXrecord * 00153 dxf_xrecord_read 00154 ( 00155 DxfFile *fp, 00157 DxfXrecord *xrecord 00159 ) 00160 { 00161 #if DEBUG 00162 DXF_DEBUG_BEGIN 00163 #endif 00164 char *temp_string = NULL; 00165 00166 /* Do some basic checks. */ 00167 if (fp == NULL) 00168 { 00169 fprintf (stderr, 00170 (_("Error in %s () a NULL file pointer was passed.\n")), 00171 __FUNCTION__); 00172 /* Clean up. */ 00173 free (temp_string); 00174 return (NULL); 00175 } 00176 if (fp->acad_version_number < AutoCAD_14) 00177 { 00178 fprintf (stderr, 00179 (_("Warning in %s () illegal DXF version for this entity.\n")), 00180 __FUNCTION__); 00181 } 00182 if (xrecord == NULL) 00183 { 00184 fprintf (stderr, 00185 (_("Warning in %s () a NULL pointer was passed.\n")), 00186 __FUNCTION__); 00187 xrecord = dxf_xrecord_new (); 00188 xrecord = dxf_xrecord_init (xrecord); 00189 } 00190 (fp->line_number)++; 00191 fscanf (fp->fp, "%[^\n]", temp_string); 00192 while (strcmp (temp_string, "0") != 0) 00193 { 00194 if (ferror (fp->fp)) 00195 { 00196 fprintf (stderr, 00197 (_("Error in %s () while reading from: %s in line: %d.\n")), 00198 __FUNCTION__, fp->filename, fp->line_number); 00199 /* Clean up. */ 00200 free (temp_string); 00201 fclose (fp->fp); 00202 return (NULL); 00203 } 00204 if (strcmp (temp_string, "5") == 0) 00205 { 00206 /* Now follows a string containing a sequential 00207 * id number. */ 00208 (fp->line_number)++; 00209 fscanf (fp->fp, "%x\n", &xrecord->id_code); 00210 } 00211 else if ((fp->acad_version_number >= AutoCAD_13) 00212 && (strcmp (temp_string, "100") == 0)) 00213 { 00214 /* Now follows a string containing the 00215 * subclass marker value. */ 00216 (fp->line_number)++; 00217 fscanf (fp->fp, "%s\n", temp_string); 00218 if (strcmp (temp_string, "AcDbXrecord") != 0) 00219 { 00220 fprintf (stderr, 00221 (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")), 00222 __FUNCTION__, fp->filename, fp->line_number); 00223 } 00224 } 00225 else if (strcmp (temp_string, "330") == 0) 00226 { 00227 /* Now follows a string containing Soft-pointer 00228 * ID/handle to owner dictionary. */ 00229 (fp->line_number)++; 00230 fscanf (fp->fp, "%s\n", xrecord->dictionary_owner_soft); 00231 } 00232 else if (strcmp (temp_string, "360") == 0) 00233 { 00234 /* Now follows a string containing Hard owner 00235 * ID/handle to owner dictionary. */ 00236 (fp->line_number)++; 00237 fscanf (fp->fp, "%s\n", xrecord->dictionary_owner_hard); 00238 } 00239 else if (strcmp (temp_string, "999") == 0) 00240 { 00241 /* Now follows a string containing a comment. */ 00242 (fp->line_number)++; 00243 fscanf (fp->fp, "%s\n", temp_string); 00244 fprintf (stdout, (_("DXF comment: %s\n")), temp_string); 00245 } 00246 else if 00247 ( 00248 ((scanf (temp_string, "%d") >= 1) && (scanf (temp_string, "%d") <= 9)) 00249 || (strcmp (temp_string, "102") == 0) 00250 || (strcmp (temp_string, "105") == 0) 00251 || ((scanf (temp_string, "%d") >= 300) && (scanf (temp_string, "%d") <= 369)) 00252 ) 00253 { 00254 xrecord->group_code = scanf (temp_string, "%d"); 00255 /* Now follows a string value. */ 00256 (fp->line_number)++; 00257 fscanf (fp->fp, "%s\n", xrecord->S); 00258 } 00259 else if 00260 ( 00261 ((scanf (temp_string, "%d") >= 10) && (scanf (temp_string, "%d") <= 59)) 00262 ) 00263 { 00264 xrecord->group_code = scanf (temp_string, "%d"); 00265 /* Now follows a double value. */ 00266 (fp->line_number)++; 00267 fscanf (fp->fp, "%lf\n", &xrecord->D); 00268 } 00269 else if 00270 ( 00271 ((scanf (temp_string, "%d") >= 60) && (scanf (temp_string, "%d") <= 79)) 00272 || ((scanf (temp_string, "%d") >= 170) && (scanf (temp_string, "%d") <= 175)) 00273 ) 00274 { 00275 xrecord->group_code = scanf (temp_string, "%d"); 00276 /* Now follows a 16-bit integer value.. */ 00277 (fp->line_number)++; 00278 fscanf (fp->fp, "%hi\n", &xrecord->I16); 00279 } 00280 else if 00281 ( 00282 ((scanf (temp_string, "%d") >= 90) && (scanf (temp_string, "%d") <= 99)) 00283 ) 00284 { 00285 xrecord->group_code = scanf (temp_string, "%d"); 00286 /* Now follows a 32-bit integer value.. */ 00287 (fp->line_number)++; 00288 fscanf (fp->fp, "%" PRIi32 "\n", &xrecord->I32); 00289 } 00290 else if 00291 ( 00292 ((scanf (temp_string, "%d") >= 140) && (scanf (temp_string, "%d") <= 147)) 00293 ) 00294 { 00295 xrecord->group_code = scanf (temp_string, "%d"); 00296 /* Now follows a float value. */ 00297 (fp->line_number)++; 00298 fscanf (fp->fp, "%f\n", &xrecord->F); 00299 } 00300 else if 00301 ( 00302 ((scanf (temp_string, "%d") >= 280) && (scanf (temp_string, "%d") <= 289)) 00303 ) 00304 { 00305 xrecord->group_code = scanf (temp_string, "%d"); 00306 /* Now follows a 8-bit integer value.. */ 00307 (fp->line_number)++; 00308 fscanf (fp->fp, "%hhi\n", &xrecord->I8); 00309 } 00310 else 00311 { 00312 fprintf (stderr, 00313 (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")), 00314 __FUNCTION__, fp->filename, fp->line_number); 00315 } 00316 } 00317 /* Clean up. */ 00318 free (temp_string); 00319 #if DEBUG 00320 DXF_DEBUG_END 00321 #endif 00322 return (xrecord); 00323 } 00324 00325 00338 int 00339 dxf_xrecord_write 00340 ( 00341 DxfFile *fp, 00343 DxfXrecord *xrecord 00345 ) 00346 { 00347 #if DEBUG 00348 DXF_DEBUG_BEGIN 00349 #endif 00350 char *dxf_entity_name = strdup ("XRECORD"); 00351 00352 /* Do some basic checks. */ 00353 if (fp == NULL) 00354 { 00355 fprintf (stderr, 00356 (_("Error in %s () a NULL file pointer was passed.\n")), 00357 __FUNCTION__); 00358 /* Clean up. */ 00359 free (dxf_entity_name); 00360 return (EXIT_FAILURE); 00361 } 00362 if (xrecord == NULL) 00363 { 00364 fprintf (stderr, 00365 (_("Error in %s () a NULL pointer was passed.\n")), 00366 __FUNCTION__); 00367 /* Clean up. */ 00368 free (dxf_entity_name); 00369 return (EXIT_FAILURE); 00370 } 00371 if (fp->acad_version_number < AutoCAD_14) 00372 { 00373 fprintf (stderr, 00374 (_("Warning in %s () illegal DXF version for this %s entity with id-code: %x.\n")), 00375 __FUNCTION__, dxf_entity_name, xrecord->id_code); 00376 } 00377 /* Start writing output. */ 00378 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00379 if (xrecord->id_code != -1) 00380 { 00381 fprintf (fp->fp, " 5\n%x\n", xrecord->id_code); 00382 } 00393 if ((strcmp (xrecord->dictionary_owner_soft, "") != 0) 00394 && (fp->acad_version_number >= AutoCAD_14)) 00395 { 00396 fprintf (fp->fp, "102\n{ACAD_REACTORS\n"); 00397 fprintf (fp->fp, "330\n%s\n", xrecord->dictionary_owner_soft); 00398 fprintf (fp->fp, "102\n}\n"); 00399 } 00400 if ((strcmp (xrecord->dictionary_owner_hard, "") != 0) 00401 && (fp->acad_version_number >= AutoCAD_14)) 00402 { 00403 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n"); 00404 fprintf (fp->fp, "360\n%s\n", xrecord->dictionary_owner_hard); 00405 fprintf (fp->fp, "102\n}\n"); 00406 } 00407 if (fp->acad_version_number >= AutoCAD_13) 00408 { 00409 fprintf (fp->fp, "100\nAcDbXrecord\n"); 00410 } 00411 fprintf (fp->fp, "%d\n", xrecord->group_code); 00412 if 00413 ( 00414 ((xrecord->group_code >= 1) && (xrecord->group_code <= 9)) 00415 || (xrecord->group_code == 102) 00416 || (xrecord->group_code == 105) 00417 || ((xrecord->group_code >= 300) && (xrecord->group_code <= 369)) 00418 ) 00419 { 00420 fprintf (fp->fp, "%s\n", xrecord->S); 00421 } 00422 else if 00423 ( 00424 ((xrecord->group_code >= 10) && (xrecord->group_code <= 59)) 00425 ) 00426 { 00427 fprintf (fp->fp, "%lf\n", xrecord->D); 00428 } 00429 else if 00430 ( 00431 ((xrecord->group_code >= 60) && (xrecord->group_code <= 79)) 00432 || ((xrecord->group_code >= 170) && (xrecord->group_code <= 175)) 00433 ) 00434 { 00435 fprintf (fp->fp, "%" PRIi16 "\n", xrecord->I16); 00436 } 00437 else if 00438 ( 00439 ((xrecord->group_code >= 90) && (xrecord->group_code <= 99)) 00440 ) 00441 { 00442 fprintf (fp->fp, "%" PRIi32 "\n", xrecord->I32); 00443 } 00444 else if 00445 ( 00446 ((xrecord->group_code >= 140) && (xrecord->group_code <= 147)) 00447 ) 00448 { 00449 fprintf (fp->fp, "%f\n", xrecord->F); 00450 } 00451 else if 00452 ( 00453 ((xrecord->group_code >= 280) && (xrecord->group_code <= 289)) 00454 ) 00455 { 00456 fprintf (fp->fp, "%" PRIi8 "\n", xrecord->I8); 00457 } 00458 else 00459 { 00460 fprintf (stderr, 00461 (_("Warning in %s () unknown group code %d found in DxfXrecord struct with id-code: %x.\n")), 00462 __FUNCTION__, xrecord->group_code, xrecord->id_code); 00463 } 00464 /* Clean up. */ 00465 free (dxf_entity_name); 00466 #if DEBUG 00467 DXF_DEBUG_END 00468 #endif 00469 return (EXIT_SUCCESS); 00470 } 00471 00472 00486 int 00487 dxf_xrecord_free 00488 ( 00489 DxfXrecord *xrecord 00492 ) 00493 { 00494 #if DEBUG 00495 DXF_DEBUG_BEGIN 00496 #endif 00497 /* Do some basic checks. */ 00498 if (xrecord == NULL) 00499 { 00500 fprintf (stderr, 00501 (_("Error in %s () a NULL pointer was passed.\n")), 00502 __FUNCTION__); 00503 return (EXIT_FAILURE); 00504 } 00505 if (xrecord->next != NULL) 00506 { 00507 fprintf (stderr, 00508 (_("Error in %s () pointer to next was not NULL.\n")), 00509 __FUNCTION__); 00510 return (EXIT_FAILURE); 00511 } 00512 free (xrecord->dictionary_owner_soft); 00513 free (xrecord->dictionary_owner_hard); 00514 free (xrecord); 00515 xrecord = NULL; 00516 #if DEBUG 00517 DXF_DEBUG_END 00518 #endif 00519 return (EXIT_SUCCESS); 00520 } 00521 00522 00533 void 00534 dxf_xrecord_free_chain 00535 ( 00536 DxfXrecord *xrecords 00538 ) 00539 { 00540 #ifdef DEBUG 00541 DXF_DEBUG_BEGIN 00542 #endif 00543 if (xrecords == NULL) 00544 { 00545 fprintf (stderr, 00546 (_("Warning in %s () a NULL pointer was passed.\n")), 00547 __FUNCTION__); 00548 } 00549 while (xrecords != NULL) 00550 { 00551 struct DxfXrecord *iter = xrecords->next; 00552 dxf_xrecord_free (xrecords); 00553 xrecords = (DxfXrecord *) iter; 00554 } 00555 #if DEBUG 00556 DXF_DEBUG_END 00557 #endif 00558 } 00559 00560 00561 /* EOF*/