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

endblk.c

Go to the documentation of this file.
00001 
00036 #include "endblk.h"
00037 
00038 
00052 DxfEndblk *
00053 dxf_endblk_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfEndblk *endblk = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfEndblk);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((endblk = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfEndblk struct.\n")),
00068                   __FUNCTION__);
00069                 endblk = NULL;
00070         }
00071         else
00072         {
00073                 memset (endblk, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (endblk);
00079 }
00080 
00081 
00095 DxfEndblk *
00096 dxf_endblk_init
00097 (
00098         DxfEndblk *endblk
00100 )
00101 {
00102 #if DEBUG
00103         DXF_DEBUG_BEGIN
00104 #endif
00105         /* Do some basic checks. */
00106         if (endblk == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 endblk = dxf_endblk_new ();
00112         }
00113         if (endblk == NULL)
00114         {
00115                 fprintf (stderr,
00116                   (_("Error in %s () could not allocate memory for a DxfEndblk struct.\n")),
00117                   __FUNCTION__);
00118                 return (NULL);
00119         }
00120         dxf_endblk_set_id_code (endblk, 0);
00121         dxf_endblk_set_layer (endblk, strdup (DXF_DEFAULT_LAYER));
00122         dxf_endblk_set_dictionary_owner_soft (endblk, strdup (""));
00123 #if DEBUG
00124         DXF_DEBUG_END
00125 #endif
00126         return (endblk);
00127 }
00128 
00152 DxfEndblk *
00153 dxf_endblk_read
00154 (
00155         DxfFile *fp,
00157         DxfEndblk *endblk
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 (endblk == NULL)
00177         {
00178                 fprintf (stderr,
00179                   (_("Warning in %s () a NULL pointer was passed.\n")),
00180                   __FUNCTION__);
00181                 endblk = dxf_endblk_new ();
00182                 endblk = dxf_endblk_init (endblk);
00183         }
00184         fscanf (fp->fp, "%[^\n]", temp_string);
00185         while (strcmp (temp_string, "0") != 0)
00186         {
00187                 if (ferror (fp->fp))
00188                 {
00189                         fprintf (stderr,
00190                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00191                           __FUNCTION__, fp->filename, fp->line_number);
00192                         fclose (fp->fp);
00193                         /* Clean up. */
00194                         free (temp_string);
00195                         return (NULL);
00196                 }
00197                 else if (strcmp (temp_string, "5") == 0)
00198                 {
00199                         /* Now follows a string containing a sequential
00200                          * id number. */
00201                         fscanf (fp->fp, "%x\n", &endblk->id_code);
00202                 }
00203                 else if (strcmp (temp_string, "8") == 0)
00204                 {
00205                         /* Now follows a string containing a layer name. */
00206                         fscanf (fp->fp, "%s\n", endblk->layer);
00207                 }
00208                 else if (strcmp (temp_string, "330") == 0)
00209                 {
00210                         /* Now follows a string containing Soft-pointer
00211                          * ID/handle to owner object. */
00212                         fscanf (fp->fp, "%s\n", endblk->dictionary_owner_soft);
00213                 }
00214                 else if (strcmp (temp_string, "999") == 0)
00215                 {
00216                         /* Now follows a string containing a comment. */
00217                         fscanf (fp->fp, "%s\n", temp_string);
00218                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00219                 }
00220                 else
00221                 {
00222                         fprintf (stderr,
00223                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00224                           __FUNCTION__, fp->filename, fp->line_number);
00225                 }
00226         }
00227         /* Handle ommitted members and/or illegal values. */
00228         if (strcmp (endblk->layer, "") == 0)
00229         {
00230                 endblk->layer = strdup (DXF_DEFAULT_LAYER);
00231         }
00232         /* Clean up. */
00233         free (temp_string);
00234 #if DEBUG
00235         DXF_DEBUG_END
00236 #endif
00237         return (endblk);
00238 }
00239 
00240 
00253 int
00254 dxf_endblk_write
00255 (
00256         DxfFile *fp,
00258         DxfEndblk *endblk
00260 )
00261 {
00262 #if DEBUG
00263         DXF_DEBUG_BEGIN
00264 #endif
00265         /* Do some basic checks. */
00266         if (fp == NULL)
00267         {
00268                 fprintf (stderr,
00269                   (_("Error in %s () a NULL file pointer was passed.\n")),
00270                   __FUNCTION__);
00271                 return (EXIT_FAILURE);
00272         }
00273         if (endblk == NULL)
00274         {
00275                 fprintf (stderr,
00276                   (_("Error in %s () a NULL pointer was passed.\n")),
00277                   __FUNCTION__);
00278                 return (EXIT_FAILURE);
00279         }
00280         /* Start writing output. */
00281         fprintf (fp->fp, "  0\nENDBLK\n");
00282         if (fp->acad_version_number >= AutoCAD_13)
00283         {
00284                 fprintf (fp->fp, "  5\n%x\n", dxf_endblk_get_id_code (endblk));
00285         }
00296         if (fp->acad_version_number >= AutoCAD_13)
00297         {
00298                 fprintf (fp->fp, "100\nAcDbEntity\n");
00299                 fprintf (fp->fp, "  8\n%s\n", dxf_endblk_get_layer (endblk));
00300                 fprintf (fp->fp, "100\nAcDbBlockEnd\n");
00301         }
00302 #if DEBUG
00303         DXF_DEBUG_END
00304 #endif
00305         return (EXIT_SUCCESS);
00306 }
00307 
00308 
00322 int
00323 dxf_endblk_free
00324 (
00325         DxfEndblk *endblk
00327 )
00328 {
00329 #if DEBUG
00330         DXF_DEBUG_BEGIN
00331 #endif
00332         /* Do some basic checks. */
00333         if (endblk == NULL)
00334         {
00335                 fprintf (stderr,
00336                   (_("Error in %s () a NULL pointer was passed.\n")),
00337                   __FUNCTION__);
00338                 return (EXIT_FAILURE);
00339         }
00340         free (dxf_endblk_get_layer (endblk));
00341         free (dxf_endblk_get_dictionary_owner_soft (endblk));
00342         free (endblk);
00343         endblk = NULL;
00344 #if DEBUG
00345         DXF_DEBUG_END
00346 #endif
00347         return (EXIT_SUCCESS);
00348 }
00349 
00350 
00362 int
00363 dxf_endblk_get_id_code
00364 (
00365         DxfEndblk *endblk
00367 )
00368 {
00369 #if DEBUG
00370         DXF_DEBUG_BEGIN
00371 #endif
00372         int result;
00373 
00374         /* Do some basic checks. */
00375         if (endblk == NULL)
00376         {
00377                 fprintf (stderr,
00378                   (_("Error in %s () a NULL pointer was passed.\n")),
00379                   __FUNCTION__);
00380                 return (EXIT_FAILURE);
00381         }
00382         if (endblk->id_code < 0)
00383         {
00384                 fprintf (stderr,
00385                   (_("Error in %s () a negative value was found in the id-code member.\n")),
00386                   __FUNCTION__);
00387                 return (EXIT_FAILURE);
00388         }
00389         result = endblk->id_code;
00390 #if DEBUG
00391         DXF_DEBUG_END
00392 #endif
00393         return (result);
00394 }
00395 
00396 
00406 DxfEndblk *
00407 dxf_endblk_set_id_code
00408 (
00409         DxfEndblk *endblk,
00411         int id_code
00415 )
00416 {
00417 #if DEBUG
00418         DXF_DEBUG_BEGIN
00419 #endif
00420         /* Do some basic checks. */
00421         if (endblk == NULL)
00422         {
00423                 fprintf (stderr,
00424                   (_("Error in %s () a NULL pointer was passed.\n")),
00425                   __FUNCTION__);
00426                 return (NULL);
00427         }
00428         if (id_code < 0)
00429         {
00430                 fprintf (stderr,
00431                   (_("Error in %s () a negative id-code value was passed.\n")),
00432                   __FUNCTION__);
00433                 return (NULL);
00434         }
00435         endblk->id_code = id_code;
00436 #if DEBUG
00437         DXF_DEBUG_END
00438 #endif
00439         return (endblk);
00440 }
00441 
00442 
00454 char *
00455 dxf_endblk_get_layer
00456 (
00457         DxfEndblk *endblk
00459 )
00460 {
00461 #if DEBUG
00462         DXF_DEBUG_BEGIN
00463 #endif
00464         char *result = NULL;
00465 
00466         /* Do some basic checks. */
00467         if (endblk == NULL)
00468         {
00469                 fprintf (stderr,
00470                   (_("Error in %s () a NULL pointer was passed.\n")),
00471                   __FUNCTION__);
00472                 return (NULL);
00473         }
00474         if (endblk->layer ==  NULL)
00475         {
00476                 fprintf (stderr,
00477                   (_("Error in %s () a NULL pointer was found in the layer member.\n")),
00478                   __FUNCTION__);
00479                 return (NULL);
00480         }
00481         result = strdup (endblk->layer);
00482 #if DEBUG
00483         DXF_DEBUG_END
00484 #endif
00485         return (result);
00486 }
00487 
00488 
00498 DxfEndblk *
00499 dxf_endblk_set_layer
00500 (
00501         DxfEndblk *endblk,
00503         char *layer
00505 )
00506 {
00507 #if DEBUG
00508         DXF_DEBUG_BEGIN
00509 #endif
00510         /* Do some basic checks. */
00511         if (endblk == NULL)
00512         {
00513                 fprintf (stderr,
00514                   (_("Error in %s () a NULL pointer was passed.\n")),
00515                   __FUNCTION__);
00516                 return (NULL);
00517         }
00518         if (layer == NULL)
00519         {
00520                 fprintf (stderr,
00521                   (_("Error in %s () a NULL pointer was passed.\n")),
00522                   __FUNCTION__);
00523                 return (NULL);
00524         }
00525         endblk->layer = strdup (layer);
00526 #if DEBUG
00527         DXF_DEBUG_END
00528 #endif
00529         return (endblk);
00530 }
00531 
00532 
00547 char *
00548 dxf_endblk_get_dictionary_owner_soft
00549 (
00550         DxfEndblk *endblk
00552 )
00553 {
00554 #if DEBUG
00555         DXF_DEBUG_BEGIN
00556 #endif
00557         char *result;
00558 
00559         /* Do some basic checks. */
00560         if (endblk == NULL)
00561         {
00562                 fprintf (stderr,
00563                   (_("Error in %s () a NULL pointer was passed.\n")),
00564                   __FUNCTION__);
00565                 return (NULL);
00566         }
00567         if (endblk->dictionary_owner_soft ==  NULL)
00568         {
00569                 fprintf (stderr,
00570                   (_("Error in %s () a NULL pointer was found in the dictionary_owner_soft member.\n")),
00571                   __FUNCTION__);
00572                 return (NULL);
00573         }
00574         result = strdup (endblk->dictionary_owner_soft);
00575 #if DEBUG
00576         DXF_DEBUG_END
00577 #endif
00578         return (result);
00579 }
00580 
00581 
00592 DxfEndblk *
00593 dxf_endblk_set_dictionary_owner_soft
00594 (
00595         DxfEndblk *endblk,
00597         char *dictionary_owner_soft
00600 )
00601 {
00602 #if DEBUG
00603         DXF_DEBUG_BEGIN
00604 #endif
00605         /* Do some basic checks. */
00606         if (endblk == NULL)
00607         {
00608                 fprintf (stderr,
00609                   (_("Error in %s () a NULL pointer was passed.\n")),
00610                   __FUNCTION__);
00611                 return (NULL);
00612         }
00613         if (dictionary_owner_soft == NULL)
00614         {
00615                 fprintf (stderr,
00616                   (_("Error in %s () a NULL pointer was passed.\n")),
00617                   __FUNCTION__);
00618                 return (NULL);
00619         }
00620         endblk->dictionary_owner_soft = strdup (dictionary_owner_soft);
00621 #if DEBUG
00622         DXF_DEBUG_END
00623 #endif
00624         return (endblk);
00625 }
00626 
00627 
00628 /* EOF */