libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00062 #include "layer_index.h" 00063 00064 00070 DxfLayerIndex * 00071 dxf_layer_index_new () 00072 { 00073 #if DEBUG 00074 DXF_DEBUG_BEGIN 00075 #endif 00076 DxfLayerIndex *layer_index = NULL; 00077 size_t size; 00078 00079 size = sizeof (DxfLayerIndex); 00080 /* avoid malloc of 0 bytes */ 00081 if (size == 0) size = 1; 00082 if ((layer_index = malloc (size)) == NULL) 00083 { 00084 fprintf (stderr, 00085 (_("Error in %s () could not allocate memory for a DxfLayerIndex struct.\n")), 00086 __FUNCTION__); 00087 layer_index = NULL; 00088 } 00089 else 00090 { 00091 memset (layer_index, 0, size); 00092 } 00093 #if DEBUG 00094 DXF_DEBUG_END 00095 #endif 00096 return (layer_index); 00097 } 00098 00099 00107 DxfLayerIndex * 00108 dxf_layer_index_init 00109 ( 00110 DxfLayerIndex *layer_index 00112 ) 00113 { 00114 #if DEBUG 00115 DXF_DEBUG_BEGIN 00116 #endif 00117 int i; 00118 00119 /* Do some basic checks. */ 00120 if (layer_index == NULL) 00121 { 00122 fprintf (stderr, 00123 (_("Warning in %s () a NULL pointer was passed.\n")), 00124 __FUNCTION__); 00125 layer_index = dxf_layer_index_new (); 00126 } 00127 if (layer_index == NULL) 00128 { 00129 fprintf (stderr, 00130 (_("Error in %s () could not allocate memory for a DxfLayerIndex struct.\n")), 00131 __FUNCTION__); 00132 return (NULL); 00133 } 00134 layer_index->id_code = 0; 00135 layer_index->dictionary_owner_soft = strdup (""); 00136 layer_index->dictionary_owner_hard = strdup (""); 00137 layer_index->time_stamp = 0; 00138 layer_index->layer_name = (DxfLayerName *) dxf_layer_name_new (); 00139 layer_index->layer_name = (DxfLayerName *) dxf_layer_name_init (layer_index->layer_name); 00140 layer_index->layer_name->name = strdup (""); 00141 for (i = 0; i < DXF_MAX_PARAM; i++) 00142 { 00143 layer_index->number_of_entries[i] = 0; 00144 layer_index->hard_owner_reference[i] = strdup (""); 00145 } 00146 layer_index->next = NULL; 00147 #if DEBUG 00148 DXF_DEBUG_END 00149 #endif 00150 return (layer_index); 00151 } 00152 00153 00165 DxfLayerIndex * 00166 dxf_layer_index_read 00167 ( 00168 DxfFile *fp, 00170 DxfLayerIndex *layer_index 00172 ) 00173 { 00174 #if DEBUG 00175 DXF_DEBUG_BEGIN 00176 #endif 00177 char *temp_string = NULL; 00178 DxfLayerName *iter; 00179 int i; 00180 int j; 00181 int k; 00182 00183 /* Do some basic checks. */ 00184 if (fp == NULL) 00185 { 00186 fprintf (stderr, 00187 (_("Error in %s () a NULL file pointer was passed.\n")), 00188 __FUNCTION__); 00189 /* Clean up. */ 00190 free (temp_string); 00191 return (NULL); 00192 } 00193 if (fp->acad_version_number < AutoCAD_14) 00194 { 00195 fprintf (stderr, 00196 (_("Warning in %s () illegal DXF version for this entity.\n")), 00197 __FUNCTION__); 00198 } 00199 if (layer_index == NULL) 00200 { 00201 fprintf (stderr, 00202 (_("Warning in %s () a NULL pointer was passed.\n")), 00203 __FUNCTION__); 00204 layer_index = dxf_layer_index_new (); 00205 layer_index = dxf_layer_index_init (layer_index); 00206 } 00207 i = 0; 00208 j = 0; 00209 k = 0; 00210 iter = (DxfLayerName *) layer_index->layer_name; /* Pointer to first entry. */ 00211 (fp->line_number)++; 00212 fscanf (fp->fp, "%[^\n]", temp_string); 00213 while (strcmp (temp_string, "0") != 0) 00214 { 00215 if (ferror (fp->fp)) 00216 { 00217 fprintf (stderr, 00218 (_("Error in %s () while reading from: %s in line: %d.\n")), 00219 __FUNCTION__, fp->filename, fp->line_number); 00220 /* Clean up. */ 00221 free (temp_string); 00222 fclose (fp->fp); 00223 return (NULL); 00224 } 00225 if (strcmp (temp_string, "5") == 0) 00226 { 00227 /* Now follows a string containing a sequential 00228 * id number. */ 00229 (fp->line_number)++; 00230 fscanf (fp->fp, "%x\n", &layer_index->id_code); 00231 } 00232 if (strcmp (temp_string, "8") == 0) 00233 { 00234 /* Now follows a string containing a layer name 00235 * (multiple entries may exist). */ 00236 if (i > 0) /* Create a pointer for the next entry. */ 00237 { 00238 dxf_layer_name_init ((DxfLayerName *) iter->next); 00239 iter = (DxfLayerName *) iter->next; 00240 } 00241 (fp->line_number)++; 00242 fscanf (fp->fp, "%s\n", iter->name); 00243 i++; /* Increase number of entries. */ 00244 } 00245 if (strcmp (temp_string, "40") == 0) 00246 { 00247 /* Now follows a string containing a time stamp. */ 00248 (fp->line_number)++; 00249 fscanf (fp->fp, "%lf\n", &layer_index->time_stamp); 00250 } 00251 if (strcmp (temp_string, "90") == 0) 00252 { 00253 /* Now follows a string containing a number of 00254 * entries in the LAYER_INDEX list (multiple 00255 * entries may exist). */ 00256 (fp->line_number)++; 00257 fscanf (fp->fp, "%d\n", &layer_index->number_of_entries[j]); 00258 j++; 00259 } 00260 else if ((fp->acad_version_number >= AutoCAD_13) 00261 && (strcmp (temp_string, "100") == 0)) 00262 { 00263 /* Now follows a string containing the 00264 * subclass marker value. */ 00265 (fp->line_number)++; 00266 fscanf (fp->fp, "%s\n", temp_string); 00267 if ((strcmp (temp_string, "AcDbIndex") != 0) 00268 && (strcmp (temp_string, "AcDbLayerIndex") != 0)) 00269 { 00270 fprintf (stderr, 00271 (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")), 00272 __FUNCTION__, fp->filename, fp->line_number); 00273 } 00274 } 00275 else if (strcmp (temp_string, "330") == 0) 00276 { 00277 /* Now follows a string containing Soft-pointer 00278 * ID/handle to owner dictionary. */ 00279 (fp->line_number)++; 00280 fscanf (fp->fp, "%s\n", layer_index->dictionary_owner_soft); 00281 } 00282 else if ((strcmp (temp_string, "360") == 0) 00283 && (k == 0)) 00284 { 00285 /* Now follows a string containing Hard owner 00286 * ID/handle to owner dictionary. */ 00287 (fp->line_number)++; 00288 fscanf (fp->fp, "%s\n", layer_index->dictionary_owner_hard); 00289 k++; 00290 } 00291 else if ((strcmp (temp_string, "360") == 0) 00292 && (k > 0)) 00293 { 00294 /* Now follows a string containing a Hard owner 00295 * reference LAYER_INDEX (multiple entries may 00296 * exist). */ 00297 (fp->line_number)++; 00298 fscanf (fp->fp, "%s\n", layer_index->hard_owner_reference[k]); 00299 k++; 00301 } 00302 else if (strcmp (temp_string, "999") == 0) 00303 { 00304 /* Now follows a string containing a comment. */ 00305 (fp->line_number)++; 00306 fscanf (fp->fp, "%s\n", temp_string); 00307 fprintf (stdout, (_("DXF comment: %s\n")), temp_string); 00308 } 00309 else 00310 { 00311 fprintf (stderr, 00312 (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")), 00313 __FUNCTION__, fp->filename, fp->line_number); 00314 } 00315 } 00316 /* Clean up. */ 00317 free (temp_string); 00318 #if DEBUG 00319 DXF_DEBUG_END 00320 #endif 00321 return (layer_index); 00322 } 00323 00324 00331 int 00332 dxf_layer_index_write 00333 ( 00334 DxfFile *fp, 00336 DxfLayerIndex *layer_index 00338 ) 00339 { 00340 #if DEBUG 00341 DXF_DEBUG_BEGIN 00342 #endif 00343 char *dxf_entity_name = strdup ("LAYER_INDEX"); 00344 DxfLayerName *iter; 00345 int i; 00346 00347 /* Do some basic checks. */ 00348 if (fp == NULL) 00349 { 00350 fprintf (stderr, 00351 (_("Error in %s () a NULL file pointer was passed.\n")), 00352 __FUNCTION__); 00353 /* Clean up. */ 00354 free (dxf_entity_name); 00355 return (EXIT_FAILURE); 00356 } 00357 if (layer_index == NULL) 00358 { 00359 fprintf (stderr, 00360 (_("Error in %s () a NULL pointer was passed.\n")), 00361 __FUNCTION__); 00362 /* Clean up. */ 00363 free (dxf_entity_name); 00364 return (EXIT_FAILURE); 00365 } 00366 if (fp->acad_version_number < AutoCAD_14) 00367 { 00368 fprintf (stderr, 00369 (_("Warning in %s () illegal DXF version for this %s entity with id-code: %x.\n")), 00370 __FUNCTION__, dxf_entity_name, layer_index->id_code); 00371 } 00372 /* Start writing output. */ 00373 fprintf (fp->fp, " 0\n%s\n", dxf_entity_name); 00374 if (layer_index->id_code != -1) 00375 { 00376 fprintf (fp->fp, " 5\n%x\n", layer_index->id_code); 00377 } 00388 if ((strcmp (layer_index->dictionary_owner_soft, "") != 0) 00389 && (fp->acad_version_number >= AutoCAD_14)) 00390 { 00391 fprintf (fp->fp, "102\n{ACAD_REACTORS\n"); 00392 fprintf (fp->fp, "330\n%s\n", layer_index->dictionary_owner_soft); 00393 fprintf (fp->fp, "102\n}\n"); 00394 } 00395 if ((strcmp (layer_index->dictionary_owner_hard, "") != 0) 00396 && (fp->acad_version_number >= AutoCAD_14)) 00397 { 00398 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n"); 00399 fprintf (fp->fp, "360\n%s\n", layer_index->dictionary_owner_hard); 00400 fprintf (fp->fp, "102\n}\n"); 00401 } 00402 if (fp->acad_version_number >= AutoCAD_13) 00403 { 00404 fprintf (fp->fp, "100\nAcDbIndex\n"); 00405 } 00406 fprintf (fp->fp, " 40\n%lf\n", layer_index->time_stamp); 00407 iter = (DxfLayerName *) layer_index->layer_name; 00408 i = 0; 00409 while (strlen (iter->name) > 0) 00410 { 00411 fprintf (fp->fp, " 8\n%s\n", iter->name); 00412 iter = (DxfLayerName *) iter->next; 00413 i++; 00414 } 00415 i = 0; 00416 while (strlen (layer_index->layer_name->name) > 0) 00417 { 00418 fprintf (fp->fp, "360\n%s\n", layer_index->hard_owner_reference[i]); 00419 fprintf (fp->fp, " 90\n%d\n", layer_index->number_of_entries[i]); 00420 i++; 00422 } 00423 /* Clean up. */ 00424 free (dxf_entity_name); 00425 #if DEBUG 00426 DXF_DEBUG_END 00427 #endif 00428 return (EXIT_SUCCESS); 00429 } 00430 00431 00439 int 00440 dxf_layer_index_free 00441 ( 00442 DxfLayerIndex *layer_index 00445 ) 00446 { 00447 #if DEBUG 00448 DXF_DEBUG_BEGIN 00449 #endif 00450 int i; 00451 00452 /* Do some basic checks. */ 00453 if (layer_index == NULL) 00454 { 00455 fprintf (stderr, 00456 (_("Error in %s () a NULL pointer was passed.\n")), 00457 __FUNCTION__); 00458 return (EXIT_FAILURE); 00459 } 00460 if (layer_index->next != NULL) 00461 { 00462 fprintf (stderr, 00463 (_("Error in %s () pointer to next was not NULL.\n")), 00464 __FUNCTION__); 00465 return (EXIT_FAILURE); 00466 } 00467 free (layer_index->dictionary_owner_soft); 00468 free (layer_index->dictionary_owner_hard); 00469 dxf_layer_name_free_chain (layer_index->layer_name); 00470 for (i = 0; i < DXF_MAX_PARAM; i++) 00471 { 00472 free (layer_index->hard_owner_reference[i]); 00473 } 00474 free (layer_index); 00475 layer_index = NULL; 00476 #if DEBUG 00477 DXF_DEBUG_END 00478 #endif 00479 return (EXIT_SUCCESS); 00480 } 00481 00482 00487 void 00488 dxf_layer_index_free_chain 00489 ( 00490 DxfLayerIndex *layer_indices 00492 ) 00493 { 00494 #ifdef DEBUG 00495 DXF_DEBUG_BEGIN 00496 #endif 00497 if (layer_indices == NULL) 00498 { 00499 fprintf (stderr, 00500 (_("Warning in %s () a NULL pointer was passed.\n")), 00501 __FUNCTION__); 00502 } 00503 while (layer_indices != NULL) 00504 { 00505 struct DxfLayerIndex *iter = layer_indices->next; 00506 dxf_layer_index_free (layer_indices); 00507 layer_indices = (DxfLayerIndex *) iter; 00508 } 00509 #if DEBUG 00510 DXF_DEBUG_END 00511 #endif 00512 } 00513 00514 00520 int 00521 dxf_layer_index_get_id_code 00522 ( 00523 DxfLayerIndex *layer_index 00525 ) 00526 { 00527 #if DEBUG 00528 DXF_DEBUG_BEGIN 00529 #endif 00530 /* Do some basic checks. */ 00531 if (layer_index == NULL) 00532 { 00533 fprintf (stderr, 00534 (_("Error in %s () a NULL pointer was passed.\n")), 00535 __FUNCTION__); 00536 return (EXIT_FAILURE); 00537 } 00538 if (layer_index->id_code < 0) 00539 { 00540 fprintf (stderr, 00541 (_("Error in %s () a negative value was found.\n")), 00542 __FUNCTION__); 00543 return (EXIT_FAILURE); 00544 } 00545 #if DEBUG 00546 DXF_DEBUG_END 00547 #endif 00548 return (layer_index->id_code); 00549 } 00550 00551 00555 DxfLayerIndex * 00556 dxf_layer_index_set_id_code 00557 ( 00558 DxfLayerIndex *layer_index, 00560 int id_code 00564 ) 00565 { 00566 #if DEBUG 00567 DXF_DEBUG_BEGIN 00568 #endif 00569 /* Do some basic checks. */ 00570 if (layer_index == NULL) 00571 { 00572 fprintf (stderr, 00573 (_("Error in %s () a NULL pointer was passed.\n")), 00574 __FUNCTION__); 00575 return (NULL); 00576 } 00577 if (id_code < 0) 00578 { 00579 fprintf (stderr, 00580 (_("Error in %s () a negative value was passed.\n")), 00581 __FUNCTION__); 00582 return (NULL); 00583 } 00584 layer_index->id_code = id_code; 00585 #if DEBUG 00586 DXF_DEBUG_END 00587 #endif 00588 return (layer_index); 00589 } 00590 00591 00600 char * 00601 dxf_layer_index_get_dictionary_owner_soft 00602 ( 00603 DxfLayerIndex *layer_index 00605 ) 00606 { 00607 #if DEBUG 00608 DXF_DEBUG_BEGIN 00609 #endif 00610 /* Do some basic checks. */ 00611 if (layer_index == NULL) 00612 { 00613 fprintf (stderr, 00614 (_("Error in %s () a NULL pointer was passed.\n")), 00615 __FUNCTION__); 00616 return (NULL); 00617 } 00618 if (layer_index->dictionary_owner_soft == NULL) 00619 { 00620 fprintf (stderr, 00621 (_("Error in %s () a NULL pointer was found.\n")), 00622 __FUNCTION__); 00623 return (NULL); 00624 } 00625 #if DEBUG 00626 DXF_DEBUG_END 00627 #endif 00628 return (strdup (layer_index->dictionary_owner_soft)); 00629 } 00630 00631 00636 DxfLayerIndex * 00637 dxf_layer_index_set_dictionary_owner_soft 00638 ( 00639 DxfLayerIndex *layer_index, 00641 char *dictionary_owner_soft 00644 ) 00645 { 00646 #if DEBUG 00647 DXF_DEBUG_BEGIN 00648 #endif 00649 /* Do some basic checks. */ 00650 if (layer_index == NULL) 00651 { 00652 fprintf (stderr, 00653 (_("Error in %s () a NULL pointer was passed.\n")), 00654 __FUNCTION__); 00655 return (NULL); 00656 } 00657 if (dictionary_owner_soft == NULL) 00658 { 00659 fprintf (stderr, 00660 (_("Error in %s () a NULL pointer was passed.\n")), 00661 __FUNCTION__); 00662 return (NULL); 00663 } 00664 layer_index->dictionary_owner_soft = strdup (dictionary_owner_soft); 00665 #if DEBUG 00666 DXF_DEBUG_END 00667 #endif 00668 return (layer_index); 00669 } 00670 00671 00680 char * 00681 dxf_layer_index_get_dictionary_owner_hard 00682 ( 00683 DxfLayerIndex *layer_index 00685 ) 00686 { 00687 #if DEBUG 00688 DXF_DEBUG_BEGIN 00689 #endif 00690 /* Do some basic checks. */ 00691 if (layer_index == NULL) 00692 { 00693 fprintf (stderr, 00694 (_("Error in %s () a NULL pointer was passed.\n")), 00695 __FUNCTION__); 00696 return (NULL); 00697 } 00698 if (layer_index->dictionary_owner_hard == NULL) 00699 { 00700 fprintf (stderr, 00701 (_("Error in %s () a NULL pointer was found.\n")), 00702 __FUNCTION__); 00703 return (NULL); 00704 } 00705 #if DEBUG 00706 DXF_DEBUG_END 00707 #endif 00708 return (strdup (layer_index->dictionary_owner_hard)); 00709 } 00710 00711 00716 DxfLayerIndex * 00717 dxf_layer_index_set_dictionary_owner_hard 00718 ( 00719 DxfLayerIndex *layer_index, 00721 char *dictionary_owner_hard 00724 ) 00725 { 00726 #if DEBUG 00727 DXF_DEBUG_BEGIN 00728 #endif 00729 /* Do some basic checks. */ 00730 if (layer_index == NULL) 00731 { 00732 fprintf (stderr, 00733 (_("Error in %s () a NULL pointer was passed.\n")), 00734 __FUNCTION__); 00735 return (NULL); 00736 } 00737 if (dictionary_owner_hard == NULL) 00738 { 00739 fprintf (stderr, 00740 (_("Error in %s () a NULL pointer was passed.\n")), 00741 __FUNCTION__); 00742 return (NULL); 00743 } 00744 layer_index->dictionary_owner_hard = strdup (dictionary_owner_hard); 00745 #if DEBUG 00746 DXF_DEBUG_END 00747 #endif 00748 return (layer_index); 00749 } 00750 00751 00757 double 00758 dxf_layer_index_get_time_stamp 00759 ( 00760 DxfLayerIndex *layer_index 00762 ) 00763 { 00764 #if DEBUG 00765 DXF_DEBUG_BEGIN 00766 #endif 00767 /* Do some basic checks. */ 00768 if (layer_index == NULL) 00769 { 00770 fprintf (stderr, 00771 (_("Error in %s () a NULL pointer was passed.\n")), 00772 __FUNCTION__); 00773 return (EXIT_FAILURE); 00774 } 00775 if (layer_index->time_stamp < 0.0) 00776 { 00777 fprintf (stderr, 00778 (_("Error in %s () a negative value was found.\n")), 00779 __FUNCTION__); 00780 return (EXIT_FAILURE); 00781 } 00782 #if DEBUG 00783 DXF_DEBUG_END 00784 #endif 00785 return (layer_index->time_stamp); 00786 } 00787 00788 00792 DxfLayerIndex * 00793 dxf_layer_index_set_time_stamp 00794 ( 00795 DxfLayerIndex *layer_index, 00797 double time_stamp 00799 ) 00800 { 00801 #if DEBUG 00802 DXF_DEBUG_BEGIN 00803 #endif 00804 /* Do some basic checks. */ 00805 if (layer_index == NULL) 00806 { 00807 fprintf (stderr, 00808 (_("Error in %s () a NULL pointer was passed.\n")), 00809 __FUNCTION__); 00810 return (NULL); 00811 } 00812 if (time_stamp < 0.0) 00813 { 00814 fprintf (stderr, 00815 (_("Error in %s () a negative value was passed.\n")), 00816 __FUNCTION__); 00817 return (NULL); 00818 } 00819 layer_index->time_stamp = time_stamp; 00820 #if DEBUG 00821 DXF_DEBUG_END 00822 #endif 00823 return (layer_index); 00824 } 00825 00826 00835 DxfLayerIndex * 00836 dxf_layer_index_get_next 00837 ( 00838 DxfLayerIndex *layer_index 00840 ) 00841 { 00842 #if DEBUG 00843 DXF_DEBUG_BEGIN 00844 #endif 00845 /* Do some basic checks. */ 00846 if (layer_index == NULL) 00847 { 00848 fprintf (stderr, 00849 (_("Error in %s () a NULL pointer was passed.\n")), 00850 __FUNCTION__); 00851 return (NULL); 00852 } 00853 if (layer_index->next == NULL) 00854 { 00855 fprintf (stderr, 00856 (_("Error in %s () a NULL pointer was found.\n")), 00857 __FUNCTION__); 00858 return (NULL); 00859 } 00860 #if DEBUG 00861 DXF_DEBUG_END 00862 #endif 00863 return ((DxfLayerIndex *) layer_index->next); 00864 } 00865 00866 00871 DxfLayerIndex * 00872 dxf_layer_index_set_next 00873 ( 00874 DxfLayerIndex *layer_index, 00876 DxfLayerIndex *next 00879 ) 00880 { 00881 #if DEBUG 00882 DXF_DEBUG_BEGIN 00883 #endif 00884 /* Do some basic checks. */ 00885 if (layer_index == NULL) 00886 { 00887 fprintf (stderr, 00888 (_("Error in %s () a NULL pointer was passed.\n")), 00889 __FUNCTION__); 00890 return (NULL); 00891 } 00892 if (next == NULL) 00893 { 00894 fprintf (stderr, 00895 (_("Error in %s () a NULL pointer was passed.\n")), 00896 __FUNCTION__); 00897 return (NULL); 00898 } 00899 layer_index->next = (struct DxfLayerIndex *) next; 00900 #if DEBUG 00901 DXF_DEBUG_END 00902 #endif 00903 return (layer_index); 00904 } 00905 00906 00915 DxfLayerIndex * 00916 dxf_layer_index_get_last 00917 ( 00918 DxfLayerIndex *layer_index 00920 ) 00921 { 00922 #if DEBUG 00923 DXF_DEBUG_BEGIN 00924 #endif 00925 /* Do some basic checks. */ 00926 if (layer_index == NULL) 00927 { 00928 fprintf (stderr, 00929 (_("Error in %s () a NULL pointer was passed.\n")), 00930 __FUNCTION__); 00931 return (NULL); 00932 } 00933 if (layer_index->next == NULL) 00934 { 00935 fprintf (stderr, 00936 (_("Warning in %s () a NULL pointer was found.\n")), 00937 __FUNCTION__); 00938 return ((DxfLayerIndex *) layer_index); 00939 } 00940 DxfLayerIndex *iter = (DxfLayerIndex *) layer_index->next; 00941 while (iter->next != NULL) 00942 { 00943 iter = (DxfLayerIndex *) iter->next; 00944 } 00945 #if DEBUG 00946 DXF_DEBUG_END 00947 #endif 00948 return ((DxfLayerIndex *) iter); 00949 } 00950 00951 00952 /* EOF*/