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

ucs.c

Go to the documentation of this file.
00001 
00035 #include "ucs.h"
00036 
00037 
00052 DxfUcs *
00053 dxf_ucs_new ()
00054 {
00055 #if DEBUG
00056         DXF_DEBUG_BEGIN
00057 #endif
00058         DxfUcs *ucs = NULL;
00059         size_t size;
00060 
00061         size = sizeof (DxfUcs);
00062         /* avoid malloc of 0 bytes */
00063         if (size == 0) size = 1;
00064         if ((ucs = malloc (size)) == NULL)
00065         {
00066                 fprintf (stderr,
00067                   (_("Error in %s () could not allocate memory for a DxfUcs struct.\n")),
00068                   __FUNCTION__);
00069                 ucs = NULL;
00070         }
00071         else
00072         {
00073                 memset (ucs, 0, size);
00074         }
00075 #if DEBUG
00076         DXF_DEBUG_END
00077 #endif
00078         return (ucs);
00079 }
00080 
00081 
00095 DxfUcs *
00096 dxf_ucs_init
00097 (
00098         DxfUcs *ucs
00100 )
00101 {
00102 #if DEBUG
00103         DXF_DEBUG_BEGIN
00104 #endif
00105         /* Do some basic checks. */
00106         if (ucs == NULL)
00107         {
00108                 fprintf (stderr,
00109                   (_("Warning in %s () a NULL pointer was passed.\n")),
00110                   __FUNCTION__);
00111                 ucs = dxf_ucs_new ();
00112         }
00113         if (ucs == NULL)
00114         {
00115                 fprintf (stderr,
00116                   (_("Error in %s () could not allocate memory for a DxfUcs struct.\n")),
00117                   __FUNCTION__);
00118                 return (NULL);
00119         }
00120         ucs->id_code = 0;
00121         ucs->UCS_name = strdup ("");
00122         ucs->x_origin = 0.0;
00123         ucs->y_origin = 0.0;
00124         ucs->z_origin = 0.0;
00125         ucs->x_X_dir = 0.0;
00126         ucs->y_X_dir = 0.0;
00127         ucs->z_X_dir = 0.0;
00128         ucs->x_Y_dir = 0.0;
00129         ucs->y_Y_dir = 0.0;
00130         ucs->z_Y_dir = 0.0;
00131         ucs->flag = 0;
00132         ucs->dictionary_owner_soft = strdup ("");
00133         ucs->dictionary_owner_hard = strdup ("");
00134         ucs->next = NULL;
00135 #if DEBUG
00136         DXF_DEBUG_END
00137 #endif
00138         return (ucs);
00139 }
00140 
00141 
00159 DxfUcs *
00160 dxf_ucs_read
00161 (
00162         DxfFile *fp,
00164         DxfUcs *ucs
00166 )
00167 {
00168 #if DEBUG
00169         DXF_DEBUG_BEGIN
00170 #endif
00171         char *temp_string = NULL;
00172 
00173         /* Do some basic checks. */
00174         if (fp == NULL)
00175         {
00176                 fprintf (stderr,
00177                   (_("Error in %s () a NULL file pointer was passed.\n")),
00178                   __FUNCTION__);
00179                 /* Clean up. */
00180                 free (temp_string);
00181                 return (NULL);
00182         }
00183         if (ucs == NULL)
00184         {
00185                 fprintf (stderr,
00186                   (_("Warning in %s () a NULL pointer was passed.\n")),
00187                   __FUNCTION__);
00188                 ucs = dxf_ucs_new ();
00189                 ucs = dxf_ucs_init (ucs);
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                         fclose (fp->fp);
00201                         /* Clean up. */
00202                         free (temp_string);
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", &ucs->id_code);
00211                 }
00212                 else if (strcmp (temp_string, "2") == 0)
00213                 {
00214                         /* Now follows a string containing an UCS
00215                          * name. */
00216                         (fp->line_number)++;
00217                         fscanf (fp->fp, "%s\n", ucs->UCS_name);
00218                 }
00219                 else if (strcmp (temp_string, "10") == 0)
00220                 {
00221                         /* Now follows a string containing the
00222                          * X-coordinate of the base point. */
00223                         (fp->line_number)++;
00224                         fscanf (fp->fp, "%lf\n", &ucs->x_origin);
00225                 }
00226                 else if (strcmp (temp_string, "20") == 0)
00227                 {
00228                         /* Now follows a string containing the
00229                          * Y-coordinate of the base point. */
00230                         (fp->line_number)++;
00231                         fscanf (fp->fp, "%lf\n", &ucs->y_origin);
00232                 }
00233                 else if (strcmp (temp_string, "30") == 0)
00234                 {
00235                         /* Now follows a string containing the
00236                          * Z-coordinate of the base point. */
00237                         (fp->line_number)++;
00238                         fscanf (fp->fp, "%lf\n", &ucs->z_origin);
00239                 }
00240                 else if (strcmp (temp_string, "11") == 0)
00241                 {
00242                         /* Now follows a string containing the
00243                          * X-coordinate of the reference point for the
00244                          * X-axis direction. */
00245                         (fp->line_number)++;
00246                         fscanf (fp->fp, "%lf\n", &ucs->x_X_dir);
00247                 }
00248                 else if (strcmp (temp_string, "21") == 0)
00249                 {
00250                         /* Now follows a string containing the
00251                          * Y-coordinate of the reference point for the
00252                          * X-axis direction. */
00253                         (fp->line_number)++;
00254                         fscanf (fp->fp, "%lf\n", &ucs->y_X_dir);
00255                 }
00256                 else if (strcmp (temp_string, "31") == 0)
00257                 {
00258                         /* Now follows a string containing the
00259                          * Z-coordinate of the reference point for the
00260                          * X-axis direction. */
00261                         (fp->line_number)++;
00262                         fscanf (fp->fp, "%lf\n", &ucs->z_X_dir);
00263                 }
00264                 else if (strcmp (temp_string, "12") == 0)
00265                 {
00266                         /* Now follows a string containing the
00267                          * X-coordinate of the reference point for the
00268                          * Y-axis direction. */
00269                         (fp->line_number)++;
00270                         fscanf (fp->fp, "%lf\n", &ucs->x_Y_dir);
00271                 }
00272                 else if (strcmp (temp_string, "22") == 0)
00273                 {
00274                         /* Now follows a string containing the
00275                          * Y-coordinate of the reference point for the
00276                          * Y-axis direction. */
00277                         (fp->line_number)++;
00278                         fscanf (fp->fp, "%lf\n", &ucs->y_Y_dir);
00279                 }
00280                 else if (strcmp (temp_string, "32") == 0)
00281                 {
00282                         /* Now follows a string containing the
00283                          * Z-coordinate of the reference point for the
00284                          * Y-axis direction. */
00285                         (fp->line_number)++;
00286                         fscanf (fp->fp, "%lf\n", &ucs->z_Y_dir);
00287                 }
00288                 else if (strcmp (temp_string, "70") == 0)
00289                 {
00290                         /* Now follows a string containing the
00291                          * standard flag value. */
00292                         (fp->line_number)++;
00293                         fscanf (fp->fp, "%d\n", &ucs->flag);
00294                 }
00295                 else if ((fp->acad_version_number >= AutoCAD_13)
00296                         && (strcmp (temp_string, "100") == 0))
00297                 {
00298                         /* Now follows a string containing the
00299                          * subclass marker value. */
00300                         (fp->line_number)++;
00301                         fscanf (fp->fp, "%s\n", temp_string);
00302                         if ((strcmp (temp_string, "AcDbSymbolTableRecord") != 0)
00303                         && ((strcmp (temp_string, "AcDbUCSTableRecord") != 0)))
00304                         {
00305                                 fprintf (stderr,
00306                                   (_("Warning in %s () found a bad subclass marker in: %s in line: %d.\n")),
00307                                   __FUNCTION__, fp->filename, fp->line_number);
00308                         }
00309                 }
00310                 else if (strcmp (temp_string, "330") == 0)
00311                 {
00312                         /* Now follows a string containing Soft-pointer
00313                          * ID/handle to owner dictionary. */
00314                         (fp->line_number)++;
00315                         fscanf (fp->fp, "%s\n", ucs->dictionary_owner_soft);
00316                 }
00317                 else if (strcmp (temp_string, "360") == 0)
00318                 {
00319                         /* Now follows a string containing Hard owner
00320                          * ID/handle to owner dictionary. */
00321                         (fp->line_number)++;
00322                         fscanf (fp->fp, "%s\n", ucs->dictionary_owner_hard);
00323                 }
00324                 else if (strcmp (temp_string, "999") == 0)
00325                 {
00326                         /* Now follows a string containing a comment. */
00327                         (fp->line_number)++;
00328                         fscanf (fp->fp, "%s\n", temp_string);
00329                         fprintf (stdout, "DXF comment: %s\n", temp_string);
00330                 }
00331                 else
00332                 {
00333                         fprintf (stderr,
00334                           (_("Warning in %s () unknown string tag found while reading from: %s in line: %d.\n")),
00335                           __FUNCTION__, fp->filename, fp->line_number);
00336                 }
00337         }
00338         /* Clean up. */
00339         free (temp_string);
00340 #if DEBUG
00341         DXF_DEBUG_END
00342 #endif
00343         return (ucs);
00344 }
00345 
00346 
00359 int
00360 dxf_ucs_write
00361 (
00362         DxfFile *fp,
00364         DxfUcs *ucs
00366 )
00367 {
00368 #if DEBUG
00369         DXF_DEBUG_BEGIN
00370 #endif
00371         char *dxf_entity_name = strdup ("UCS");
00372 
00373         /* Do some basic checks. */
00374         if (fp == NULL)
00375         {
00376                 fprintf (stderr,
00377                   (_("Error in %s () a NULL file pointer was passed.\n")),
00378                   __FUNCTION__);
00379                 /* Clean up. */
00380                 free (dxf_entity_name);
00381                 return (EXIT_FAILURE);
00382         }
00383         if (ucs == NULL)
00384         {
00385                 fprintf (stderr,
00386                   (_("Error in %s () a NULL pointer was passed.\n")),
00387                   __FUNCTION__);
00388                 /* Clean up. */
00389                 free (dxf_entity_name);
00390                 return (EXIT_FAILURE);
00391         }
00392         if ((ucs->UCS_name == NULL)
00393           || (strcmp (ucs->UCS_name, "") == 0))
00394         {
00395                 fprintf (stderr,
00396                   (_("Error in %s () empty UCS name string for the %s entity with id-code: %x\n")),
00397                   __FUNCTION__, dxf_entity_name, ucs->id_code);
00398                 fprintf (stderr,
00399                   (_("\t%s entity is discarded from output.\n")),
00400                   dxf_entity_name);
00401                 /* Clean up. */
00402                 free (dxf_entity_name);
00403                 return (EXIT_FAILURE);
00404         }
00405         /* Start writing output. */
00406         fprintf (fp->fp, "  0\n%s\n", dxf_entity_name);
00407         if (ucs->id_code != -1)
00408         {
00409                 fprintf (fp->fp, "  5\n%x\n", ucs->id_code);
00410         }
00421         if ((strcmp (ucs->dictionary_owner_soft, "") != 0)
00422           && (fp->acad_version_number >= AutoCAD_14))
00423         {
00424                 fprintf (fp->fp, "102\n{ACAD_REACTORS\n");
00425                 fprintf (fp->fp, "330\n%s\n", ucs->dictionary_owner_soft);
00426                 fprintf (fp->fp, "102\n}\n");
00427         }
00428         if ((strcmp (ucs->dictionary_owner_hard, "") != 0)
00429           && (fp->acad_version_number >= AutoCAD_14))
00430         {
00431                 fprintf (fp->fp, "102\n{ACAD_XDICTIONARY\n");
00432                 fprintf (fp->fp, "360\n%s\n", ucs->dictionary_owner_hard);
00433                 fprintf (fp->fp, "102\n}\n");
00434         }
00435         if (fp->acad_version_number >= AutoCAD_13)
00436         {
00437                 fprintf (fp->fp, "100\nAcDbSymbolTableRecord\n");
00438                 fprintf (fp->fp, "100\nAcDbUCSTableRecord\n");
00439         }
00440         fprintf (fp->fp, "  2\n%s\n", ucs->UCS_name);
00441         fprintf (fp->fp, " 70\n%d\n", ucs->flag);
00442         fprintf (fp->fp, " 10\n%f\n", ucs->x_origin);
00443         fprintf (fp->fp, " 20\n%f\n", ucs->y_origin);
00444         fprintf (fp->fp, " 30\n%f\n", ucs->z_origin);
00445         fprintf (fp->fp, " 11\n%f\n", ucs->x_X_dir);
00446         fprintf (fp->fp, " 21\n%f\n", ucs->y_X_dir);
00447         fprintf (fp->fp, " 31\n%f\n", ucs->z_X_dir);
00448         fprintf (fp->fp, " 12\n%f\n", ucs->x_Y_dir);
00449         fprintf (fp->fp, " 22\n%f\n", ucs->y_Y_dir);
00450         fprintf (fp->fp, " 32\n%f\n", ucs->z_Y_dir);
00451         /* Clean up. */
00452         free (dxf_entity_name);
00453 #if DEBUG
00454         DXF_DEBUG_END
00455 #endif
00456         return (EXIT_SUCCESS);
00457 }
00458 
00459 
00473 int
00474 dxf_ucs_free
00475 (
00476         DxfUcs *ucs
00479 )
00480 {
00481 #if DEBUG
00482         DXF_DEBUG_BEGIN
00483 #endif
00484         /* Do some basic checks. */
00485         if (ucs == NULL)
00486         {
00487                 fprintf (stderr,
00488                   (_("Error in %s () a NULL pointer was passed.\n")),
00489                   __FUNCTION__);
00490                 return (EXIT_FAILURE);
00491         }
00492         if (ucs->next != NULL)
00493         {
00494                 fprintf (stderr,
00495                   (_("Error in %s () pointer to next was not NULL.\n")),
00496                   __FUNCTION__);
00497                 return (EXIT_FAILURE);
00498         }
00499         free (ucs->UCS_name);
00500         free (ucs->dictionary_owner_soft);
00501         free (ucs->dictionary_owner_hard);
00502         free (ucs);
00503         ucs = NULL;
00504 #if DEBUG
00505         DXF_DEBUG_END
00506 #endif
00507         return (EXIT_SUCCESS);
00508 }
00509 
00510 
00521 void
00522 dxf_ucs_free_chain
00523 (
00524         DxfUcs *ucss
00527 )
00528 {
00529 #ifdef DEBUG
00530         DXF_DEBUG_BEGIN
00531 #endif
00532         if (ucss == NULL)
00533         {
00534                 fprintf (stderr,
00535                   (_("Warning in %s () a NULL pointer was passed.\n")),
00536                   __FUNCTION__);
00537         }
00538         while (ucss != NULL)
00539         {
00540                 struct DxfUcs *iter = ucss->next;
00541                 dxf_ucs_free (ucss);
00542                 ucss = (DxfUcs *) iter;
00543         }
00544 #if DEBUG
00545         DXF_DEBUG_END
00546 #endif
00547 }
00548 
00549 
00550 /* EOF */