#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Go to the source code of this file.
Data Structures | |
struct | s_linked_vptr |
struct | s_linked_int |
struct | s_ivec |
Defines | |
#define | IN |
#define | OUT |
#define | INOUT |
#define | ERRTAG "ERROR:\t" |
#define | WARNTAG "WARN:\t" |
#define | BUFSIZE 300 |
#define | nint(a) ((int) floor (a + 0.5)) |
Typedefs | |
typedef struct s_linked_int | t_linked_int |
typedef struct s_ivec | t_ivec |
Enumerations | |
enum | boolean { FALSE, TRUE } |
Functions | |
int | limit_value (int cur, int max, const char *name) |
void * | my_calloc (size_t nelem, size_t size) |
void * | my_malloc (size_t size) |
void * | my_realloc (void *ptr, size_t size) |
void * | my_chunk_malloc (size_t size, struct s_linked_vptr **chunk_ptr_head, int *mem_avail_ptr, char **next_mem_loc_ptr) |
void | free_chunk_memory (struct s_linked_vptr *chunk_ptr_head) |
void | free_ivec_vector (struct s_ivec *ivec_vector, int nrmin, int nrmax) |
void | free_ivec_matrix (struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax) |
void | free_ivec_matrix3 (struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax) |
void ** | alloc_matrix (int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize) |
void *** | alloc_matrix3 (int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize) |
void **** | alloc_matrix4 (int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, int nemax, size_t elsize) |
void | free_matrix (void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize) |
void | free_matrix3 (void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize) |
void | free_matrix4 (void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, size_t elsize) |
void | print_int_matrix3 (int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file) |
struct s_linked_vptr * | insert_in_vptr_list (struct s_linked_vptr *head, void *vptr_to_add) |
t_linked_int * | insert_in_int_list (t_linked_int *head, int data, t_linked_int **free_list_head_ptr) |
void | free_int_list (t_linked_int **int_list_head_ptr) |
void | alloc_ivector_and_copy_int_list (t_linked_int **list_head_ptr, int num_items, struct s_ivec *ivec, t_linked_int **free_list_head_ptr) |
FILE * | my_fopen (const char *fname, const char *flag) |
char * | my_strtok (char *ptr, char *tokens, FILE *fp, char *buf) |
char * | my_fgets (char *buf, int max_size, FILE *fp) |
int | my_atoi (const char *str) |
char * | my_strdup (const char *str) |
char * | my_strncpy (char *dest, const char *src, size_t size) |
void | my_srandom (int seed) |
int | my_irand (int imax) |
float | my_frand (void) |
Variables | |
int | linenum |
typedef struct s_linked_int t_linked_int |
void alloc_ivector_and_copy_int_list | ( | t_linked_int ** | list_head_ptr, | |
int | num_items, | |||
struct s_ivec * | ivec, | |||
t_linked_int ** | free_list_head_ptr | |||
) |
Definition at line 379 of file util.c.
00383 { 00384 00385 /* Allocates an integer vector with num_items elements and copies the * 00386 * integers from the list pointed to by list_head (of which there must be * 00387 * num_items) over to it. The int_list is then put on the free list, and * 00388 * the list_head_ptr is set to NULL. */ 00389 00390 t_linked_int *linked_int, *list_head; 00391 int i, *list; 00392 00393 list_head = *list_head_ptr; 00394 00395 if(num_items == 0) 00396 { /* Empty list. */ 00397 ivec->nelem = 0; 00398 ivec->list = NULL; 00399 00400 if(list_head != NULL) 00401 { 00402 printf(ERRTAG 00403 "alloc_ivector_and_copy_int_list: Copied %d elements, " 00404 "but list at %p contains more.\n", num_items, 00405 (void *)list_head); 00406 exit(1); 00407 } 00408 return; 00409 } 00410 00411 ivec->nelem = num_items; 00412 list = (int *)my_malloc(num_items * sizeof(int)); 00413 ivec->list = list; 00414 linked_int = list_head; 00415 00416 for(i = 0; i < num_items - 1; i++) 00417 { 00418 list[i] = linked_int->data; 00419 linked_int = linked_int->next; 00420 } 00421 00422 list[num_items - 1] = linked_int->data; 00423 00424 if(linked_int->next != NULL) 00425 { 00426 printf 00427 ("Error in alloc_ivector_and_copy_int_list:\n Copied %d elements, " 00428 "but list at %p contains more.\n", num_items, 00429 (void *)list_head); 00430 exit(1); 00431 } 00432 00433 linked_int->next = *free_list_head_ptr; 00434 *free_list_head_ptr = list_head; 00435 *list_head_ptr = NULL; 00436 }
void** alloc_matrix | ( | int | nrmin, | |
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
size_t | elsize | |||
) |
Definition at line 613 of file util.c.
00618 { 00619 00620 /* allocates an generic matrix with nrmax-nrmin + 1 rows and ncmax - * 00621 * ncmin + 1 columns, with each element of size elsize. i.e. * 00622 * returns a pointer to a storage block [nrmin..nrmax][ncmin..ncmax].* 00623 * Simply cast the returned array pointer to the proper type. */ 00624 00625 int i; 00626 char **cptr; 00627 00628 cptr = (char **)my_malloc((nrmax - nrmin + 1) * sizeof(char *)); 00629 cptr -= nrmin; 00630 for(i = nrmin; i <= nrmax; i++) 00631 { 00632 cptr[i] = (char *)my_malloc((ncmax - ncmin + 1) * elsize); 00633 cptr[i] -= ncmin * elsize / sizeof(char); /* sizeof(char) = 1 */ 00634 } 00635 return ((void **)cptr); 00636 }
void*** alloc_matrix3 | ( | int | nrmin, | |
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
int | ndmax, | |||
size_t | elsize | |||
) |
Definition at line 662 of file util.c.
00669 { 00670 00671 /* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - * 00672 * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each * 00673 * element of size elsize. i.e. returns a pointer to a storage block * 00674 * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the * 00675 * returned array pointer to the proper type. */ 00676 00677 int i, j; 00678 char ***cptr; 00679 00680 cptr = (char ***)my_malloc((nrmax - nrmin + 1) * sizeof(char **)); 00681 cptr -= nrmin; 00682 for(i = nrmin; i <= nrmax; i++) 00683 { 00684 cptr[i] = 00685 (char **)my_malloc((ncmax - ncmin + 1) * sizeof(char *)); 00686 cptr[i] -= ncmin; 00687 for(j = ncmin; j <= ncmax; j++) 00688 { 00689 cptr[i][j] = 00690 (char *)my_malloc((ndmax - ndmin + 1) * elsize); 00691 cptr[i][j] -= ndmin * elsize / sizeof(char); /* sizeof(char) = 1) */ 00692 } 00693 } 00694 return ((void ***)cptr); 00695 }
void**** alloc_matrix4 | ( | int | nrmin, | |
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
int | ndmax, | |||
int | nemin, | |||
int | nemax, | |||
size_t | elsize | |||
) |
Definition at line 698 of file util.c.
00707 { 00708 00709 /* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - * 00710 * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each * 00711 * element of size elsize. i.e. returns a pointer to a storage block * 00712 * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the * 00713 * returned array pointer to the proper type. */ 00714 00715 int i, j, k; 00716 char ****cptr; 00717 00718 cptr = (char ****)my_malloc((nrmax - nrmin + 1) * sizeof(char ***)); 00719 cptr -= nrmin; 00720 for(i = nrmin; i <= nrmax; i++) 00721 { 00722 cptr[i] = 00723 (char ***)my_malloc((ncmax - ncmin + 1) * sizeof(char **)); 00724 cptr[i] -= ncmin; 00725 for(j = ncmin; j <= ncmax; j++) 00726 { 00727 cptr[i][j] = 00728 (char **)my_malloc((ndmax - ndmin + 1) * 00729 sizeof(char *)); 00730 cptr[i][j] -= ndmin; 00731 for(k = ndmin; k <= ndmax; k++) 00732 { 00733 cptr[i][j][k] = 00734 (char *)my_malloc((nemax - nemin + 1) * 00735 elsize); 00736 cptr[i][j][k] -= nemin * elsize / sizeof(char); /* sizeof(char) = 1) */ 00737 } 00738 } 00739 } 00740 return ((void ****)cptr); 00741 }
void free_chunk_memory | ( | struct s_linked_vptr * | chunk_ptr_head | ) |
Definition at line 289 of file util.c.
00290 { 00291 00292 /* Frees the memory allocated by a sequence of calls to my_chunk_malloc. */ 00293 00294 struct s_linked_vptr *curr_ptr, *prev_ptr; 00295 00296 curr_ptr = chunk_ptr_head; 00297 00298 while(curr_ptr != NULL) 00299 { 00300 free(curr_ptr->data_vptr); /* Free memory "chunk". */ 00301 prev_ptr = curr_ptr; 00302 curr_ptr = curr_ptr->next; 00303 free(prev_ptr); /* Free memory used to track "chunk". */ 00304 } 00305 }
void free_int_list | ( | t_linked_int ** | int_list_head_ptr | ) |
Definition at line 357 of file util.c.
00358 { 00359 00360 /* This routine truly frees (calls free) all the integer list elements * 00361 * on the linked list pointed to by *head, and sets head = NULL. */ 00362 00363 t_linked_int *linked_int, *next_linked_int; 00364 00365 linked_int = *int_list_head_ptr; 00366 00367 while(linked_int != NULL) 00368 { 00369 next_linked_int = linked_int->next; 00370 free(linked_int); 00371 linked_int = next_linked_int; 00372 } 00373 00374 *int_list_head_ptr = NULL; 00375 }
void free_ivec_matrix | ( | struct s_ivec ** | ivec_matrix, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
int | ncmax | |||
) |
Definition at line 553 of file util.c.
00558 { 00559 00560 /* Frees a 2D matrix of integer vectors (ivecs). */ 00561 00562 int i, j; 00563 00564 for(i = nrmin; i <= nrmax; i++) 00565 { 00566 for(j = ncmin; j <= ncmax; j++) 00567 { 00568 if(ivec_matrix[i][j].nelem != 0) 00569 { 00570 free(ivec_matrix[i][j].list); 00571 } 00572 } 00573 } 00574 00575 free_matrix(ivec_matrix, nrmin, nrmax, ncmin, sizeof(struct s_ivec)); 00576 }
void free_ivec_matrix3 | ( | struct s_ivec *** | ivec_matrix3, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
int | ndmax | |||
) |
Definition at line 580 of file util.c.
00587 { 00588 00589 /* Frees a 3D matrix of integer vectors (ivecs). */ 00590 00591 int i, j, k; 00592 00593 for(i = nrmin; i <= nrmax; i++) 00594 { 00595 for(j = ncmin; j <= ncmax; j++) 00596 { 00597 for(k = ndmin; k <= ndmax; k++) 00598 { 00599 if(ivec_matrix3[i][j][k].nelem != 0) 00600 { 00601 free(ivec_matrix3[i][j][k].list); 00602 } 00603 } 00604 } 00605 } 00606 00607 free_matrix3(ivec_matrix3, nrmin, nrmax, ncmin, ncmax, ndmin, 00608 sizeof(struct s_ivec)); 00609 }
void free_ivec_vector | ( | struct s_ivec * | ivec_vector, | |
int | nrmin, | |||
int | nrmax | |||
) |
Definition at line 535 of file util.c.
00538 { 00539 00540 /* Frees a 1D array of integer vectors. */ 00541 00542 int i; 00543 00544 for(i = nrmin; i <= nrmax; i++) 00545 if(ivec_vector[i].nelem != 0) 00546 free(ivec_vector[i].list); 00547 00548 free(ivec_vector + nrmin); 00549 }
void free_matrix | ( | void * | vptr, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
size_t | elsize | |||
) |
void free_matrix3 | ( | void * | vptr, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
size_t | elsize | |||
) |
Definition at line 777 of file util.c.
00784 { 00785 00786 int i, j; 00787 char ***cptr; 00788 00789 cptr = (char ***)vptr; 00790 00791 for(i = nrmin; i <= nrmax; i++) 00792 { 00793 for(j = ncmin; j <= ncmax; j++) 00794 free(cptr[i][j] + ndmin * elsize / sizeof(char)); 00795 free(cptr[i] + ncmin); 00796 } 00797 free(cptr + nrmin); 00798 }
void free_matrix4 | ( | void * | vptr, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
int | ndmax, | |||
int | nemin, | |||
size_t | elsize | |||
) |
Definition at line 801 of file util.c.
00810 { 00811 00812 int i, j, k; 00813 char ****cptr; 00814 00815 cptr = (char ****)vptr; 00816 00817 for(i = nrmin; i <= nrmax; i++) 00818 { 00819 for(j = ncmin; j <= ncmax; j++) 00820 { 00821 for(k = ndmin; k <= ndmax; k++) 00822 free(cptr[i][j][k] + nemin * elsize / sizeof(char)); 00823 free(cptr[i][j] + ndmin * elsize / sizeof(char)); 00824 } 00825 free(cptr[i] + ncmin); 00826 } 00827 free(cptr + nrmin); 00828 }
t_linked_int* insert_in_int_list | ( | t_linked_int * | head, | |
int | data, | |||
t_linked_int ** | free_list_head_ptr | |||
) |
Definition at line 328 of file util.c.
00331 { 00332 00333 /* Inserts a new element at the head of a linked list of integers. Returns * 00334 * the new head of the list. One argument is the address of the head of * 00335 * a list of free ilist elements. If there are any elements on this free * 00336 * list, the new element is taken from it. Otherwise a new one is malloced. */ 00337 00338 t_linked_int *linked_int; 00339 00340 if(*free_list_head_ptr != NULL) 00341 { 00342 linked_int = *free_list_head_ptr; 00343 *free_list_head_ptr = linked_int->next; 00344 } 00345 else 00346 { 00347 linked_int = (t_linked_int *) my_malloc(sizeof(t_linked_int)); 00348 } 00349 00350 linked_int->data = data; 00351 linked_int->next = head; 00352 return (linked_int); 00353 }
struct s_linked_vptr* insert_in_vptr_list | ( | struct s_linked_vptr * | head, | |
void * | vptr_to_add | |||
) | [read] |
Definition at line 309 of file util.c.
00311 { 00312 00313 /* Inserts a new element at the head of a linked list of void pointers. * 00314 * Returns the new head of the list. */ 00315 00316 struct s_linked_vptr *linked_vptr; 00317 00318 linked_vptr = (struct s_linked_vptr *)my_malloc(sizeof(struct 00319 s_linked_vptr)); 00320 00321 linked_vptr->data_vptr = vptr_to_add; 00322 linked_vptr->next = head; 00323 return (linked_vptr); /* New head of the list */ 00324 }
int limit_value | ( | int | cur, | |
int | max, | |||
const char * | name | |||
) |
int my_atoi | ( | const char * | str | ) |
Definition at line 110 of file util.c.
00111 { 00112 00113 /* Returns the integer represented by the first part of the character * 00114 * string. */ 00115 00116 if(str[0] < '0' || str[0] > '9') 00117 { 00118 if(!(str[0] == '-' && str[1] >= '0' && str[1] <= '9')) 00119 { 00120 printf(ERRTAG "expected number instead of '%s'.\n", str); 00121 exit(1); 00122 } 00123 } 00124 return (atoi(str)); 00125 }
void* my_calloc | ( | size_t | nelem, | |
size_t | size | |||
) |
void* my_chunk_malloc | ( | size_t | size, | |
struct s_linked_vptr ** | chunk_ptr_head, | |||
int * | mem_avail_ptr, | |||
char ** | next_mem_loc_ptr | |||
) |
Definition at line 189 of file util.c.
00193 { 00194 00195 /* This routine should be used for allocating fairly small data * 00196 * structures where memory-efficiency is crucial. This routine allocates * 00197 * large "chunks" of data, and parcels them out as requested. Whenever * 00198 * it mallocs a new chunk it adds it to the linked list pointed to by * 00199 * chunk_ptr_head. This list can be used to free the chunked memory. * 00200 * If chunk_ptr_head is NULL, no list of chunked memory blocks will be kept * 00201 * -- this is useful for data structures that you never intend to free as * 00202 * it means you don't have to keep track of the linked lists. * 00203 * Information about the currently open "chunk" must be stored by the * 00204 * user program. mem_avail_ptr points to an int storing how many bytes are * 00205 * left in the current chunk, while next_mem_loc_ptr is the address of a * 00206 * pointer to the next free bytes in the chunk. To start a new chunk, * 00207 * simply set *mem_avail_ptr = 0. Each independent set of data structures * 00208 * should use a new chunk. */ 00209 00210 /* To make sure the memory passed back is properly aligned, I must * 00211 * only send back chunks in multiples of the worst-case alignment * 00212 * restriction of the machine. On most machines this should be * 00213 * a long, but on 64-bit machines it might be a long long or a * 00214 * double. Change the typedef below if this is the case. */ 00215 00216 typedef long Align; 00217 00218 #define CHUNK_SIZE 32768 00219 #define FRAGMENT_THRESHOLD 100 00220 00221 char *tmp_ptr; 00222 int aligned_size; 00223 00224 assert(*mem_avail_ptr >= 0); 00225 00226 if((size_t) (*mem_avail_ptr) < size) 00227 { /* Need to malloc more memory. */ 00228 if(size > CHUNK_SIZE) 00229 { /* Too big, use standard routine. */ 00230 tmp_ptr = my_malloc(size); 00231 00232 /* When debugging, uncomment the code below to see if memory allocation size */ 00233 /* makes sense */ 00234 /*#ifdef DEBUG 00235 printf("NB: my_chunk_malloc got a request for %d bytes.\n", 00236 size); 00237 printf("You should consider using my_malloc for such big requests.\n"); 00238 #endif */ 00239 00240 if(chunk_ptr_head != NULL) 00241 *chunk_ptr_head = 00242 insert_in_vptr_list(*chunk_ptr_head, tmp_ptr); 00243 return (tmp_ptr); 00244 } 00245 00246 if(*mem_avail_ptr < FRAGMENT_THRESHOLD) 00247 { /* Only a small scrap left. */ 00248 *next_mem_loc_ptr = my_malloc(CHUNK_SIZE); 00249 *mem_avail_ptr = CHUNK_SIZE; 00250 if(chunk_ptr_head != NULL) 00251 *chunk_ptr_head = insert_in_vptr_list(*chunk_ptr_head, 00252 *next_mem_loc_ptr); 00253 } 00254 00255 /* Execute else clause only when the chunk we want is pretty big, * 00256 * and would leave too big an unused fragment. Then we use malloc * 00257 * to allocate normally. */ 00258 00259 else 00260 { 00261 tmp_ptr = my_malloc(size); 00262 if(chunk_ptr_head != NULL) 00263 *chunk_ptr_head = 00264 insert_in_vptr_list(*chunk_ptr_head, tmp_ptr); 00265 return (tmp_ptr); 00266 } 00267 } 00268 00269 /* Find the smallest distance to advance the memory pointer and keep * 00270 * everything aligned. */ 00271 00272 if(size % sizeof(Align) == 0) 00273 { 00274 aligned_size = size; 00275 } 00276 else 00277 { 00278 aligned_size = size + sizeof(Align) - size % sizeof(Align); 00279 } 00280 00281 tmp_ptr = *next_mem_loc_ptr; 00282 *next_mem_loc_ptr += aligned_size; 00283 *mem_avail_ptr -= aligned_size; 00284 return (tmp_ptr); 00285 }
char* my_fgets | ( | char * | buf, | |
int | max_size, | |||
FILE * | fp | |||
) |
Definition at line 442 of file util.c.
00445 { 00446 /* Get an input line, update the line number and cut off * 00447 * any comment part. A \ at the end of a line with no * 00448 * comment part (#) means continue. */ 00449 00450 char *val; 00451 int i; 00452 00453 cont = 0; 00454 val = fgets(buf, max_size, fp); 00455 linenum++; 00456 if(val == NULL) 00457 return (val); 00458 00459 /* Check that line completely fit into buffer. (Flags long line * 00460 * truncation). */ 00461 00462 for(i = 0; i < max_size; i++) 00463 { 00464 if(buf[i] == '\n') 00465 break; 00466 if(buf[i] == '\0') 00467 { 00468 printf 00469 ("Error on line %d -- line is too long for input buffer.\n", 00470 linenum); 00471 printf("All lines must be at most %d characters long.\n", 00472 BUFSIZE - 2); 00473 printf 00474 ("The problem could also be caused by a missing newline.\n"); 00475 exit(1); 00476 } 00477 } 00478 00479 00480 for(i = 0; i < max_size && buf[i] != '\0'; i++) 00481 { 00482 if(buf[i] == '#') 00483 { 00484 buf[i] = '\0'; 00485 break; 00486 } 00487 } 00488 00489 if(i < 2) 00490 return (val); 00491 if(buf[i - 1] == '\n' && buf[i - 2] == '\\') 00492 { 00493 cont = 1; /* line continued */ 00494 buf[i - 2] = '\n'; /* May need this for tokens */ 00495 buf[i - 1] = '\0'; 00496 } 00497 return (val); 00498 }
FILE* my_fopen | ( | const char * | fname, | |
const char * | flag | |||
) |
Definition at line 60 of file util.c.
00062 { 00063 FILE *fp; 00064 int Len; 00065 char *new_fname = NULL; 00066 00067 /* Appends a prefix string for output files */ 00068 if(OutFilePrefix) 00069 { 00070 if(strchr(flag, 'w')) 00071 { 00072 Len = 1; /* NULL char */ 00073 Len += strlen(OutFilePrefix); 00074 Len += strlen(fname); 00075 new_fname = (char *)my_malloc(Len * sizeof(char)); 00076 strcpy(new_fname, OutFilePrefix); 00077 strcat(new_fname, fname); 00078 fname = new_fname; 00079 } 00080 } 00081 00082 if(NULL == (fp = fopen(fname, flag))) 00083 { 00084 printf("Error opening file %s for %s access.\n", fname, flag); 00085 exit(1); 00086 } 00087 00088 if(new_fname) 00089 free(new_fname); 00090 00091 return (fp); 00092 }
float my_frand | ( | void | ) |
Definition at line 876 of file util.c.
00877 { 00878 00879 /* Creates a random float between 0 and 1. i.e. [0..1). */ 00880 00881 float fval; 00882 int ival; 00883 00884 current_random = current_random * IA + IC; /* Use overflow to wrap */ 00885 ival = current_random & (IM - 1); /* Modulus */ 00886 fval = (float)ival / (float)IM; 00887 00888 #ifdef CHECK_RAND 00889 if((fval < 0) || (fval > 1.)) 00890 { 00891 printf("Bad value in my_frand, fval = %g\n", fval); 00892 exit(1); 00893 } 00894 #endif 00895 00896 return (fval); 00897 }
int my_irand | ( | int | imax | ) |
Definition at line 850 of file util.c.
00851 { 00852 00853 /* Creates a random integer between 0 and imax, inclusive. i.e. [0..imax] */ 00854 00855 int ival; 00856 00857 /* current_random = (current_random * IA + IC) % IM; */ 00858 current_random = current_random * IA + IC; /* Use overflow to wrap */ 00859 ival = current_random & (IM - 1); /* Modulus */ 00860 ival = (int)((float)ival * (float)(imax + 0.999) / (float)IM); 00861 00862 #ifdef CHECK_RAND 00863 if((ival < 0) || (ival > imax)) 00864 { 00865 printf("Bad value in my_irand, imax = %d ival = %d\n", imax, 00866 ival); 00867 exit(1); 00868 } 00869 #endif 00870 00871 return (ival); 00872 }
void* my_malloc | ( | size_t | size | ) |
void* my_realloc | ( | void * | ptr, | |
size_t | size | |||
) |
Definition at line 162 of file util.c.
00164 { 00165 00166 void *ret; 00167 00168 if(size <= 0) 00169 { 00170 printf("reallocating of size <= 0.\n"); 00171 } 00172 00173 ret = realloc(ptr, size); 00174 if(NULL == ret) 00175 { 00176 printf(ERRTAG "Unable to realloc memory. Aborting. " 00177 "ptr=%p, Size=%d.\n", ptr, size); 00178 if(ptr == NULL) 00179 { 00180 printf(ERRTAG "my_realloc: ptr == NULL. Aborting.\n"); 00181 } 00182 exit(1); 00183 } 00184 return (ret); 00185 }
void my_srandom | ( | int | seed | ) |
Definition at line 843 of file util.c.
00844 { 00845 current_random = (unsigned int)seed; 00846 }
char* my_strdup | ( | const char * | str | ) |
Definition at line 96 of file util.c.
00097 { 00098 int Len; 00099 char *Dst; 00100 00101 Len = 1 + strlen(str); 00102 Dst = (char *)my_malloc(Len * sizeof(char)); 00103 memcpy(Dst, str, Len); 00104 00105 return Dst; 00106 }
char* my_strncpy | ( | char * | dest, | |
const char * | src, | |||
size_t | size | |||
) |
Definition at line 38 of file util.c.
00041 { 00042 /* Find string's length */ 00043 size_t len = strlen(src); 00044 00045 /* Cap length at (num - 1) to leave room for \0 */ 00046 if(size <= len) 00047 len = (size - 1); 00048 00049 /* Copy as much of string as we can fit */ 00050 memcpy(dest, src, len); 00051 00052 /* explicit null termination */ 00053 dest[len] = '\0'; 00054 00055 return dest; 00056 }
char* my_strtok | ( | char * | ptr, | |
char * | tokens, | |||
FILE * | fp, | |||
char * | buf | |||
) |
Definition at line 502 of file util.c.
00506 { 00507 00508 /* Get next token, and wrap to next line if \ at end of line. * 00509 * There is a bit of a "gotcha" in strtok. It does not make a * 00510 * copy of the character array which you pass by pointer on the * 00511 * first call. Thus, you must make sure this array exists for * 00512 * as long as you are using strtok to parse that line. Don't * 00513 * use local buffers in a bunch of subroutines calling each * 00514 * other; the local buffer may be overwritten when the stack is * 00515 * restored after return from the subroutine. */ 00516 00517 char *val; 00518 00519 val = strtok(ptr, tokens); 00520 for(;;) 00521 { 00522 if(val != NULL || cont == 0) 00523 return (val); 00524 00525 /* return unless we have a null value and a continuation line */ 00526 if(my_fgets(buf, BUFSIZE, fp) == NULL) 00527 return (NULL); 00528 00529 val = strtok(buf, tokens); 00530 } 00531 }
void print_int_matrix3 | ( | int *** | vptr, | |
int | nrmin, | |||
int | nrmax, | |||
int | ncmin, | |||
int | ncmax, | |||
int | ndmin, | |||
int | ndmax, | |||
char * | file | |||
) |
Definition at line 744 of file util.c.
00752 { 00753 FILE *outfile; 00754 int i, j, k; 00755 00756 outfile = my_fopen(file, "w"); 00757 00758 for(k = nrmin; k <= nrmax; ++k) 00759 { 00760 fprintf(outfile, "Plane %d\n", k); 00761 for(j = ncmin; j <= ncmax; ++j) 00762 { 00763 for(i = ndmin; i <= ndmax; ++i) 00764 { 00765 fprintf(outfile, "%d ", vptr[k][j][i]); 00766 } 00767 fprintf(outfile, "\n"); 00768 } 00769 fprintf(outfile, "\n"); 00770 } 00771 00772 fclose(outfile); 00773 }