libDXF 0.0.1
A library with DXF related functions written in C.
|
00001 00038 #include <stdarg.h> 00039 #include "util.h" 00040 00041 00047 DxfChar * 00048 dxf_char_new () 00049 { 00050 #if DEBUG 00051 DXF_DEBUG_BEGIN 00052 #endif 00053 DxfChar *c = NULL; 00054 size_t size; 00055 00056 size = sizeof (DxfChar); 00057 /* avoid malloc of 0 bytes */ 00058 if (size == 0) size = 1; 00059 if ((c = malloc (size)) == NULL) 00060 { 00061 fprintf (stderr, 00062 (_("Error in %s () could not allocate memory for a DxfChar struct.\n")), 00063 __FUNCTION__); 00064 c = NULL; 00065 } 00066 else 00067 { 00068 memset (c, 0, size); 00069 } 00070 #if DEBUG 00071 DXF_DEBUG_END 00072 #endif 00073 return (c); 00074 } 00075 00076 00084 DxfChar * 00085 dxf_char_init 00086 ( 00087 DxfChar *c 00089 ) 00090 { 00091 #if DEBUG 00092 DXF_DEBUG_BEGIN 00093 #endif 00094 /* Do some basic checks. */ 00095 if (c == NULL) 00096 { 00097 fprintf (stderr, 00098 (_("Warning in %s () a NULL pointer was passed.\n")), 00099 __FUNCTION__); 00100 c = dxf_char_new (); 00101 } 00102 if (c == NULL) 00103 { 00104 fprintf (stderr, 00105 (_("Error in %s () could not allocate memory for a DxfChar struct.\n")), 00106 __FUNCTION__); 00107 return (NULL); 00108 } 00109 c->value = strdup (""); 00110 c->length = 0; 00111 c->next = NULL; 00112 #if DEBUG 00113 DXF_DEBUG_END 00114 #endif 00115 return (c); 00116 } 00117 00118 00126 int 00127 dxf_char_free 00128 ( 00129 DxfChar *c 00132 ) 00133 { 00134 #if DEBUG 00135 DXF_DEBUG_BEGIN 00136 #endif 00137 /* Do some basic checks. */ 00138 if (c == NULL) 00139 { 00140 fprintf (stderr, 00141 (_("Error in %s () a NULL pointer was passed.\n")), 00142 __FUNCTION__); 00143 return (EXIT_FAILURE); 00144 } 00145 if (c->next != NULL) 00146 { 00147 fprintf (stderr, 00148 (_("Error in %s () pointer to next was not NULL.\n")), 00149 __FUNCTION__); 00150 return (EXIT_FAILURE); 00151 } 00152 free (c->value); 00153 free (c); 00154 c = NULL; 00155 #if DEBUG 00156 DXF_DEBUG_END 00157 #endif 00158 return (EXIT_SUCCESS); 00159 } 00160 00161 00166 int 00167 dxf_char_free_chain 00168 ( 00169 DxfChar *chars 00172 ) 00173 { 00174 #ifdef DEBUG 00175 DXF_DEBUG_BEGIN 00176 #endif 00177 if (chars == NULL) 00178 { 00179 fprintf (stderr, 00180 (_("Warning in %s () a NULL pointer was passed.\n")), 00181 __FUNCTION__); 00182 return (EXIT_FAILURE); 00183 } 00184 while (chars != NULL) 00185 { 00186 struct DxfChar *iter = chars->next; 00187 dxf_char_free (chars); 00188 chars = (DxfChar *) iter; 00189 } 00190 #if DEBUG 00191 DXF_DEBUG_END 00192 #endif 00193 return (EXIT_SUCCESS); 00194 } 00195 00196 00202 DxfDouble * 00203 dxf_double_new () 00204 { 00205 #if DEBUG 00206 DXF_DEBUG_BEGIN 00207 #endif 00208 DxfDouble *d = NULL; 00209 size_t size; 00210 00211 size = sizeof (DxfDouble); 00212 /* avoid malloc of 0 bytes */ 00213 if (size == 0) size = 1; 00214 if ((d = malloc (size)) == NULL) 00215 { 00216 fprintf (stderr, 00217 (_("Error in %s () could not allocate memory for a DxfDouble struct.\n")), 00218 __FUNCTION__); 00219 d = NULL; 00220 } 00221 else 00222 { 00223 memset (d, 0, size); 00224 } 00225 #if DEBUG 00226 DXF_DEBUG_END 00227 #endif 00228 return (d); 00229 } 00230 00231 00239 DxfDouble * 00240 dxf_double_init 00241 ( 00242 DxfDouble *d 00244 ) 00245 { 00246 #if DEBUG 00247 DXF_DEBUG_BEGIN 00248 #endif 00249 /* Do some basic checks. */ 00250 if (d == NULL) 00251 { 00252 fprintf (stderr, 00253 (_("Warning in %s () a NULL pointer was passed.\n")), 00254 __FUNCTION__); 00255 d = dxf_double_new (); 00256 } 00257 if (d == NULL) 00258 { 00259 fprintf (stderr, 00260 (_("Error in %s () could not allocate memory for a DxfDouble struct.\n")), 00261 __FUNCTION__); 00262 return (NULL); 00263 } 00264 d->value = 0.0; 00265 d->next = NULL; 00266 #if DEBUG 00267 DXF_DEBUG_END 00268 #endif 00269 return (d); 00270 } 00271 00272 00280 int 00281 dxf_double_free 00282 ( 00283 DxfDouble *d 00286 ) 00287 { 00288 #if DEBUG 00289 DXF_DEBUG_BEGIN 00290 #endif 00291 /* Do some basic checks. */ 00292 if (d == NULL) 00293 { 00294 fprintf (stderr, 00295 (_("Error in %s () a NULL pointer was passed.\n")), 00296 __FUNCTION__); 00297 return (EXIT_FAILURE); 00298 } 00299 if (d->next != NULL) 00300 { 00301 fprintf (stderr, 00302 (_("Error in %s () pointer to next was not NULL.\n")), 00303 __FUNCTION__); 00304 return (EXIT_FAILURE); 00305 } 00306 free (d); 00307 d = NULL; 00308 #if DEBUG 00309 DXF_DEBUG_END 00310 #endif 00311 return (EXIT_SUCCESS); 00312 } 00313 00314 00319 int 00320 dxf_double_free_chain 00321 ( 00322 DxfDouble *doubles 00325 ) 00326 { 00327 #ifdef DEBUG 00328 DXF_DEBUG_BEGIN 00329 #endif 00330 if (doubles == NULL) 00331 { 00332 fprintf (stderr, 00333 (_("Warning in %s () a NULL pointer was passed.\n")), 00334 __FUNCTION__); 00335 return (EXIT_FAILURE); 00336 } 00337 while (doubles != NULL) 00338 { 00339 struct DxfDouble *iter = doubles->next; 00340 dxf_double_free (doubles); 00341 doubles = (DxfDouble *) iter; 00342 } 00343 #if DEBUG 00344 DXF_DEBUG_END 00345 #endif 00346 return (EXIT_SUCCESS); 00347 } 00348 00349 00355 DxfInt * 00356 dxf_int_new () 00357 { 00358 #if DEBUG 00359 DXF_DEBUG_BEGIN 00360 #endif 00361 DxfInt *i = NULL; 00362 size_t size; 00363 00364 size = sizeof (DxfInt); 00365 /* avoid malloc of 0 bytes */ 00366 if (size == 0) size = 1; 00367 if ((i = malloc (size)) == NULL) 00368 { 00369 fprintf (stderr, 00370 (_("Error in %s () could not allocate memory for a DxfInt struct.\n")), 00371 __FUNCTION__); 00372 i = NULL; 00373 } 00374 else 00375 { 00376 memset (i, 0, size); 00377 } 00378 #if DEBUG 00379 DXF_DEBUG_END 00380 #endif 00381 return (i); 00382 } 00383 00384 00392 DxfInt * 00393 dxf_int_init 00394 ( 00395 DxfInt *i 00397 ) 00398 { 00399 #if DEBUG 00400 DXF_DEBUG_BEGIN 00401 #endif 00402 /* Do some basic checks. */ 00403 if (i == NULL) 00404 { 00405 fprintf (stderr, 00406 (_("Warning in %s () a NULL pointer was passed.\n")), 00407 __FUNCTION__); 00408 i = dxf_int_new (); 00409 } 00410 if (i == NULL) 00411 { 00412 fprintf (stderr, 00413 (_("Error in %s () could not allocate memory for a DxfInt struct.\n")), 00414 __FUNCTION__); 00415 return (NULL); 00416 } 00417 i->value = 0; 00418 i->next = NULL; 00419 #if DEBUG 00420 DXF_DEBUG_END 00421 #endif 00422 return (i); 00423 } 00424 00425 00433 int 00434 dxf_int_free 00435 ( 00436 DxfInt *i 00439 ) 00440 { 00441 #if DEBUG 00442 DXF_DEBUG_BEGIN 00443 #endif 00444 /* Do some basic checks. */ 00445 if (i == NULL) 00446 { 00447 fprintf (stderr, 00448 (_("Error in %s () a NULL pointer was passed.\n")), 00449 __FUNCTION__); 00450 return (EXIT_FAILURE); 00451 } 00452 if (i->next != NULL) 00453 { 00454 fprintf (stderr, 00455 (_("Error in %s () pointer to next was not NULL.\n")), 00456 __FUNCTION__); 00457 return (EXIT_FAILURE); 00458 } 00459 free (i); 00460 i = NULL; 00461 #if DEBUG 00462 DXF_DEBUG_END 00463 #endif 00464 return (EXIT_SUCCESS); 00465 } 00466 00467 00472 int 00473 dxf_int_free_chain 00474 ( 00475 DxfInt *ints 00478 ) 00479 { 00480 #ifdef DEBUG 00481 DXF_DEBUG_BEGIN 00482 #endif 00483 if (ints == NULL) 00484 { 00485 fprintf (stderr, 00486 (_("Warning in %s () a NULL pointer was passed.\n")), 00487 __FUNCTION__); 00488 return (EXIT_FAILURE); 00489 } 00490 while (ints != NULL) 00491 { 00492 struct DxfInt *iter = ints->next; 00493 dxf_int_free (ints); 00494 ints = (DxfInt *) iter; 00495 } 00496 #if DEBUG 00497 DXF_DEBUG_END 00498 #endif 00499 return (EXIT_SUCCESS); 00500 } 00501 00502 00503 int 00504 dxf_read_is_double (int type) 00505 { 00506 #if DEBUG 00507 DXF_DEBUG_BEGIN 00508 #endif 00509 if (type >=10 && type < 60) 00510 { 00511 #if DEBUG 00512 DXF_DEBUG_END 00513 #endif 00514 return TRUE; 00515 } 00516 else 00517 { 00518 #if DEBUG 00519 DXF_DEBUG_END 00520 #endif 00521 return FALSE; 00522 } 00523 } 00524 00525 00526 int 00527 dxf_read_is_int (int type) 00528 { 00529 #if DEBUG 00530 DXF_DEBUG_BEGIN 00531 #endif 00532 if (type >= 60 && type < 80) 00533 { 00534 #if DEBUG 00535 DXF_DEBUG_END 00536 #endif 00537 return TRUE; 00538 } 00539 else 00540 { 00541 #if DEBUG 00542 DXF_DEBUG_END 00543 #endif 00544 return FALSE; 00545 } 00546 } 00547 00548 00549 int 00550 dxf_read_is_string (int type) 00551 { 00552 #if DEBUG 00553 DXF_DEBUG_BEGIN 00554 #endif 00555 if (type >= 0 && type <= 9) 00556 { 00557 #if DEBUG 00558 DXF_DEBUG_END 00559 #endif 00560 return TRUE; 00561 } 00562 else 00563 { 00564 #if DEBUG 00565 DXF_DEBUG_END 00566 #endif 00567 return FALSE; 00568 } 00569 } 00570 00578 DxfFile * 00579 dxf_read_init (const char *filename) 00580 { 00581 #if DEBUG 00582 DXF_DEBUG_BEGIN 00583 #endif 00584 DxfFile * file = NULL; 00585 FILE *fp; 00586 if (!filename) 00587 { 00588 fprintf (stderr, 00589 (_("Error: filename is not initialised (NULL pointer).\n"))); 00590 return (NULL); 00591 } 00592 if (strcmp (filename, "") == 0) 00593 { 00594 fprintf (stderr, 00595 (_("Error: filename contains an empty string.\n"))); 00596 return (NULL); 00597 } 00598 fp = fopen (filename, "r"); 00599 if (!fp) 00600 { 00601 fprintf (stderr, 00602 (_("Error: could not open file: %s for reading (NULL pointer).\n")), 00603 filename); 00604 return (NULL); 00605 } 00606 file = malloc (sizeof(DxfFile)); 00607 file->fp = fp; 00608 file->filename = strdup(filename); 00609 file->line_number = 0; 00614 #if DEBUG 00615 DXF_DEBUG_END 00616 #endif 00617 return file; 00618 } 00619 00620 00621 void 00622 dxf_read_close (DxfFile *file) 00623 { 00624 #if DEBUG 00625 DXF_DEBUG_BEGIN 00626 #endif 00627 00628 if (file == NULL) 00629 { 00630 fprintf (stderr, 00631 (_("Error: file is not initialised (NULL pointer).\n"))); 00632 } 00633 else 00634 { 00635 fclose (file->fp); 00636 free (file->filename); 00637 free (file); 00638 file = NULL; 00639 } 00640 #if DEBUG 00641 DXF_DEBUG_END 00642 #endif 00643 } 00644 00645 00652 int 00653 dxf_read_line (char * temp_string, DxfFile *fp) 00654 { 00655 #if DEBUG 00656 DXF_DEBUG_BEGIN 00657 #endif 00658 int ret; 00659 00660 ret = fscanf (fp->fp, "%[^\n]\n", temp_string); 00661 if (ferror (fp->fp)) 00662 { 00663 fprintf (stderr, 00664 (_("Error: while reading from: %s in line: %d.\n")), 00665 fp->filename, fp->line_number); 00666 return (EXIT_FAILURE); 00667 } 00668 if (ret) 00669 { 00670 fp->line_number++; 00671 } 00672 #if DEBUG 00673 DXF_DEBUG_END 00674 #endif 00675 return ret; 00676 } 00677 00684 int 00685 dxf_read_scanf (DxfFile *fp, const char *template, ...) 00686 { 00687 #if DEBUG 00688 DXF_DEBUG_BEGIN 00689 #endif 00690 int ret; 00691 char * search_result; 00692 va_list lst; 00693 va_start (lst, template); 00694 ret = vfscanf (fp->fp, template, lst); 00695 if (ferror (fp->fp)) 00696 { 00697 fprintf (stderr, 00698 (_("Error: while reading from: %s in line: %d.\n")), 00699 fp->filename, fp->line_number); 00700 return (EXIT_FAILURE); 00701 } 00702 va_end (lst); 00703 if (ret) 00704 { 00705 /* 00706 * we have to find each \n from the template to know how many lines will we read; 00707 */ 00708 search_result = (char *) template; 00709 while (TRUE) 00710 { 00711 search_result = strstr (search_result, "\n"); 00712 if (search_result == NULL) 00713 break; 00714 fp->line_number++; 00715 *++search_result; 00716 } 00717 } 00718 return ret; 00719 #if DEBUG 00720 DXF_DEBUG_END 00721 #endif 00722 } 00723 00724 00725 /* EOF */