libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00043 #include "object_ptr.h" 00044 00045 00051 DxfObjectPtr * 00052 dxf_object_ptr_new () 00053 { 00054 #if DEBUG 00055 DXF_DEBUG_BEGIN 00056 #endif 00057 DxfObjectPtr *object_ptr = NULL; 00058 size_t size; 00059 00060 size = sizeof (DxfObjectPtr); 00061 /* avoid malloc of 0 bytes */ 00062 if (size == 0) size = 1; 00063 if ((object_ptr = malloc (size)) == NULL) 00064 { 00065 fprintf (stderr, 00066 (_("Error in %s () could not allocate memory for a DxfObjectPtr struct.\n")), 00067 __FUNCTION__); 00068 object_ptr = NULL; 00069 } 00070 else 00071 { 00072 memset (object_ptr, 0, size); 00073 } 00074 #if DEBUG 00075 DXF_DEBUG_END 00076 #endif 00077 return (object_ptr); 00078 } 00079 00080 00088 DxfObjectPtr * 00089 dxf_object_ptr_init 00090 ( 00091 DxfObjectPtr *object_ptr 00093 ) 00094 { 00095 #if DEBUG 00096 DXF_DEBUG_BEGIN 00097 #endif 00098 /* Do some basic checks. */ 00099 if (object_ptr == NULL) 00100 { 00101 fprintf (stderr, 00102 (_("Warning in %s () a NULL pointer was passed.\n")), 00103 __FUNCTION__); 00104 object_ptr = dxf_object_ptr_new (); 00105 } 00106 if (object_ptr == NULL) 00107 { 00108 fprintf (stderr, 00109 (_("Error in %s () could not allocate memory for a DxfObjectPtr struct.\n")), 00110 __FUNCTION__); 00111 return (NULL); 00112 } 00113 object_ptr->id_code = 0; 00114 object_ptr->dictionary_owner_soft = strdup (""); 00115 object_ptr->dictionary_owner_hard = strdup (""); 00116 object_ptr->xdata->value = NULL; 00117 object_ptr->xdata->length = 0; 00118 object_ptr->xdata->next = NULL; 00119 object_ptr->next = NULL; 00120 #if DEBUG 00121 DXF_DEBUG_END 00122 #endif 00123 return (object_ptr); 00124 } 00125 00126 00138 DxfObjectPtr * 00139 dxf_object_ptr_read 00140 ( 00141 DxfFile *fp, 00143 DxfObjectPtr *object_ptr 00145 ) 00146 { 00147 #if DEBUG 00148 DXF_DEBUG_BEGIN 00149 #endif 00150 char *temp_string = NULL; 00151 00152 /* Do some basic checks. */ 00153 if (fp == NULL) 00154 { 00155 fprintf (stderr, 00156 (_("Error in %s () a NULL file pointer was passed.\n")), 00157 __FUNCTION__); 00158 /* Clean up. */ 00159 free (temp_string); 00160 return (NULL); 00161 } 00162 if (fp->acad_version_number < AutoCAD_14) 00163 { 00164 fprintf (stderr, 00165 (_("Warning in %s () illegal DXF version for this entity.\n")), 00166 __FUNCTION__); 00167 } 00168 if (object_ptr == NULL) 00169 { 00170 fprintf (stderr, 00171 (_("Warning in %s () a NULL pointer was passed.\n")), 00172 __FUNCTION__); 00173 object_ptr = dxf_object_ptr_new (); 00174 object_ptr = dxf_object_ptr_init (object_ptr); 00175 } 00176 (fp->line_number)++; 00177 fscanf (fp->fp, "%[^\n]", temp_string); 00178 while (strcmp (temp_string, "0") != 0) 00179 { 00180 if (ferror (fp->fp)) 00181 { 00182 fprintf (stderr, 00183 (_("Error in %s () while reading from: %s in line: %d.\n")), 00184 __FUNCTION__, fp->filename, fp->line_number); 00185 /* Clean up. */ 00186 free (temp_string); 00187 fclose (fp->fp); 00188 return (NULL); 00189 } 00190 if (strcmp (temp_string, "5") == 0) 00191 { 00192 /* Now follows a string containing a sequential 00193 * id number. */ 00194 (fp->line_number)++; 00195 fscanf (fp->fp, "%x\n", &object_ptr->id_code); 00196 } 00197 else if (strcmp (temp_string, "330") == 0) 00198 { 00199 /* Now follows a string containing Soft-pointer 00200 * ID/handle to owner dictionary. */ 00201 (fp->line_number)++; 00202 fscanf (fp->fp, "%s\n", object_ptr->dictionary_owner_soft); 00203 } 00204 else if (strcmp (temp_string, "360") == 0) 00205 { 00206 /* Now follows a string containing Hard owner 00207 * ID/handle to owner dictionary. */ 00208 (fp->line_number)++; 00209 fscanf (fp->fp, "%s\n", object_ptr->dictionary_owner_hard); 00210 } 00211 else if (strcmp (temp_string, "999") == 0) 00212 { 00213 /* Now follows a string containing a comment. */ 00214 (fp->line_number)++; 00215 fscanf (fp->fp, "%s\n", temp_string); 00216 fprintf (stdout, (_("DXF comment: %s\n")), temp_string); 00217 } 00218 else if (strcmp (temp_string, "1001") == 0) 00219 { 00220 /* Now follows a string containing extended data. */ 00221 (fp->line_number)++; 00222 fscanf (fp->fp, "%s\n", object_ptr->xdata->value); 00223 object_ptr->xdata->length = strlen (object_ptr->xdata->value); 00226 } 00227 else 00228 { 00229 fprintf (stderr, 00230 (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")), 00231 __FUNCTION__, fp->filename, fp->line_number); 00232 } 00233 } 00234 /* Clean up. */ 00235 free (temp_string); 00236 #if DEBUG 00237 DXF_DEBUG_END 00238 #endif 00239 return (object_ptr); 00240 } 00241 00242 00249 int 00250 dxf_object_ptr_write 00251 ( 00252 DxfFile *fp, 00254 DxfObjectPtr *object_ptr 00256 ) 00257 { 00258 #if DEBUG 00259 DXF_DEBUG_BEGIN 00260 #endif 00261 char *dxf_entity_name = strdup ("OBJECT_PTR"); 00262 00263 /* Do some basic checks. */ 00264 if (fp == NULL) 00265 { 00266 fprintf (stderr, 00267 (_("Error in %s () a NULL file pointer was passed.\n")), 00268 __FUNCTION__); 00269 /* Clean up. */ 00270 free (dxf_entity_name); 00271 return (EXIT_FAILURE); 00272 } 00273 if (object_ptr == NULL) 00274 { 00275 fprintf (stderr, 00276 (_("Error in %s () a NULL pointer was passed.\n")), 00277 __FUNCTION__); 00278 /* Clean up. */ 00279 free (dxf_entity_name); 00280 return (EXIT_FAILURE); 00281 } 00282 if (fp->acad_version_number < AutoCAD_14) 00283 { 00284 fprintf (stderr, 00285 (_("Warning in %s () illegal DXF version for this %s entity with id-code: %x.\n")), 00286 __FUNCTION__, dxf_entity_name, object_ptr->id_code); 00287 } 00288 /* Start writing output. */ 00289 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00290 if (object_ptr->id_code != -1) 00291 { 00292 fprintf (fp->fp, " 5\n%x\n", object_ptr->id_code); 00293 } 00304 if ((strcmp (object_ptr->dictionary_owner_soft, "") != 0) 00305 && (fp->acad_version_number >= AutoCAD_14)) 00306 { 00307 fprintf (fp->fp, "102\n{ACAD_REACTORS\n"); 00308 fprintf (fp->fp, "330\n%s\n", object_ptr->dictionary_owner_soft); 00309 fprintf (fp->fp, "102\n}\n"); 00310 } 00311 if ((strcmp (object_ptr->dictionary_owner_hard, "") != 0) 00312 && (fp->acad_version_number >= AutoCAD_14)) 00313 { 00314 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n"); 00315 fprintf (fp->fp, "360\n%s\n", object_ptr->dictionary_owner_hard); 00316 fprintf (fp->fp, "102\n}\n"); 00317 } 00318 while (object_ptr->xdata->value != NULL) 00319 { 00320 fprintf (fp->fp, "1001\n%s\n", object_ptr->xdata->value); 00322 } 00323 /* Clean up. */ 00324 free (dxf_entity_name); 00325 #if DEBUG 00326 DXF_DEBUG_END 00327 #endif 00328 return (EXIT_SUCCESS); 00329 } 00330 00331 00339 int 00340 dxf_object_ptr_free 00341 ( 00342 DxfObjectPtr *object_ptr 00345 ) 00346 { 00347 #if DEBUG 00348 DXF_DEBUG_BEGIN 00349 #endif 00350 struct DxfChar *iter = NULL; 00351 00352 /* Do some basic checks. */ 00353 if (object_ptr == NULL) 00354 { 00355 fprintf (stderr, 00356 (_("Error in %s () a NULL pointer was passed.\n")), 00357 __FUNCTION__); 00358 return (EXIT_FAILURE); 00359 } 00360 if (object_ptr->next != NULL) 00361 { 00362 fprintf (stderr, 00363 (_("Error in %s () pointer to next was not NULL.\n")), 00364 __FUNCTION__); 00365 return (EXIT_FAILURE); 00366 } 00367 free (object_ptr->dictionary_owner_soft); 00368 free (object_ptr->dictionary_owner_hard); 00369 while (object_ptr->xdata->value != NULL) 00370 { 00371 iter = (struct DxfChar *) object_ptr->xdata->next; 00372 free (object_ptr->xdata->value); 00373 free (object_ptr->xdata); 00374 if (iter != NULL) 00375 { 00376 object_ptr->xdata = (DxfChar *) iter; 00377 } 00378 } 00379 free (object_ptr); 00380 free (iter); 00381 object_ptr = NULL; 00382 iter = NULL; 00383 #if DEBUG 00384 DXF_DEBUG_END 00385 #endif 00386 return (EXIT_SUCCESS); 00387 } 00388 00389 00394 void 00395 dxf_object_ptr_free_chain 00396 ( 00397 DxfObjectPtr *objectptrs 00399 ) 00400 { 00401 #ifdef DEBUG 00402 DXF_DEBUG_BEGIN 00403 #endif 00404 if (objectptrs == NULL) 00405 { 00406 fprintf (stderr, 00407 (_("Warning in %s () a NULL pointer was passed.\n")), 00408 __FUNCTION__); 00409 } 00410 while (objectptrs != NULL) 00411 { 00412 struct DxfObjectPtr *iter = objectptrs->next; 00413 dxf_object_ptr_free (objectptrs); 00414 objectptrs = (DxfObjectPtr *) iter; 00415 } 00416 #if DEBUG 00417 DXF_DEBUG_END 00418 #endif 00419 } 00420 00421 00427 int 00428 dxf_object_ptr_get_id_code 00429 ( 00430 DxfObjectPtr *object_ptr 00432 ) 00433 { 00434 #if DEBUG 00435 DXF_DEBUG_BEGIN 00436 #endif 00437 /* Do some basic checks. */ 00438 if (object_ptr == NULL) 00439 { 00440 fprintf (stderr, 00441 (_("Error in %s () a NULL pointer was passed.\n")), 00442 __FUNCTION__); 00443 return (EXIT_FAILURE); 00444 } 00445 if (object_ptr->id_code < 0) 00446 { 00447 fprintf (stderr, 00448 (_("Error in %s () a negative value was found.\n")), 00449 __FUNCTION__); 00450 return (EXIT_FAILURE); 00451 } 00452 #if DEBUG 00453 DXF_DEBUG_END 00454 #endif 00455 return (object_ptr->id_code); 00456 } 00457 00458 00462 DxfObjectPtr * 00463 dxf_object_ptr_set_id_code 00464 ( 00465 DxfObjectPtr *object_ptr, 00467 int id_code 00471 ) 00472 { 00473 #if DEBUG 00474 DXF_DEBUG_BEGIN 00475 #endif 00476 /* Do some basic checks. */ 00477 if (object_ptr == NULL) 00478 { 00479 fprintf (stderr, 00480 (_("Error in %s () a NULL pointer was passed.\n")), 00481 __FUNCTION__); 00482 return (NULL); 00483 } 00484 if (id_code < 0) 00485 { 00486 fprintf (stderr, 00487 (_("Error in %s () a negative value was passed.\n")), 00488 __FUNCTION__); 00489 return (NULL); 00490 } 00491 object_ptr->id_code = id_code; 00492 #if DEBUG 00493 DXF_DEBUG_END 00494 #endif 00495 return (object_ptr); 00496 } 00497 00498 00507 char * 00508 dxf_object_ptr_get_dictionary_owner_soft 00509 ( 00510 DxfObjectPtr *object_ptr 00512 ) 00513 { 00514 #if DEBUG 00515 DXF_DEBUG_BEGIN 00516 #endif 00517 /* Do some basic checks. */ 00518 if (object_ptr == NULL) 00519 { 00520 fprintf (stderr, 00521 (_("Error in %s () a NULL pointer was passed.\n")), 00522 __FUNCTION__); 00523 return (NULL); 00524 } 00525 if (object_ptr->dictionary_owner_soft == NULL) 00526 { 00527 fprintf (stderr, 00528 (_("Error in %s () a NULL pointer was found.\n")), 00529 __FUNCTION__); 00530 return (NULL); 00531 } 00532 #if DEBUG 00533 DXF_DEBUG_END 00534 #endif 00535 return (strdup (object_ptr->dictionary_owner_soft)); 00536 } 00537 00538 00543 DxfObjectPtr * 00544 dxf_object_ptr_set_dictionary_owner_soft 00545 ( 00546 DxfObjectPtr *object_ptr, 00548 char *dictionary_owner_soft 00551 ) 00552 { 00553 #if DEBUG 00554 DXF_DEBUG_BEGIN 00555 #endif 00556 /* Do some basic checks. */ 00557 if (object_ptr == NULL) 00558 { 00559 fprintf (stderr, 00560 (_("Error in %s () a NULL pointer was passed.\n")), 00561 __FUNCTION__); 00562 return (NULL); 00563 } 00564 if (dictionary_owner_soft == NULL) 00565 { 00566 fprintf (stderr, 00567 (_("Error in %s () a NULL pointer was passed.\n")), 00568 __FUNCTION__); 00569 return (NULL); 00570 } 00571 object_ptr->dictionary_owner_soft = strdup (dictionary_owner_soft); 00572 #if DEBUG 00573 DXF_DEBUG_END 00574 #endif 00575 return (object_ptr); 00576 } 00577 00578 00587 char * 00588 dxf_object_ptr_get_dictionary_owner_hard 00589 ( 00590 DxfObjectPtr *object_ptr 00592 ) 00593 { 00594 #if DEBUG 00595 DXF_DEBUG_BEGIN 00596 #endif 00597 /* Do some basic checks. */ 00598 if (object_ptr == NULL) 00599 { 00600 fprintf (stderr, 00601 (_("Error in %s () a NULL pointer was passed.\n")), 00602 __FUNCTION__); 00603 return (NULL); 00604 } 00605 if (object_ptr->dictionary_owner_hard == NULL) 00606 { 00607 fprintf (stderr, 00608 (_("Error in %s () a NULL pointer was found.\n")), 00609 __FUNCTION__); 00610 return (NULL); 00611 } 00612 #if DEBUG 00613 DXF_DEBUG_END 00614 #endif 00615 return (strdup (object_ptr->dictionary_owner_hard)); 00616 } 00617 00618 00623 DxfObjectPtr * 00624 dxf_object_ptr_set_dictionary_owner_hard 00625 ( 00626 DxfObjectPtr *object_ptr, 00628 char *dictionary_owner_hard 00631 ) 00632 { 00633 #if DEBUG 00634 DXF_DEBUG_BEGIN 00635 #endif 00636 /* Do some basic checks. */ 00637 if (object_ptr == NULL) 00638 { 00639 fprintf (stderr, 00640 (_("Error in %s () a NULL pointer was passed.\n")), 00641 __FUNCTION__); 00642 return (NULL); 00643 } 00644 if (dictionary_owner_hard == NULL) 00645 { 00646 fprintf (stderr, 00647 (_("Error in %s () a NULL pointer was passed.\n")), 00648 __FUNCTION__); 00649 return (NULL); 00650 } 00651 object_ptr->dictionary_owner_hard = strdup (dictionary_owner_hard); 00652 #if DEBUG 00653 DXF_DEBUG_END 00654 #endif 00655 return (object_ptr); 00656 } 00657 00658 00667 DxfObjectPtr * 00668 dxf_object_ptr_get_next 00669 ( 00670 DxfObjectPtr *object_ptr 00672 ) 00673 { 00674 #if DEBUG 00675 DXF_DEBUG_BEGIN 00676 #endif 00677 /* Do some basic checks. */ 00678 if (object_ptr == NULL) 00679 { 00680 fprintf (stderr, 00681 (_("Error in %s () a NULL pointer was passed.\n")), 00682 __FUNCTION__); 00683 return (NULL); 00684 } 00685 if (object_ptr->next == NULL) 00686 { 00687 fprintf (stderr, 00688 (_("Error in %s () a NULL pointer was found.\n")), 00689 __FUNCTION__); 00690 return (NULL); 00691 } 00692 #if DEBUG 00693 DXF_DEBUG_END 00694 #endif 00695 return ((DxfObjectPtr *) object_ptr->next); 00696 } 00697 00698 00703 DxfObjectPtr * 00704 dxf_object_ptr_set_next 00705 ( 00706 DxfObjectPtr *object_ptr, 00708 DxfObjectPtr *next 00711 ) 00712 { 00713 #if DEBUG 00714 DXF_DEBUG_BEGIN 00715 #endif 00716 /* Do some basic checks. */ 00717 if (object_ptr == NULL) 00718 { 00719 fprintf (stderr, 00720 (_("Error in %s () a NULL pointer was passed.\n")), 00721 __FUNCTION__); 00722 return (NULL); 00723 } 00724 if (next == NULL) 00725 { 00726 fprintf (stderr, 00727 (_("Error in %s () a NULL pointer was passed.\n")), 00728 __FUNCTION__); 00729 return (NULL); 00730 } 00731 object_ptr->next = (struct DxfObjectPtr *) next; 00732 #if DEBUG 00733 DXF_DEBUG_END 00734 #endif 00735 return (object_ptr); 00736 } 00737 00738 00747 DxfObjectPtr * 00748 dxf_object_ptr_get_last 00749 ( 00750 DxfObjectPtr *object_ptr 00752 ) 00753 { 00754 #if DEBUG 00755 DXF_DEBUG_BEGIN 00756 #endif 00757 /* Do some basic checks. */ 00758 if (object_ptr == NULL) 00759 { 00760 fprintf (stderr, 00761 (_("Error in %s () a NULL pointer was passed.\n")), 00762 __FUNCTION__); 00763 return (NULL); 00764 } 00765 if (object_ptr->next == NULL) 00766 { 00767 fprintf (stderr, 00768 (_("Warning in %s () a NULL pointer was found.\n")), 00769 __FUNCTION__); 00770 return ((DxfObjectPtr *) object_ptr); 00771 } 00772 DxfObjectPtr *iter = (DxfObjectPtr *) object_ptr->next; 00773 while (iter->next != NULL) 00774 { 00775 iter = (DxfObjectPtr *) iter->next; 00776 } 00777 #if DEBUG 00778 DXF_DEBUG_END 00779 #endif 00780 return ((DxfObjectPtr *) iter); 00781 } 00782 00783 00784 /* EOF*/