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

style.c

Go to the documentation of this file.
00001 
00035 #include "style.h"
00036 
00037 
00052 DxfStyle *
00053 dxf_style_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfStyle *style = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfStyle);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((style = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfStyle struct.\n")),
00068                   __FUNCTION__);
00069                 style = NULL;
00070         }
00071         else
00072         {
00073                 memset (style, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (style);
00079 }
00080 
00081 
00095 DxfStyle *
00096 dxf_style_init
00097 (
00098         DxfStyle *style
00100 )
00101 {
00102 #if DEBUG
00103         DXF_DEBUG_BEGIN
00104 #endif
00105         /* Do some basic checks. */
00106         if (style == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 style = dxf_style_new ();
00112         }
00113         if (style == NULL)
00114         {
00115               fprintf (stderr,
00116                 (_("Error in %s () could not allocate memory for a DxfStyle struct.\n")),
00117                 __FUNCTION__);
00118               return (NULL);
00119         }
00120         style->id_code = 0;
00121         style->style_name = strdup ("");
00122         style->primary_font_filename = strdup ("");
00123         style->big_font_filename = strdup ("");
00124         style->height = 0.0;
00125         style->width = 0.0;
00126         style->last_height = 0.0;
00127         style->oblique_angle = 0.0;
00128         style->flag = 0;
00129         style->text_generation_flag = 0;
00130         style->next = NULL;
00131 #if DEBUG
00132         DXF_DEBUG_END
00133 #endif
00134         return (style);
00135 }
00136 
00137 
00155 DxfStyle *
00156 dxf_style_read
00157 (
00158         DxfFile *fp,
00160         DxfStyle *style
00162 )
00163 {
00164 #if DEBUG
00165         DXF_DEBUG_BEGIN
00166 #endif
00167         char *temp_string = NULL;
00168 
00169         /* Do some basic checks. */
00170         if (fp == NULL)
00171         {
00172                 fprintf (stderr,
00173                   (_("Error in %s () a NULL file pointer was passed.\n")),
00174                   __FUNCTION__);
00175                 /* Clean up. */
00176                 free (temp_string);
00177                 return (NULL);
00178         }
00179         if (style == NULL)
00180         {
00181                 fprintf (stderr,
00182                   (_("Warning in %s () a NULL pointer was passed.\n")),
00183                   __FUNCTION__);
00184                 style = dxf_style_new ();
00185                 style = dxf_style_init (style);
00186         }
00187         (fp->line_number)++;
00188         fscanf (fp->fp, "%[^\n]", temp_string);
00189         while (strcmp (temp_string, "0") != 0)
00190         {
00191                 if (ferror (fp->fp))
00192                 {
00193                         fprintf (stderr,
00194                           (_("Error in %s () while reading from: %s in line: %d.\n")),
00195                           __FUNCTION__, fp->filename, fp->line_number);
00196                         fclose (fp->fp);
00197                         /* Clean up. */
00198                         free (temp_string);
00199                         return (NULL);
00200                 }
00201                 if (strcmp (temp_string, "5") == 0)
00202                 {
00203                         /* Now follows a string containing a sequential
00204                          * id number. */
00205                         (fp->line_number)++;
00206                         fscanf (fp->fp, "%x\n", &style->id_code);
00207                 }
00208                 else if (strcmp (temp_string, "2") == 0)
00209                 {
00210                         /* Now follows a string containing a style name. */
00211                         (fp->line_number)++;
00212                         fscanf (fp->fp, "%s\n", style->style_name);
00213                 }
00214                 else if (strcmp (temp_string, "3") == 0)
00215                 {
00216                         /* Now follows a string containing a primary
00217                          * font filename. */
00218                         (fp->line_number)++;
00219                         fscanf (fp->fp, "%s\n", style->primary_font_filename);
00220                 }
00221                 else if (strcmp (temp_string, "4") == 0)
00222                 {
00223                         /* Now follows a string containing a big font
00224                          * filename. */
00225                         (fp->line_number)++;
00226                         fscanf (fp->fp, "%s\n", style->big_font_filename);
00227                 }
00228                 else if (strcmp (temp_string, "40") == 0)
00229                 {
00230                         /* Now follows a string containing the
00231                          * height. */
00232                         (fp->line_number)++;
00233                         fscanf (fp->fp, "%lf\n", &style->height);
00234                 }
00235                 else if (strcmp (temp_string, "41") == 0)
00236                 {
00237                         /* Now follows a string containing the
00238                          * width. */
00239                         (fp->line_number)++;
00240                         fscanf (fp->fp, "%lf\n", &style->width);
00241                 }
00242                 else if (strcmp (temp_string, "42") == 0)
00243                 {
00244                         /* Now follows a string containing the
00245                          * last used height. */
00246                         (fp->line_number)++;
00247                         fscanf (fp->fp, "%lf\n", &style->last_height);
00248                 }
00249                 else if (strcmp (temp_string, "50") == 0)
00250                 {
00251                         /* Now follows a string containing the
00252                          * oblique angle. */
00253                         (fp->line_number)++;
00254                         fscanf (fp->fp, "%lf\n", &style->oblique_angle);
00255                 }
00256                 else if (strcmp (temp_string, "70") == 0)
00257                 {
00258                         /* Now follows a string containing the
00259                          * standard flag value. */
00260                         (fp->line_number)++;
00261                         fscanf (fp->fp, "%d\n", &style->flag);
00262                 }
00263                 else if (strcmp (temp_string, "71") == 0)
00264                 {
00265                         /* Now follows a string containing the
00266                          * text generation flag value. */
00267                         (fp->line_number)++;
00268                         fscanf (fp->fp, "%d\n", &style->text_generation_flag);
00269                 }
00270                 else if ((fp->acad_version_number >= AutoCAD_13)
00271                         && (strcmp (temp_string, "100") == 0))
00272                 {
00273                         /* Now follows a string containing the
00274                          * subclass marker value. */
00275                         (fp->line_number)++;
00276                         fscanf (fp->fp, "%s\n", temp_string);
00277                         if ((strcmp (temp_string, "AcDbSymbolTableRecord") != 0)
00278                         && ((strcmp (temp_string, "AcDbTextStyleTableRecord") != 0)))
00279                         {
00280                                 fprintf (stderr,
00281                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00282                                   __FUNCTION__, fp->filename, fp->line_number);
00283                         }
00284                 }
00285                 else if (strcmp (temp_string, "330") == 0)
00286                 {
00287                         /* Now follows a string containing Soft-pointer
00288                          * ID/handle to owner dictionary. */
00289                         (fp->line_number)++;
00290                         fscanf (fp->fp, "%s\n", style->dictionary_owner_soft);
00291                 }
00292                 else if (strcmp (temp_string, "360") == 0)
00293                 {
00294                         /* Now follows a string containing Hard owner
00295                          * ID/handle to owner dictionary. */
00296                         (fp->line_number)++;
00297                         fscanf (fp->fp, "%s\n", style->dictionary_owner_hard);
00298                 }
00299                 else if (strcmp (temp_string, "999") == 0)
00300                 {
00301                         /* Now follows a string containing a comment. */
00302                         (fp->line_number)++;
00303                         fscanf (fp->fp, "%s\n", temp_string);
00304                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00305                 }
00306                 else
00307                 {
00308                         fprintf (stderr,
00309                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00310                           __FUNCTION__, fp->filename, fp->line_number);
00311                 }
00312         }
00313         /* Handle omitted members and/or illegal values. */
00314         if (strcmp (style->style_name, "") == 0)
00315         {
00316                 sprintf (style->style_name, "%i", style->id_code);
00317                 fprintf (stderr,
00318                   (_("Warning in %s () illegal style name value found while reading from: %s in line: %d.\n")),
00319                   __FUNCTION__, fp->filename, fp->line_number);
00320         }
00321         if ((strcmp (style->primary_font_filename, "") == 0)
00322           && (style->flag == 1))
00323         {
00324                 sprintf (style->primary_font_filename, "%i", style->id_code);
00325                 fprintf (stderr,
00326                   (_("Warning in %s () illegal primary font filename value found while reading from: %s in line: %d.\n")),
00327                   __FUNCTION__, fp->filename, fp->line_number);
00328         }
00329         /* Clean up. */
00330         free (temp_string);
00331 #if DEBUG
00332         DXF_DEBUG_END
00333 #endif
00334         return (style);
00335 }
00336 
00337 
00350 int
00351 dxf_style_write
00352 (
00353         DxfFile *fp,
00355         DxfStyle *style
00357 )
00358 {
00359 #if DEBUG
00360         DXF_DEBUG_BEGIN
00361 #endif
00362         char *dxf_entity_name = strdup ("STYLE");
00363 
00364         /* Do some basic checks. */
00365         if (fp == NULL)
00366         {
00367                 fprintf (stderr,
00368                   (_("Error in %s () a NULL file pointer was passed.\n")),
00369                   __FUNCTION__);
00370                 /* Clean up. */
00371                 free (dxf_entity_name);
00372                 return (EXIT_FAILURE);
00373         }
00374         if (style == NULL)
00375         {
00376                 fprintf (stderr,
00377                   (_("Error in %s () a NULL pointer was passed.\n")),
00378                   __FUNCTION__);
00379                 /* Clean up. */
00380                 free (dxf_entity_name);
00381                 return (EXIT_FAILURE);
00382         }
00383         /* Start writing output. */
00384         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00385         if (style->id_code != -1)
00386         {
00387                 fprintf (fp->fp, "  5\n%x\n", style->id_code);
00388         }
00399         if ((strcmp (style->dictionary_owner_soft, "") != 0)
00400           && (fp->acad_version_number >= AutoCAD_14))
00401         {
00402                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00403                 fprintf (fp->fp, "330\n%s\n", style->dictionary_owner_soft);
00404                 fprintf (fp->fp, "102\n}\n");
00405         }
00406         if ((strcmp (style->dictionary_owner_hard, "") != 0)
00407           && (fp->acad_version_number >= AutoCAD_14))
00408         {
00409                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00410                 fprintf (fp->fp, "360\n%s\n", style->dictionary_owner_hard);
00411                 fprintf (fp->fp, "102\n}\n");
00412         }
00413         if (fp->acad_version_number >= AutoCAD_13)
00414         {
00415                 fprintf (fp->fp, "100\nAcDbSymbolTableRecord\n");
00416                 fprintf (fp->fp, "100\nAcDbTextStyleTableRecord\n");
00417         }
00418         fprintf (fp->fp, "  2\n%s\n", style->style_name);
00419         fprintf (fp->fp, " 70\n%d\n", style->flag);
00420         fprintf (fp->fp, " 40\n%f\n", style->height);
00421         fprintf (fp->fp, " 41\n%f\n", style->width);
00422         fprintf (fp->fp, " 50\n%f\n", style->oblique_angle);
00423         fprintf (fp->fp, " 71\n%d\n", style->text_generation_flag);
00424         fprintf (fp->fp, " 42\n%f\n", style->last_height);
00425         fprintf (fp->fp, "  3\n%s\n", style->primary_font_filename);
00426         fprintf (fp->fp, "  4\n%s\n", style->big_font_filename);
00427         /* Clean up. */
00428         free (dxf_entity_name);
00429 #if DEBUG
00430         DXF_DEBUG_END
00431 #endif
00432         return (EXIT_SUCCESS);
00433 }
00434 
00435 
00449 int
00450 dxf_style_free
00451 (
00452         DxfStyle *style
00455 )
00456 {
00457 #if DEBUG
00458         DXF_DEBUG_BEGIN
00459 #endif
00460         /* Do some basic checks. */
00461         if (style == NULL)
00462         {
00463                 fprintf (stderr,
00464                   (_("Error in %s () a NULL pointer was passed.\n")),
00465                   __FUNCTION__);
00466                 return (EXIT_FAILURE);
00467         }
00468         if (style->next != NULL)
00469         {
00470                 fprintf (stderr,
00471                   (_("Error in %s () pointer to next was not NULL.\n")),
00472                   __FUNCTION__);
00473                 return (EXIT_FAILURE);
00474         }
00475         free (style->style_name);
00476         free (style->primary_font_filename);
00477         free (style->big_font_filename);
00478         free (style);
00479         style = NULL;
00480 #if DEBUG
00481         DXF_DEBUG_END
00482 #endif
00483         return (EXIT_SUCCESS);
00484 }
00485 
00486 
00497 void
00498 dxf_style_free_chain
00499 (
00500         DxfStyle *styles
00503 )
00504 {
00505 #ifdef DEBUG
00506         DXF_DEBUG_BEGIN
00507 #endif
00508         if (styles == NULL)
00509         {
00510                 fprintf (stderr,
00511                   (_("Warning in %s () a NULL pointer was passed.\n")),
00512                   __FUNCTION__);
00513         }
00514         while (styles != NULL)
00515         {
00516                 struct DxfStyle *iter = styles->next;
00517                 dxf_style_free (styles);
00518                 styles = (DxfStyle *) iter;
00519         }
00520 #if DEBUG
00521         DXF_DEBUG_END
00522 #endif
00523 }
00524 
00525 
00538 int
00539 dxf_style_is_shape_file
00540 (
00541         DxfStyle *style
00543 )
00544 {
00545 #if DEBUG
00546         DXF_DEBUG_BEGIN
00547 #endif
00548         int result = FALSE;
00549 
00550         /* Do some basic checks. */
00551         if (style == NULL)
00552         {
00553                 fprintf (stderr,
00554                   (_("Error in %s () a NULL pointer was passed.\n")),
00555                   __FUNCTION__);
00556                 return (-1);
00557         }
00558         result = DXF_CHECK_BIT (style->flag, 0);
00559 #if DEBUG
00560         DXF_DEBUG_END
00561 #endif
00562         return (result);
00563 }
00564 
00565 
00579 int
00580 dxf_style_is_text_vertical
00581 (
00582         DxfStyle *style
00584 )
00585 {
00586 #if DEBUG
00587         DXF_DEBUG_BEGIN
00588 #endif
00589         int result = FALSE;
00590 
00591         /* Do some basic checks. */
00592         if (style == NULL)
00593         {
00594                 fprintf (stderr,
00595                   (_("Error in %s () a NULL pointer was passed.\n")),
00596                   __FUNCTION__);
00597                 return (-1);
00598         }
00599         result = DXF_CHECK_BIT (style->flag, 2);
00600 #if DEBUG
00601         DXF_DEBUG_END
00602 #endif
00603         return (result);
00604 }
00605 
00606 
00620 int
00621 dxf_style_is_xreferenced
00622 (
00623         DxfStyle *style
00625 )
00626 {
00627 #if DEBUG
00628         DXF_DEBUG_BEGIN
00629 #endif
00630         int result = FALSE;
00631 
00632         /* Do some basic checks. */
00633         if (style == NULL)
00634         {
00635                 fprintf (stderr,
00636                   (_("Error in %s () a NULL pointer was passed.\n")),
00637                   __FUNCTION__);
00638                 return (-1);
00639         }
00640         result = DXF_CHECK_BIT (style->flag, 4);
00641 #if DEBUG
00642         DXF_DEBUG_END
00643 #endif
00644         return (result);
00645 }
00646 
00647 
00663 int
00664 dxf_style_is_xresolved
00665 (
00666         DxfStyle *style
00668 )
00669 {
00670 #if DEBUG
00671         DXF_DEBUG_BEGIN
00672 #endif
00673         int result = FALSE;
00674 
00675         /* Do some basic checks. */
00676         if (style == NULL)
00677         {
00678                 fprintf (stderr,
00679                   (_("Error in %s () a NULL pointer was passed.\n")),
00680                   __FUNCTION__);
00681                 return (-1);
00682         }
00683         result = ((DXF_CHECK_BIT (style->flag, 4))
00684           && (DXF_CHECK_BIT (style->flag, 5)));
00685 #if DEBUG
00686         DXF_DEBUG_END
00687 #endif
00688         return (result);
00689 }
00690 
00691 
00705 int
00706 dxf_style_is_referenced
00707 (
00708         DxfStyle *style
00710 )
00711 {
00712 #if DEBUG
00713         DXF_DEBUG_BEGIN
00714 #endif
00715         int result = FALSE;
00716 
00717         /* Do some basic checks. */
00718         if (style == NULL)
00719         {
00720                 fprintf (stderr,
00721                   (_("Error in %s () a NULL pointer was passed.\n")),
00722                   __FUNCTION__);
00723                 return (-1);
00724         }
00725         result = DXF_CHECK_BIT (style->flag, 6);
00726 #if DEBUG
00727         DXF_DEBUG_END
00728 #endif
00729         return (result);
00730 }
00731 
00732 
00733 /* EOF*/