libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00043 #include "dictionary.h" 00044 00045 00051 DxfDictionary * 00052 dxf_dictionary_new () 00053 { 00054 #ifdef DEBUG 00055 DXF_DEBUG_BEGIN 00056 #endif 00057 DxfDictionary *dictionary = NULL; 00058 size_t size; 00059 00060 size = sizeof (DxfDictionary); 00061 /* avoid malloc of 0 bytes */ 00062 if (size == 0) size = 1; 00063 if ((dictionary = malloc (size)) == NULL) 00064 { 00065 fprintf (stderr, 00066 (_("Error in %s () could not allocate memory for a DxfDictionary struct.\n")), 00067 __FUNCTION__); 00068 dictionary = NULL; 00069 } 00070 else 00071 { 00072 memset (dictionary, 0, size); 00073 } 00074 #ifdef DEBUG 00075 DXF_DEBUG_END 00076 #endif 00077 return (dictionary); 00078 } 00079 00080 00088 DxfDictionary * 00089 dxf_dictionary_init 00090 ( 00091 DxfDictionary *dictionary 00093 ) 00094 { 00095 #if DEBUG 00096 DXF_DEBUG_BEGIN 00097 #endif 00098 /* Do some basic checks. */ 00099 if (dictionary == NULL) 00100 { 00101 fprintf (stderr, 00102 (_("Warning in %s () a NULL pointer was passed.\n")), 00103 __FUNCTION__); 00104 dictionary = dxf_dictionary_new (); 00105 } 00106 if (dictionary == NULL) 00107 { 00108 fprintf (stderr, 00109 (_("Error in %s () could not allocate memory for a DxfDictionary struct.\n")), 00110 __FUNCTION__); 00111 return (NULL); 00112 } 00113 dxf_dictionary_set_id_code (dictionary, 0); 00114 dxf_dictionary_set_dictionary_owner_soft (dictionary, strdup ("")); 00115 dxf_dictionary_set_dictionary_owner_hard (dictionary, strdup ("")); 00116 dxf_dictionary_set_entry_name (dictionary, strdup ("")); 00117 dxf_dictionary_set_entry_object_handle (dictionary, strdup ("")); 00118 dxf_dictionary_set_next (dictionary, NULL); 00119 #if DEBUG 00120 DXF_DEBUG_END 00121 #endif 00122 return (dictionary); 00123 } 00124 00125 00136 DxfDictionary * 00137 dxf_dictionary_read 00138 ( 00139 DxfFile *fp, 00141 DxfDictionary *dictionary 00143 ) 00144 { 00145 #if DEBUG 00146 DXF_DEBUG_BEGIN 00147 #endif 00148 char *temp_string = NULL; 00149 00150 /* Do some basic checks. */ 00151 if (fp == NULL) 00152 { 00153 fprintf (stderr, 00154 (_("Error in %s () a NULL file pointer was passed.\n")), 00155 __FUNCTION__); 00156 /* Clean up. */ 00157 free (temp_string); 00158 return (NULL); 00159 } 00160 if (fp->acad_version_number < AutoCAD_13) 00161 { 00162 fprintf (stderr, 00163 (_("Warning in %s () illegal DXF version for this entity.\n")), 00164 __FUNCTION__); 00165 } 00166 if (dictionary == NULL) 00167 { 00168 fprintf (stderr, 00169 (_("Warning in %s () a NULL pointer was passed.\n")), 00170 __FUNCTION__); 00171 dictionary = dxf_dictionary_new (); 00172 dictionary = dxf_dictionary_init (dictionary); 00173 } 00174 (fp->line_number)++; 00175 fscanf (fp->fp, "%[^\n]", temp_string); 00176 while (strcmp (temp_string, "0") != 0) 00177 { 00178 if (ferror (fp->fp)) 00179 { 00180 fprintf (stderr, 00181 (_("Error in %s () while reading from: %s in line: %d.\n")), 00182 __FUNCTION__, fp->filename, fp->line_number); 00183 /* Clean up. */ 00184 free (temp_string); 00185 fclose (fp->fp); 00186 return (NULL); 00187 } 00188 else if (strcmp (temp_string, "3") == 0) 00189 { 00190 /* Now follows a string containing additional 00191 * proprietary data. */ 00192 (fp->line_number)++; 00193 fscanf (fp->fp, "%s\n", dictionary->entry_name); 00194 } 00195 if (strcmp (temp_string, "5") == 0) 00196 { 00197 /* Now follows a string containing a sequential 00198 * id number. */ 00199 (fp->line_number)++; 00200 fscanf (fp->fp, "%x\n", &dictionary->id_code); 00201 } 00202 else if ((fp->acad_version_number >= AutoCAD_13) 00203 && (strcmp (temp_string, "100") == 0)) 00204 { 00205 /* Now follows a string containing the 00206 * subclass marker value. */ 00207 (fp->line_number)++; 00208 fscanf (fp->fp, "%s\n", temp_string); 00209 if (strcmp (temp_string, "AcDbDictionary") != 0) 00210 { 00211 fprintf (stderr, 00212 (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")), 00213 __FUNCTION__, fp->filename, fp->line_number); 00214 } 00215 } 00216 else if (strcmp (temp_string, "330") == 0) 00217 { 00218 /* Now follows a string containing Soft-pointer 00219 * ID/handle to owner dictionary. */ 00220 (fp->line_number)++; 00221 fscanf (fp->fp, "%s\n", dictionary->dictionary_owner_soft); 00222 } 00223 else if (strcmp (temp_string, "350") == 0) 00224 { 00225 /* Now follows a string containing a handle to ae 00226 * entry object. */ 00227 (fp->line_number)++; 00228 fscanf (fp->fp, "%s\n", dictionary->entry_object_handle); 00229 } 00230 else if (strcmp (temp_string, "360") == 0) 00231 { 00232 /* Now follows a string containing Hard owner 00233 * ID/handle to owner dictionary. */ 00234 (fp->line_number)++; 00235 fscanf (fp->fp, "%s\n", dictionary->dictionary_owner_hard); 00236 } 00237 else if (strcmp (temp_string, "999") == 0) 00238 { 00239 /* Now follows a string containing a comment. */ 00240 (fp->line_number)++; 00241 fscanf (fp->fp, "%s\n", temp_string); 00242 fprintf (stdout, (_("DXF comment: %s\n")), temp_string); 00243 } 00244 else 00245 { 00246 fprintf (stderr, 00247 (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")), 00248 __FUNCTION__, fp->filename, fp->line_number); 00249 } 00250 } 00251 /* Clean up. */ 00252 free (temp_string); 00253 #if DEBUG 00254 DXF_DEBUG_END 00255 #endif 00256 return (dictionary); 00257 } 00258 00259 00266 int 00267 dxf_dictionary_write 00268 ( 00269 DxfFile *fp, 00271 DxfDictionary *dictionary 00273 ) 00274 { 00275 #if DEBUG 00276 DXF_DEBUG_BEGIN 00277 #endif 00278 char *dxf_entity_name = strdup ("DICTIONARY"); 00279 00280 /* Do some basic checks. */ 00281 if (fp == NULL) 00282 { 00283 fprintf (stderr, 00284 (_("Error in %s () a NULL file pointer was passed.\n")), 00285 __FUNCTION__); 00286 /* Clean up. */ 00287 free (dxf_entity_name); 00288 return (EXIT_FAILURE); 00289 } 00290 if (dictionary == NULL) 00291 { 00292 fprintf (stderr, 00293 (_("Error in %s () a NULL pointer was passed.\n")), 00294 __FUNCTION__); 00295 /* Clean up. */ 00296 free (dxf_entity_name); 00297 return (EXIT_FAILURE); 00298 } 00299 if (strcmp (dxf_dictionary_get_entry_name (dictionary), "") == 0) 00300 { 00301 fprintf (stderr, 00302 (_("Error in %s () empty entry name string for the %s entity with id-code: %x\n")), 00303 __FUNCTION__, dxf_entity_name, dxf_dictionary_get_id_code (dictionary)); 00304 /* Clean up. */ 00305 free (dxf_entity_name); 00306 return (EXIT_FAILURE); 00307 } 00308 if (fp->acad_version_number < AutoCAD_13) 00309 { 00310 fprintf (stderr, 00311 (_("Warning in %s () illegal DXF version for this %s entity with id-code: %x.\n")), 00312 __FUNCTION__, dxf_entity_name, dxf_dictionary_get_id_code (dictionary)); 00313 } 00314 /* Start writing output. */ 00315 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00316 if (dxf_dictionary_get_id_code (dictionary) != -1) 00317 { 00318 fprintf (fp->fp, " 5\n%x\n", dxf_dictionary_get_id_code (dictionary)); 00319 } 00330 if ((strcmp (dxf_dictionary_get_dictionary_owner_soft (dictionary), "") != 0) 00331 && (fp->acad_version_number >= AutoCAD_14)) 00332 { 00333 fprintf (fp->fp, "102\n{ACAD_REACTORS\n"); 00334 fprintf (fp->fp, "330\n%s\n", dxf_dictionary_get_dictionary_owner_soft (dictionary)); 00335 fprintf (fp->fp, "102\n}\n"); 00336 } 00337 if ((strcmp (dictionary->dictionary_owner_hard, "") != 0) 00338 && (fp->acad_version_number >= AutoCAD_14)) 00339 { 00340 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n"); 00341 fprintf (fp->fp, "360\n%s\n", dxf_dictionary_get_dictionary_owner_hard (dictionary)); 00342 fprintf (fp->fp, "102\n}\n"); 00343 } 00344 if (fp->acad_version_number >= AutoCAD_13) 00345 { 00346 fprintf (fp->fp, "100\nAcDbDictionary\n"); 00347 } 00348 fprintf (fp->fp, " 3\n%s\n", dxf_dictionary_get_entry_name (dictionary)); 00349 fprintf (fp->fp, "350\n%s\n", dxf_dictionary_get_entry_object_handle (dictionary)); 00350 /* Clean up. */ 00351 free (dxf_entity_name); 00352 #if DEBUG 00353 DXF_DEBUG_END 00354 #endif 00355 return (EXIT_SUCCESS); 00356 } 00357 00358 00366 int 00367 dxf_dictionary_free 00368 ( 00369 DxfDictionary *dictionary 00372 ) 00373 { 00374 #if DEBUG 00375 DXF_DEBUG_BEGIN 00376 #endif 00377 /* Do some basic checks. */ 00378 if (dictionary == NULL) 00379 { 00380 fprintf (stderr, 00381 (_("Error in %s () a NULL pointer was passed.\n")), 00382 __FUNCTION__); 00383 return (EXIT_FAILURE); 00384 } 00385 if (dictionary->next != NULL) 00386 { 00387 fprintf (stderr, 00388 (_("Error in %s () pointer to next was not NULL.\n")), 00389 __FUNCTION__); 00390 return (EXIT_FAILURE); 00391 } 00392 free (dxf_dictionary_get_dictionary_owner_soft (dictionary)); 00393 free (dxf_dictionary_get_dictionary_owner_hard (dictionary)); 00394 free (dxf_dictionary_get_entry_name (dictionary)); 00395 free (dxf_dictionary_get_entry_object_handle (dictionary)); 00396 free (dictionary); 00397 dictionary = NULL; 00398 #if DEBUG 00399 DXF_DEBUG_END 00400 #endif 00401 return (EXIT_SUCCESS); 00402 } 00403 00404 00409 void 00410 dxf_dictionary_free_chain 00411 ( 00412 DxfDictionary *dictionaries 00414 ) 00415 { 00416 #ifdef DEBUG 00417 DXF_DEBUG_BEGIN 00418 #endif 00419 if (dictionaries == NULL) 00420 { 00421 fprintf (stderr, 00422 (_("Warning in %s () a NULL pointer was passed.\n")), 00423 __FUNCTION__); 00424 } 00425 while (dictionaries != NULL) 00426 { 00427 struct DxfDictionary *iter = dictionaries->next; 00428 dxf_dictionary_free (dictionaries); 00429 dictionaries = (DxfDictionary *) iter; 00430 } 00431 #if DEBUG 00432 DXF_DEBUG_END 00433 #endif 00434 } 00435 00436 00442 int 00443 dxf_dictionary_get_id_code 00444 ( 00445 DxfDictionary *dictionary 00447 ) 00448 { 00449 #if DEBUG 00450 DXF_DEBUG_BEGIN 00451 #endif 00452 /* Do some basic checks. */ 00453 if (dictionary == NULL) 00454 { 00455 fprintf (stderr, 00456 (_("Error in %s () a NULL pointer was passed.\n")), 00457 __FUNCTION__); 00458 return (EXIT_FAILURE); 00459 } 00460 if (dictionary->id_code < 0) 00461 { 00462 fprintf (stderr, 00463 (_("Error in %s () a negative value was found in the id-code member.\n")), 00464 __FUNCTION__); 00465 return (EXIT_FAILURE); 00466 } 00467 #if DEBUG 00468 DXF_DEBUG_END 00469 #endif 00470 return (dictionary->id_code); 00471 } 00472 00473 00477 DxfDictionary * 00478 dxf_dictionary_set_id_code 00479 ( 00480 DxfDictionary *dictionary, 00482 int id_code 00486 ) 00487 { 00488 #if DEBUG 00489 DXF_DEBUG_BEGIN 00490 #endif 00491 /* Do some basic checks. */ 00492 if (dictionary == NULL) 00493 { 00494 fprintf (stderr, 00495 (_("Error in %s () a NULL pointer was passed.\n")), 00496 __FUNCTION__); 00497 return (NULL); 00498 } 00499 if (id_code < 0) 00500 { 00501 fprintf (stderr, 00502 (_("Error in %s () a negative id-code value was passed.\n")), 00503 __FUNCTION__); 00504 return (NULL); 00505 } 00506 dictionary->id_code = id_code; 00507 #if DEBUG 00508 DXF_DEBUG_END 00509 #endif 00510 return (dictionary); 00511 } 00512 00513 00522 char * 00523 dxf_dictionary_get_dictionary_owner_soft 00524 ( 00525 DxfDictionary *dictionary 00527 ) 00528 { 00529 #if DEBUG 00530 DXF_DEBUG_BEGIN 00531 #endif 00532 /* Do some basic checks. */ 00533 if (dictionary == NULL) 00534 { 00535 fprintf (stderr, 00536 (_("Error in %s () a NULL pointer was passed.\n")), 00537 __FUNCTION__); 00538 return (NULL); 00539 } 00540 if (dictionary->dictionary_owner_soft == NULL) 00541 { 00542 fprintf (stderr, 00543 (_("Error in %s () a NULL pointer was found in the dictionary_owner_soft member.\n")), 00544 __FUNCTION__); 00545 return (NULL); 00546 } 00547 #if DEBUG 00548 DXF_DEBUG_END 00549 #endif 00550 return (strdup (dictionary->dictionary_owner_soft)); 00551 } 00552 00553 00558 DxfDictionary * 00559 dxf_dictionary_set_dictionary_owner_soft 00560 ( 00561 DxfDictionary *dictionary, 00563 char *dictionary_owner_soft 00566 ) 00567 { 00568 #if DEBUG 00569 DXF_DEBUG_BEGIN 00570 #endif 00571 /* Do some basic checks. */ 00572 if (dictionary == NULL) 00573 { 00574 fprintf (stderr, 00575 (_("Error in %s () a NULL pointer was passed.\n")), 00576 __FUNCTION__); 00577 return (NULL); 00578 } 00579 if (dictionary_owner_soft == NULL) 00580 { 00581 fprintf (stderr, 00582 (_("Error in %s () a NULL pointer was passed.\n")), 00583 __FUNCTION__); 00584 return (NULL); 00585 } 00586 dictionary->dictionary_owner_soft = strdup (dictionary_owner_soft); 00587 #if DEBUG 00588 DXF_DEBUG_END 00589 #endif 00590 return (dictionary); 00591 } 00592 00593 00602 char * 00603 dxf_dictionary_get_dictionary_owner_hard 00604 ( 00605 DxfDictionary *dictionary 00607 ) 00608 { 00609 #if DEBUG 00610 DXF_DEBUG_BEGIN 00611 #endif 00612 /* Do some basic checks. */ 00613 if (dictionary == NULL) 00614 { 00615 fprintf (stderr, 00616 (_("Error in %s () a NULL pointer was passed.\n")), 00617 __FUNCTION__); 00618 return (NULL); 00619 } 00620 if (dictionary->dictionary_owner_hard == NULL) 00621 { 00622 fprintf (stderr, 00623 (_("Error in %s () a NULL pointer was found in the dictionary_owner_hard member.\n")), 00624 __FUNCTION__); 00625 return (NULL); 00626 } 00627 #if DEBUG 00628 DXF_DEBUG_END 00629 #endif 00630 return (strdup (dictionary->dictionary_owner_hard)); 00631 } 00632 00633 00638 DxfDictionary * 00639 dxf_dictionary_set_dictionary_owner_hard 00640 ( 00641 DxfDictionary *dictionary, 00643 char *dictionary_owner_hard 00646 ) 00647 { 00648 #if DEBUG 00649 DXF_DEBUG_BEGIN 00650 #endif 00651 /* Do some basic checks. */ 00652 if (dictionary == NULL) 00653 { 00654 fprintf (stderr, 00655 (_("Error in %s () a NULL pointer was passed.\n")), 00656 __FUNCTION__); 00657 return (NULL); 00658 } 00659 if (dictionary_owner_hard == NULL) 00660 { 00661 fprintf (stderr, 00662 (_("Error in %s () a NULL pointer was passed.\n")), 00663 __FUNCTION__); 00664 return (NULL); 00665 } 00666 dictionary->dictionary_owner_hard = strdup (dictionary_owner_hard); 00667 #if DEBUG 00668 DXF_DEBUG_END 00669 #endif 00670 return (dictionary); 00671 } 00672 00673 00679 char * 00680 dxf_dictionary_get_entry_name 00681 ( 00682 DxfDictionary *dictionary 00684 ) 00685 { 00686 #if DEBUG 00687 DXF_DEBUG_BEGIN 00688 #endif 00689 /* Do some basic checks. */ 00690 if (dictionary == NULL) 00691 { 00692 fprintf (stderr, 00693 (_("Error in %s () a NULL pointer was passed.\n")), 00694 __FUNCTION__); 00695 return (NULL); 00696 } 00697 if (dictionary->entry_name == NULL) 00698 { 00699 fprintf (stderr, 00700 (_("Error in %s () a NULL pointer was found in the entry_name member.\n")), 00701 __FUNCTION__); 00702 return (NULL); 00703 } 00704 #if DEBUG 00705 DXF_DEBUG_END 00706 #endif 00707 return (strdup (dictionary->entry_name)); 00708 } 00709 00710 00714 DxfDictionary * 00715 dxf_dictionary_set_entry_name 00716 ( 00717 DxfDictionary *dictionary, 00719 char *entry_name 00721 ) 00722 { 00723 #if DEBUG 00724 DXF_DEBUG_BEGIN 00725 #endif 00726 /* Do some basic checks. */ 00727 if (dictionary == NULL) 00728 { 00729 fprintf (stderr, 00730 (_("Error in %s () a NULL pointer was passed.\n")), 00731 __FUNCTION__); 00732 return (NULL); 00733 } 00734 if (entry_name == NULL) 00735 { 00736 fprintf (stderr, 00737 (_("Error in %s () a NULL pointer was passed.\n")), 00738 __FUNCTION__); 00739 return (NULL); 00740 } 00741 dictionary->entry_name = strdup (entry_name); 00742 #if DEBUG 00743 DXF_DEBUG_END 00744 #endif 00745 return (dictionary); 00746 } 00747 00748 00754 char * 00755 dxf_dictionary_get_entry_object_handle 00756 ( 00757 DxfDictionary *dictionary 00759 ) 00760 { 00761 #if DEBUG 00762 DXF_DEBUG_BEGIN 00763 #endif 00764 /* Do some basic checks. */ 00765 if (dictionary == NULL) 00766 { 00767 fprintf (stderr, 00768 (_("Error in %s () a NULL pointer was passed.\n")), 00769 __FUNCTION__); 00770 return (NULL); 00771 } 00772 if (dictionary->entry_object_handle == NULL) 00773 { 00774 fprintf (stderr, 00775 (_("Error in %s () a NULL pointer was found in the entry_object_handle member.\n")), 00776 __FUNCTION__); 00777 return (NULL); 00778 } 00779 #if DEBUG 00780 DXF_DEBUG_END 00781 #endif 00782 return (strdup (dictionary->entry_object_handle)); 00783 } 00784 00785 00789 DxfDictionary * 00790 dxf_dictionary_set_entry_object_handle 00791 ( 00792 DxfDictionary *dictionary, 00794 char *entry_object_handle 00797 ) 00798 { 00799 #if DEBUG 00800 DXF_DEBUG_BEGIN 00801 #endif 00802 /* Do some basic checks. */ 00803 if (dictionary == NULL) 00804 { 00805 fprintf (stderr, 00806 (_("Error in %s () a NULL pointer was passed.\n")), 00807 __FUNCTION__); 00808 return (NULL); 00809 } 00810 if (entry_object_handle == NULL) 00811 { 00812 fprintf (stderr, 00813 (_("Error in %s () a NULL pointer was passed.\n")), 00814 __FUNCTION__); 00815 return (NULL); 00816 } 00817 dictionary->entry_object_handle = strdup (entry_object_handle); 00818 #if DEBUG 00819 DXF_DEBUG_END 00820 #endif 00821 return (dictionary); 00822 } 00823 00824 00833 DxfDictionary * 00834 dxf_dictionary_get_next 00835 ( 00836 DxfDictionary *dictionary 00838 ) 00839 { 00840 #if DEBUG 00841 DXF_DEBUG_BEGIN 00842 #endif 00843 /* Do some basic checks. */ 00844 if (dictionary == NULL) 00845 { 00846 fprintf (stderr, 00847 (_("Error in %s () a NULL pointer was passed.\n")), 00848 __FUNCTION__); 00849 return (NULL); 00850 } 00851 if (dictionary->next == NULL) 00852 { 00853 fprintf (stderr, 00854 (_("Error in %s () a NULL pointer was found in the next member.\n")), 00855 __FUNCTION__); 00856 return (NULL); 00857 } 00858 #if DEBUG 00859 DXF_DEBUG_END 00860 #endif 00861 return ((DxfDictionary *) dictionary->next); 00862 } 00863 00864 00869 DxfDictionary * 00870 dxf_dictionary_set_next 00871 ( 00872 DxfDictionary *dictionary, 00874 DxfDictionary *next 00877 ) 00878 { 00879 #if DEBUG 00880 DXF_DEBUG_BEGIN 00881 #endif 00882 /* Do some basic checks. */ 00883 if (dictionary == NULL) 00884 { 00885 fprintf (stderr, 00886 (_("Error in %s () a NULL pointer was passed.\n")), 00887 __FUNCTION__); 00888 return (NULL); 00889 } 00890 if (next == NULL) 00891 { 00892 fprintf (stderr, 00893 (_("Error in %s () a NULL pointer was passed.\n")), 00894 __FUNCTION__); 00895 return (NULL); 00896 } 00897 dictionary->next = (struct DxfDictionary *) next; 00898 #if DEBUG 00899 DXF_DEBUG_END 00900 #endif 00901 return (dictionary); 00902 } 00903 00904 00913 DxfDictionary * 00914 dxf_dictionary_get_last 00915 ( 00916 DxfDictionary *dictionary 00918 ) 00919 { 00920 #if DEBUG 00921 DXF_DEBUG_BEGIN 00922 #endif 00923 /* Do some basic checks. */ 00924 if (dictionary == NULL) 00925 { 00926 fprintf (stderr, 00927 (_("Error in %s () a NULL pointer was passed.\n")), 00928 __FUNCTION__); 00929 return (NULL); 00930 } 00931 if (dictionary->next == NULL) 00932 { 00933 fprintf (stderr, 00934 (_("Warning in %s () a NULL pointer was found in the next member.\n")), 00935 __FUNCTION__); 00936 return ((DxfDictionary *) dictionary); 00937 } 00938 DxfDictionary *iter = (DxfDictionary *) dictionary->next; 00939 while (iter->next != NULL) 00940 { 00941 iter = (DxfDictionary *) iter->next; 00942 } 00943 #if DEBUG 00944 DXF_DEBUG_END 00945 #endif 00946 return ((DxfDictionary *) dictionary->next); 00947 } 00948 00949 00950 /* EOF*/