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

comment.c

Go to the documentation of this file.
00001 
00043 #include "comment.h"
00044 
00050 DxfComment *
00051 dxf_comment_new ()
00052 {
00053 #ifdef DEBUG
00054         DXF_DEBUG_BEGIN
00055 #endif
00056         DxfComment *comment = NULL;
00057         size_t size;
00058 
00059         size = sizeof (DxfComment);
00060         /* avoid malloc of 0 bytes */
00061         if (size == 0) size = 1;
00062         if ((comment = malloc (size)) == NULL)
00063         {
00064                 fprintf (stderr,
00065                   (_("Error in %s () could not allocate memory for a DxfComment struct.\n")),
00066                   __FUNCTION__);
00067                 comment = NULL;
00068         }
00069         else
00070         {
00071                 memset (comment, 0, size);
00072         }
00073 #ifdef DEBUG
00074         DXF_DEBUG_END
00075 #endif
00076         return (comment);
00077 }
00078 
00079 
00087 DxfComment *
00088 dxf_comment_init
00089 (
00090         DxfComment *comment
00092 )
00093 {
00094 #ifdef DEBUG
00095         DXF_DEBUG_BEGIN
00096 #endif
00097         /* Do some basic checks. */
00098         if (comment == NULL)
00099         {
00100                 fprintf (stderr,
00101                   (_("Warning in %s () a NULL pointer was passed.\n")),
00102                   __FUNCTION__);
00103                 comment = dxf_comment_new ();
00104         }
00105         if (comment == NULL)
00106         {
00107                 fprintf (stderr,
00108                   (_("Error in %s () could not allocate memory for a DxfComment struct.\n")),
00109                   __FUNCTION__);
00110                 return (NULL);
00111         }
00112         dxf_comment_set_id_code (comment, 0);
00113         dxf_comment_set_value (comment, strdup (""));
00114         dxf_comment_set_next (comment, NULL);
00115 #ifdef DEBUG
00116         DXF_DEBUG_END
00117 #endif
00118         return (comment);
00119 }
00120 
00121 
00138 int
00139 dxf_comment_write
00140 (
00141         DxfFile *fp,
00143         DxfComment *comment
00145 )
00146 {
00147 #if DEBUG
00148         DXF_DEBUG_BEGIN
00149 #endif
00150         /* Do some basic checks. */
00151         if (fp == NULL)
00152         {
00153                 fprintf (stderr,
00154                   (_("Error in %s () a NULL file pointer was passed.\n")),
00155                   __FUNCTION__);
00156                 return (EXIT_FAILURE);
00157         }
00158         /* Start writing output. */
00159         DxfComment *iter = (DxfComment *) comment;
00160         while (dxf_comment_get_value (iter) != NULL)
00161         {
00162                 fprintf (fp->fp, "999\n%s\n", dxf_comment_get_value (iter));
00163                 iter = dxf_comment_get_next (iter);
00164         }
00165 #if DEBUG
00166         DXF_DEBUG_END
00167 #endif
00168         return (EXIT_SUCCESS);
00169 }
00170 
00171 
00179 int
00180 dxf_comment_free
00181 (
00182         DxfComment *comment
00185 )
00186 {
00187 #ifdef DEBUG
00188         DXF_DEBUG_BEGIN
00189 #endif
00190         if (comment == NULL)
00191         {
00192                 fprintf (stderr,
00193                   (_("Error in %s () a NULL pointer was passed.\n")),
00194                   __FUNCTION__);
00195                 return (EXIT_FAILURE);
00196         }
00197         if (comment->next != NULL)
00198         {
00199                 fprintf (stderr,
00200                   (_("Error in %s () pointer to next was not NULL.\n")),
00201                   __FUNCTION__);
00202                 return (EXIT_FAILURE);
00203         }
00204         free (dxf_comment_get_value (comment));
00205         free (comment);
00206         comment = NULL;
00207 #ifdef DEBUG
00208         DXF_DEBUG_END
00209 #endif
00210         return (EXIT_SUCCESS);
00211 }
00212 
00213 
00218 void
00219 dxf_comment_free_chain
00220 (
00221         DxfComment *comments
00223 )
00224 {
00225 #ifdef DEBUG
00226         DXF_DEBUG_BEGIN
00227 #endif
00228         if (comments == NULL)
00229         {
00230                 fprintf (stderr,
00231                   (_("Warning in %s () a NULL pointer was passed.\n")),
00232                   __FUNCTION__);
00233         }
00234         while (comments != NULL)
00235         {
00236                 struct DxfComment *iter = comments->next;
00237                 dxf_comment_free (comments);
00238                 comments = (DxfComment *) iter;
00239         }
00240 #if DEBUG
00241         DXF_DEBUG_END
00242 #endif
00243 }
00244 
00245 
00251 int
00252 dxf_comment_get_id_code
00253 (
00254         DxfComment *comment
00256 )
00257 {
00258 #if DEBUG
00259         DXF_DEBUG_BEGIN
00260 #endif
00261         /* Do some basic checks. */
00262         if (comment == NULL)
00263         {
00264                 fprintf (stderr,
00265                   (_("Error in %s () a NULL pointer was passed.\n")),
00266                   __FUNCTION__);
00267                 return (EXIT_FAILURE);
00268         }
00269         if (comment->id_code < 0)
00270         {
00271                 fprintf (stderr,
00272                   (_("Error in %s () a negative value was found in the id-code member.\n")),
00273                   __FUNCTION__);
00274                 return (EXIT_FAILURE);
00275         }
00276 #if DEBUG
00277         DXF_DEBUG_END
00278 #endif
00279         return (comment->id_code);
00280 }
00281 
00282 
00286 DxfComment *
00287 dxf_comment_set_id_code
00288 (
00289         DxfComment *comment,
00291         int id_code
00295 )
00296 {
00297 #if DEBUG
00298         DXF_DEBUG_BEGIN
00299 #endif
00300         /* Do some basic checks. */
00301         if (comment == NULL)
00302         {
00303                 fprintf (stderr,
00304                   (_("Error in %s () a NULL pointer was passed.\n")),
00305                   __FUNCTION__);
00306                 return (NULL);
00307         }
00308         if (id_code < 0)
00309         {
00310                 fprintf (stderr,
00311                   (_("Error in %s () a negative id-code value was passed.\n")),
00312                   __FUNCTION__);
00313                 return (NULL);
00314         }
00315         comment->id_code = id_code;
00316 #if DEBUG
00317         DXF_DEBUG_END
00318 #endif
00319         return (comment);
00320 }
00321 
00322 
00326 char *
00327 dxf_comment_get_value
00328 (
00329         DxfComment *comment
00331 )
00332 {
00333 #ifdef DEBUG
00334         DXF_DEBUG_BEGIN
00335 #endif
00336         if (comment == NULL)
00337         {
00338                 fprintf (stderr,
00339                   (_("Error in %s () a NULL pointer was passed.\n")),
00340                   __FUNCTION__);
00341               return (NULL);
00342         }
00343         if (comment->value == NULL)
00344         {
00345               fprintf (stderr,
00346                   (_("Warning in %s () a NULL pointer was found.\n")),
00347                 __FUNCTION__);
00348               return (NULL);
00349         }
00350 #if DEBUG
00351         DXF_DEBUG_END
00352 #endif
00353         return (comment->value);
00354 }
00355 
00356 
00360 DxfComment *
00361 dxf_comment_set_value
00362 (
00363         DxfComment *comment,
00365         char *value
00367 )
00368 {
00369 #ifdef DEBUG
00370         DXF_DEBUG_BEGIN
00371 #endif
00372         if (comment == NULL)
00373         {
00374                 fprintf (stderr,
00375                   (_("Error in %s () a NULL pointer was passed.\n")),
00376                   __FUNCTION__);
00377               return (NULL);
00378         }
00379         if (value != NULL)
00380         {
00381               fprintf (stderr,
00382                   (_("Error in %s () a NULL pointer was passed.\n")),
00383                 __FUNCTION__);
00384               return (NULL);
00385         }
00386         comment->value = strdup (value);
00387 #if DEBUG
00388         DXF_DEBUG_END
00389 #endif
00390         return (comment);
00391 }
00392 
00393 
00402 DxfComment *
00403 dxf_comment_get_next
00404 (
00405         DxfComment *comment
00407 )
00408 {
00409 #if DEBUG
00410         DXF_DEBUG_BEGIN
00411 #endif
00412         /* Do some basic checks. */
00413         if (comment == NULL)
00414         {
00415                 fprintf (stderr,
00416                   (_("Error in %s () a NULL pointer was passed.\n")),
00417                   __FUNCTION__);
00418                 return (NULL);
00419         }
00420         if (comment->next == NULL)
00421         {
00422                 fprintf (stderr,
00423                   (_("Error in %s () a NULL pointer was found in the next member.\n")),
00424                   __FUNCTION__);
00425                 return (NULL);
00426         }
00427 #if DEBUG
00428         DXF_DEBUG_END
00429 #endif
00430         return ((DxfComment *) comment->next);
00431 }
00432 
00433 
00438 DxfComment *
00439 dxf_comment_set_next
00440 (
00441         DxfComment *comment,
00443         DxfComment *next
00445 )
00446 {
00447 #if DEBUG
00448         DXF_DEBUG_BEGIN
00449 #endif
00450         /* Do some basic checks. */
00451         if (comment == NULL)
00452         {
00453                 fprintf (stderr,
00454                   (_("Error in %s () a NULL pointer was passed.\n")),
00455                   __FUNCTION__);
00456                 return (NULL);
00457         }
00458         if (next == NULL)
00459         {
00460                 fprintf (stderr,
00461                   (_("Error in %s () a NULL pointer was passed.\n")),
00462                   __FUNCTION__);
00463                 return (NULL);
00464         }
00465         comment->next = (struct DxfComment *) next;
00466 #if DEBUG
00467         DXF_DEBUG_END
00468 #endif
00469         return (comment);
00470 }
00471 
00472 
00481 DxfComment *
00482 dxf_comment_get_last
00483 (
00484         DxfComment *comment
00486 )
00487 {
00488 #if DEBUG
00489         DXF_DEBUG_BEGIN
00490 #endif
00491         /* Do some basic checks. */
00492         if (comment == NULL)
00493         {
00494                 fprintf (stderr,
00495                   (_("Error in %s () a NULL pointer was passed.\n")),
00496                   __FUNCTION__);
00497                 return (NULL);
00498         }
00499         if (comment->next == NULL)
00500         {
00501                 fprintf (stderr,
00502                   (_("Warning in %s () a NULL pointer was found in the next member.\n")),
00503                   __FUNCTION__);
00504                 return ((DxfComment *) comment);
00505         }
00506         DxfComment *iter = (DxfComment *) comment->next;
00507         while (iter->next != NULL)
00508         {
00509                 iter = (DxfComment *) iter->next;
00510         }
00511 #if DEBUG
00512         DXF_DEBUG_END
00513 #endif
00514         return ((DxfComment *) iter);
00515 }
00516 
00517 
00518 /* EOF */