libDXF 0.0.1
A library with DXF related functions written in C.

spatial_index.c

Go to the documentation of this file.
00001 
00046 #include "spatial_index.h"
00047 
00048 
00054 DxfSpatialIndex *
00055 dxf_spatial_index_new ()
00056 {
00057 #if DEBUG
00058         DXF_DEBUG_BEGIN
00059 #endif
00060         DxfSpatialIndex *spatial_index = NULL;
00061         size_t size;
00062 
00063         size = sizeof (DxfSpatialIndex);
00064         /* avoid malloc of 0 bytes */
00065         if (size == 0) size = 1;
00066         if ((spatial_index = malloc (size)) == NULL)
00067         {
00068                 fprintf (stderr,
00069                   (_("Error in %s () could not allocate memory for a DxfSpatialIndex struct.\n")),
00070                   __FUNCTION__);
00071                 spatial_index = NULL;
00072         }
00073         else
00074         {
00075                 memset (spatial_index, 0, size);
00076         }
00077 #if DEBUG
00078         DXF_DEBUG_END
00079 #endif
00080         return (spatial_index);
00081 }
00082 
00083 
00091 DxfSpatialIndex *
00092 dxf_spatial_index_init
00093 (
00094         DxfSpatialIndex *spatial_index
00096 )
00097 {
00098 #if DEBUG
00099         DXF_DEBUG_BEGIN
00100 #endif
00101         int JD;
00102         float fraction_day;
00103         time_t now;
00104 
00105         /* Do some basic checks. */
00106         if (spatial_index == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 spatial_index = dxf_spatial_index_new ();
00112         }
00113         if (spatial_index == NULL)
00114         {
00115                 fprintf (stderr,
00116                   (_("Error in %s () could not allocate memory for a DxfSpatialIndex struct.\n")),
00117                   __FUNCTION__);
00118                 return (NULL);
00119         }
00120         spatial_index->id_code = 0;
00121         spatial_index->dictionary_owner_soft = strdup ("");
00122         spatial_index->dictionary_owner_hard = strdup ("");
00123         if (time (&now) != (time_t)(-1))
00124         {
00125                 struct tm *current_time = localtime (&now);
00126 
00127                 /* Transform the current local gregorian date in a julian date.*/
00128                 JD = current_time->tm_mday - 32075 + 1461 * (current_time->tm_year + 6700 + (current_time->tm_mon - 13) / 12) / 4 + 367 * (current_time->tm_mon - 1 - (current_time->tm_mon - 13) / 12 * 12) / 12 - 3 * ((current_time->tm_year + 6800 + (current_time->tm_mon - 13) / 12) / 100) / 4;
00129                 /* Transform the current local clock time in fraction of day.*/
00130                 fraction_day = (current_time->tm_hour + (current_time->tm_min / 60.0) + (current_time->tm_sec / 3600.0)) / 24.0;
00131                 spatial_index->time_stamp = (double) (JD + fraction_day);
00132         }
00133         else spatial_index->time_stamp = 0.0;
00134         spatial_index->next = NULL;
00135 #if DEBUG
00136         DXF_DEBUG_END
00137 #endif
00138         return (spatial_index);
00139 }
00140 
00141 
00153 DxfSpatialIndex *
00154 dxf_spatial_index_read
00155 (
00156         DxfFile *fp,
00158         DxfSpatialIndex *spatial_index
00160 )
00161 {
00162 #if DEBUG
00163         DXF_DEBUG_BEGIN
00164 #endif
00165         char *temp_string = NULL;
00166 
00167         /* Do some basic checks. */
00168         if (fp == NULL)
00169         {
00170                 fprintf (stderr,
00171                   (_("Error in %s () a NULL file pointer was passed.\n")),
00172                   __FUNCTION__);
00173                 /* Clean up. */
00174                 free (temp_string);
00175                 return (NULL);
00176         }
00177         if (fp->acad_version_number < AutoCAD_14)
00178         {
00179                 fprintf (stderr,
00180                   (_("Warning in %s () illegal DXF version for this entity.\n")),
00181                   __FUNCTION__);
00182         }
00183         if (spatial_index == NULL)
00184         {
00185                 fprintf (stderr,
00186                   (_("Warning in %s () a NULL pointer was passed.\n")),
00187                   __FUNCTION__);
00188                 spatial_index = dxf_spatial_index_new ();
00189                 spatial_index = dxf_spatial_index_init (spatial_index);
00190         }
00191         (fp->line_number)++;
00192         fscanf (fp->fp, "%[^\n]", temp_string);
00193         while (strcmp (temp_string, "0") != 0)
00194         {
00195                 if (ferror (fp->fp))
00196                 {
00197                         fprintf (stderr,
00198                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00199                           __FUNCTION__, fp->filename, fp->line_number);
00200                         /* Clean up. */
00201                         free (temp_string);
00202                         fclose (fp->fp);
00203                         return (NULL);
00204                 }
00205                 if (strcmp (temp_string, "5") == 0)
00206                 {
00207                         /* Now follows a string containing a sequential
00208                          * id number. */
00209                         (fp->line_number)++;
00210                         fscanf (fp->fp, "%x\n", &spatial_index->id_code);
00211                 }
00212                 if (strcmp (temp_string, "40") == 0)
00213                 {
00214                         /* Now follows a string containing a timestamp
00215                          * (Julian date). */
00216                         (fp->line_number)++;
00217                         fscanf (fp->fp, "%lf\n", &spatial_index->time_stamp);
00218                 }
00219                 else if ((fp->acad_version_number >= AutoCAD_13)
00220                         && (strcmp (temp_string, "100") == 0))
00221                 {
00222                         /* Now follows a string containing the
00223                          * subclass marker value. */
00224                         (fp->line_number)++;
00225                         fscanf (fp->fp, "%s\n", temp_string);
00226                         if ((strcmp (temp_string, "AcDbIndex") != 0)
00227                           && (strcmp (temp_string, "AcDbSpatialIndex") != 0))
00228                         {
00229                                 fprintf (stderr,
00230                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00231                                   __FUNCTION__, fp->filename, fp->line_number);
00232                         }
00233                 }
00234                 else if (strcmp (temp_string, "330") == 0)
00235                 {
00236                         /* Now follows a string containing Soft-pointer
00237                          * ID/handle to owner dictionary. */
00238                         (fp->line_number)++;
00239                         fscanf (fp->fp, "%s\n", spatial_index->dictionary_owner_soft);
00240                 }
00241                 else if (strcmp (temp_string, "360") == 0)
00242                 {
00243                         /* Now follows a string containing Hard owner
00244                          * ID/handle to owner dictionary. */
00245                         (fp->line_number)++;
00246                         fscanf (fp->fp, "%s\n", spatial_index->dictionary_owner_hard);
00247                 }
00248                 else if (strcmp (temp_string, "999") == 0)
00249                 {
00250                         /* Now follows a string containing a comment. */
00251                         (fp->line_number)++;
00252                         fscanf (fp->fp, "%s\n", temp_string);
00253                         fprintf (stdout, (_("DXF comment: %s\n")), temp_string);
00254                 }
00255                 else
00256                 {
00257                         fprintf (stderr,
00258                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00259                           __FUNCTION__, fp->filename, fp->line_number);
00260                 }
00261         }
00262         /* Clean up. */
00263         free (temp_string);
00264 #if DEBUG
00265         DXF_DEBUG_END
00266 #endif
00267         return (spatial_index);
00268 }
00269 
00270 
00280 int
00281 dxf_spatial_index_write
00282 (
00283         DxfFile *fp,
00285         DxfSpatialIndex *spatial_index
00287 )
00288 {
00289 #if DEBUG
00290         DXF_DEBUG_BEGIN
00291 #endif
00292         char *dxf_entity_name = strdup ("SPATIAL_INDEX");
00293 
00294         /* Do some basic checks. */
00295         if (fp == NULL)
00296         {
00297                 fprintf (stderr,
00298                   (_("Error in %s () a NULL file pointer was passed.\n")),
00299                   __FUNCTION__);
00300                 /* Clean up. */
00301                 free (dxf_entity_name);
00302                 return (EXIT_FAILURE);
00303         }
00304         if (spatial_index == NULL)
00305         {
00306                 fprintf (stderr,
00307                   (_("Error in %s () a NULL pointer was passed.\n")),
00308                   __FUNCTION__);
00309                 /* Clean up. */
00310                 free (dxf_entity_name);
00311                 return (EXIT_FAILURE);
00312         }
00313         if (fp->acad_version_number < AutoCAD_14)
00314         {
00315                 fprintf (stderr,
00316                   (_("Warning in %s () illegal DXF version for this %s entity with id-code: %x.\n")),
00317                   __FUNCTION__, dxf_entity_name, spatial_index->id_code);
00318         }
00319         /* Start writing output. */
00320         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00321         if (spatial_index->id_code != -1)
00322         {
00323                 fprintf (fp->fp, "  5\n%x\n", spatial_index->id_code);
00324         }
00335         if ((strcmp (spatial_index->dictionary_owner_soft, "") != 0)
00336           && (fp->acad_version_number >= AutoCAD_14))
00337         {
00338                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00339                 fprintf (fp->fp, "330\n%s\n", spatial_index->dictionary_owner_soft);
00340                 fprintf (fp->fp, "102\n}\n");
00341         }
00342         if ((strcmp (spatial_index->dictionary_owner_hard, "") != 0)
00343           && (fp->acad_version_number >= AutoCAD_14))
00344         {
00345                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00346                 fprintf (fp->fp, "360\n%s\n", spatial_index->dictionary_owner_hard);
00347                 fprintf (fp->fp, "102\n}\n");
00348         }
00349         if (fp->acad_version_number >= AutoCAD_13)
00350         {
00351                 fprintf (fp->fp, "100\nAcDbIndex\n");
00352         }
00353         fprintf (fp->fp, " 40\n%f\n", spatial_index->time_stamp);
00354         if (fp->acad_version_number >= AutoCAD_13)
00355         {
00356                 fprintf (fp->fp, "100\nAcDbSpatialIndex\n");
00357         }
00358         /* Clean up. */
00359         free (dxf_entity_name);
00360 #if DEBUG
00361         DXF_DEBUG_END
00362 #endif
00363         return (EXIT_SUCCESS);
00364 }
00365 
00366 
00374 int
00375 dxf_spatial_index_free
00376 (
00377         DxfSpatialIndex *spatial_index
00380 )
00381 {
00382 #if DEBUG
00383         DXF_DEBUG_BEGIN
00384 #endif
00385         /* Do some basic checks. */
00386         if (spatial_index == NULL)
00387         {
00388                 fprintf (stderr,
00389                   (_("Error in %s () a NULL pointer was passed.\n")),
00390                   __FUNCTION__);
00391                 return (EXIT_FAILURE);
00392         }
00393         if (spatial_index->next != NULL)
00394         {
00395                 fprintf (stderr,
00396                   (_("Error in %s () pointer to next was not NULL.\n")),
00397                   __FUNCTION__);
00398                 return (EXIT_FAILURE);
00399         }
00400         free (spatial_index->dictionary_owner_soft);
00401         free (spatial_index->dictionary_owner_hard);
00402         free (spatial_index);
00403         spatial_index = NULL;
00404 #if DEBUG
00405         DXF_DEBUG_END
00406 #endif
00407         return (EXIT_SUCCESS);
00408 }
00409 
00410 
00415 void
00416 dxf_spatial_index_free_chain
00417 (
00418         DxfSpatialIndex *spatial_indices
00421 )
00422 {
00423 #ifdef DEBUG
00424         DXF_DEBUG_BEGIN
00425 #endif
00426         if (spatial_indices == NULL)
00427         {
00428                 fprintf (stderr,
00429                   (_("Warning in %s () a NULL pointer was passed.\n")),
00430                   __FUNCTION__);
00431         }
00432         while (spatial_indices != NULL)
00433         {
00434                 struct DxfSpatialIndex *iter = spatial_indices->next;
00435                 dxf_spatial_index_free (spatial_indices);
00436                 spatial_indices = (DxfSpatialIndex *) iter;
00437         }
00438 #if DEBUG
00439         DXF_DEBUG_END
00440 #endif
00441 }
00442 
00443 
00449 int
00450 dxf_spatial_index_get_id_code
00451 (
00452         DxfSpatialIndex *spatial_index
00454 )
00455 {
00456 #if DEBUG
00457         DXF_DEBUG_BEGIN
00458 #endif
00459         /* Do some basic checks. */
00460         if (spatial_index == NULL)
00461         {
00462                 fprintf (stderr,
00463                   (_("Error in %s () a NULL pointer was passed.\n")),
00464                   __FUNCTION__);
00465                 return (EXIT_FAILURE);
00466         }
00467         if (spatial_index->id_code < 0)
00468         {
00469                 fprintf (stderr,
00470                   (_("Warning in %s () a negative value was found.\n")),
00471                   __FUNCTION__);
00472         }
00473 #if DEBUG
00474         DXF_DEBUG_END
00475 #endif
00476         return (spatial_index->id_code);
00477 }
00478 
00479 
00483 DxfSpatialIndex *
00484 dxf_spatial_index_set_id_code
00485 (
00486         DxfSpatialIndex *spatial_index,
00488         int id_code
00492 )
00493 {
00494 #if DEBUG
00495         DXF_DEBUG_BEGIN
00496 #endif
00497         /* Do some basic checks. */
00498         if (spatial_index == NULL)
00499         {
00500                 fprintf (stderr,
00501                   (_("Error in %s () a NULL pointer was passed.\n")),
00502                   __FUNCTION__);
00503                 return (NULL);
00504         }
00505         if (id_code < 0)
00506         {
00507                 fprintf (stderr,
00508                   (_("Warning in %s () a negative value was passed.\n")),
00509                   __FUNCTION__);
00510         }
00511         spatial_index->id_code = id_code;
00512 #if DEBUG
00513         DXF_DEBUG_END
00514 #endif
00515         return (spatial_index);
00516 }
00517 
00518 
00527 char *
00528 dxf_spatial_index_get_dictionary_owner_soft
00529 (
00530         DxfSpatialIndex *spatial_index
00532 )
00533 {
00534 #if DEBUG
00535         DXF_DEBUG_BEGIN
00536 #endif
00537         /* Do some basic checks. */
00538         if (spatial_index == NULL)
00539         {
00540                 fprintf (stderr,
00541                   (_("Error in %s () a NULL pointer was passed.\n")),
00542                   __FUNCTION__);
00543                 return (NULL);
00544         }
00545         if (spatial_index->dictionary_owner_soft ==  NULL)
00546         {
00547                 fprintf (stderr,
00548                   (_("Error in %s () a NULL pointer was found.\n")),
00549                   __FUNCTION__);
00550                 return (NULL);
00551         }
00552 #if DEBUG
00553         DXF_DEBUG_END
00554 #endif
00555         return (strdup (spatial_index->dictionary_owner_soft));
00556 }
00557 
00558 
00563 DxfSpatialIndex *
00564 dxf_spatial_index_set_dictionary_owner_soft
00565 (
00566         DxfSpatialIndex *spatial_index,
00568         char *dictionary_owner_soft
00571 )
00572 {
00573 #if DEBUG
00574         DXF_DEBUG_BEGIN
00575 #endif
00576         /* Do some basic checks. */
00577         if (spatial_index == NULL)
00578         {
00579                 fprintf (stderr,
00580                   (_("Error in %s () a NULL pointer was passed.\n")),
00581                   __FUNCTION__);
00582                 return (NULL);
00583         }
00584         if (dictionary_owner_soft == NULL)
00585         {
00586                 fprintf (stderr,
00587                   (_("Error in %s () a NULL pointer was passed.\n")),
00588                   __FUNCTION__);
00589                 return (NULL);
00590         }
00591         spatial_index->dictionary_owner_soft = strdup (dictionary_owner_soft);
00592 #if DEBUG
00593         DXF_DEBUG_END
00594 #endif
00595         return (spatial_index);
00596 }
00597 
00598 
00607 char *
00608 dxf_spatial_index_get_dictionary_owner_hard
00609 (
00610         DxfSpatialIndex *spatial_index
00612 )
00613 {
00614 #if DEBUG
00615         DXF_DEBUG_BEGIN
00616 #endif
00617         /* Do some basic checks. */
00618         if (spatial_index == NULL)
00619         {
00620                 fprintf (stderr,
00621                   (_("Error in %s () a NULL pointer was passed.\n")),
00622                   __FUNCTION__);
00623                 return (NULL);
00624         }
00625         if (spatial_index->dictionary_owner_hard ==  NULL)
00626         {
00627                 fprintf (stderr,
00628                   (_("Error in %s () a NULL pointer was found.\n")),
00629                   __FUNCTION__);
00630                 return (NULL);
00631         }
00632 #if DEBUG
00633         DXF_DEBUG_END
00634 #endif
00635         return (strdup (spatial_index->dictionary_owner_hard));
00636 }
00637 
00638 
00643 DxfSpatialIndex *
00644 dxf_spatial_index_set_dictionary_owner_hard
00645 (
00646         DxfSpatialIndex *spatial_index,
00648         char *dictionary_owner_hard
00651 )
00652 {
00653 #if DEBUG
00654         DXF_DEBUG_BEGIN
00655 #endif
00656         /* Do some basic checks. */
00657         if (spatial_index == NULL)
00658         {
00659                 fprintf (stderr,
00660                   (_("Error in %s () a NULL pointer was passed.\n")),
00661                   __FUNCTION__);
00662                 return (NULL);
00663         }
00664         if (dictionary_owner_hard == NULL)
00665         {
00666                 fprintf (stderr,
00667                   (_("Error in %s () a NULL pointer was passed.\n")),
00668                   __FUNCTION__);
00669                 return (NULL);
00670         }
00671         spatial_index->dictionary_owner_hard = strdup (dictionary_owner_hard);
00672 #if DEBUG
00673         DXF_DEBUG_END
00674 #endif
00675         return (spatial_index);
00676 }
00677 
00678 
00685 double
00686 dxf_spatial_index_get_time_stamp
00687 (
00688         DxfSpatialIndex *spatial_index
00690 )
00691 {
00692 #ifdef DEBUG
00693         DXF_DEBUG_BEGIN
00694 #endif
00695 
00696         /* Do some basic checks. */
00697         if (spatial_index == NULL)
00698         {
00699                 fprintf (stderr,
00700                   (_("Error in %s () a NULL pointer was passed.\n")),
00701                   __FUNCTION__);
00702                 return (EXIT_FAILURE);
00703         }
00704 #if DEBUG
00705         DXF_DEBUG_END
00706 #endif
00707         return (spatial_index->time_stamp);
00708 }
00709 
00710 
00718 DxfSpatialIndex *
00719 dxf_spatial_index_set_time_stamp
00720 (
00721         DxfSpatialIndex *spatial_index,
00723         double time_stamp
00726 )
00727 {
00728 #ifdef DEBUG
00729         DXF_DEBUG_BEGIN
00730 #endif
00731         /* Do some basic checks. */
00732         if (spatial_index == NULL)
00733         {
00734                 fprintf (stderr,
00735                   (_("Error in %s () a NULL pointer was passed.\n")),
00736                   __FUNCTION__);
00737                 return (NULL);
00738         }
00739         spatial_index->time_stamp = time_stamp;
00740 #if DEBUG
00741         DXF_DEBUG_END
00742 #endif
00743         return (spatial_index);
00744 }
00745 
00746 
00755 DxfSpatialIndex *
00756 dxf_spatial_index_get_next
00757 (
00758         DxfSpatialIndex *spatial_index
00760 )
00761 {
00762 #if DEBUG
00763         DXF_DEBUG_BEGIN
00764 #endif
00765         /* Do some basic checks. */
00766         if (spatial_index == NULL)
00767         {
00768                 fprintf (stderr,
00769                   (_("Error in %s () a NULL pointer was passed.\n")),
00770                   __FUNCTION__);
00771                 return (NULL);
00772         }
00773         if (spatial_index->next == NULL)
00774         {
00775                 fprintf (stderr,
00776                   (_("Error in %s () a NULL pointer was found.\n")),
00777                   __FUNCTION__);
00778                 return (NULL);
00779         }
00780 #if DEBUG
00781         DXF_DEBUG_END
00782 #endif
00783         return ((DxfSpatialIndex *) spatial_index->next);
00784 }
00785 
00786 
00791 DxfSpatialIndex *
00792 dxf_spatial_index_set_next
00793 (
00794         DxfSpatialIndex *spatial_index,
00796         DxfSpatialIndex *next
00799 )
00800 {
00801 #if DEBUG
00802         DXF_DEBUG_BEGIN
00803 #endif
00804         /* Do some basic checks. */
00805         if (spatial_index == NULL)
00806         {
00807                 fprintf (stderr,
00808                   (_("Error in %s () a NULL pointer was passed.\n")),
00809                   __FUNCTION__);
00810                 return (NULL);
00811         }
00812         if (next == NULL)
00813         {
00814                 fprintf (stderr,
00815                   (_("Error in %s () a NULL pointer was passed.\n")),
00816                   __FUNCTION__);
00817                 return (NULL);
00818         }
00819         spatial_index->next = (struct DxfSpatialIndex *) next;
00820 #if DEBUG
00821         DXF_DEBUG_END
00822 #endif
00823         return (spatial_index);
00824 }
00825 
00826 
00835 DxfSpatialIndex *
00836 dxf_spatial_index_get_last
00837 (
00838         DxfSpatialIndex *spatial_index
00840 )
00841 {
00842 #if DEBUG
00843         DXF_DEBUG_BEGIN
00844 #endif
00845         /* Do some basic checks. */
00846         if (spatial_index == NULL)
00847         {
00848                 fprintf (stderr,
00849                   (_("Error in %s () a NULL pointer was passed.\n")),
00850                   __FUNCTION__);
00851                 return (NULL);
00852         }
00853         if (spatial_index->next == NULL)
00854         {
00855                 fprintf (stderr,
00856                   (_("Warning in %s () a NULL pointer was found.\n")),
00857                   __FUNCTION__);
00858                 return ((DxfSpatialIndex *) spatial_index);
00859         }
00860         DxfSpatialIndex *iter = (DxfSpatialIndex *) spatial_index->next;
00861         while (iter->next != NULL)
00862         {
00863                 iter = (DxfSpatialIndex *) iter->next;
00864         }
00865 #if DEBUG
00866         DXF_DEBUG_END
00867 #endif
00868         return ((DxfSpatialIndex *) iter);
00869 }
00870 
00871 
00872 /* EOF*/