📄 util.c
字号:
char *val; int i; cont = 0; val = fgets(buf, max_size, fp); linenum++; if(val == NULL) return (val);/* Check that line completely fit into buffer. (Flags long line * * truncation). */ for(i = 0; i < max_size; i++) { if(buf[i] == '\n') break; if(buf[i] == '\0') { printf ("Error on line %d -- line is too long for input buffer.\n", linenum); printf("All lines must be at most %d characters long.\n", BUFSIZE - 2); printf ("The problem could also be caused by a missing newline.\n"); exit(1); } } for(i = 0; i < max_size && buf[i] != '\0'; i++) { if(buf[i] == '#') { buf[i] = '\0'; break; } } if(i < 2) return (val); if(buf[i - 1] == '\n' && buf[i - 2] == '\\') { cont = 1; /* line continued */ buf[i - 2] = '\n'; /* May need this for tokens */ buf[i - 1] = '\0'; } return (val);}char *my_strtok(char *ptr, char *tokens, FILE * fp, char *buf){/* Get next token, and wrap to next line if \ at end of line. * * There is a bit of a "gotcha" in strtok. It does not make a * * copy of the character array which you pass by pointer on the * * first call. Thus, you must make sure this array exists for * * as long as you are using strtok to parse that line. Don't * * use local buffers in a bunch of subroutines calling each * * other; the local buffer may be overwritten when the stack is * * restored after return from the subroutine. */ char *val; val = strtok(ptr, tokens); for(;;) { if(val != NULL || cont == 0) return (val); /* return unless we have a null value and a continuation line */ if(my_fgets(buf, BUFSIZE, fp) == NULL) return (NULL); val = strtok(buf, tokens); }}voidfree_ivec_vector(struct s_ivec *ivec_vector, int nrmin, int nrmax){/* Frees a 1D array of integer vectors. */ int i; for(i = nrmin; i <= nrmax; i++) if(ivec_vector[i].nelem != 0) free(ivec_vector[i].list); free(ivec_vector + nrmin);}voidfree_ivec_matrix(struct s_ivec **ivec_matrix, int nrmin, int nrmax, int ncmin, int ncmax){/* Frees a 2D matrix of integer vectors (ivecs). */ int i, j; for(i = nrmin; i <= nrmax; i++) { for(j = ncmin; j <= ncmax; j++) { if(ivec_matrix[i][j].nelem != 0) { free(ivec_matrix[i][j].list); } } } free_matrix(ivec_matrix, nrmin, nrmax, ncmin, sizeof(struct s_ivec));}voidfree_ivec_matrix3(struct s_ivec ***ivec_matrix3, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax){/* Frees a 3D matrix of integer vectors (ivecs). */ int i, j, k; for(i = nrmin; i <= nrmax; i++) { for(j = ncmin; j <= ncmax; j++) { for(k = ndmin; k <= ndmax; k++) { if(ivec_matrix3[i][j][k].nelem != 0) { free(ivec_matrix3[i][j][k].list); } } } } free_matrix3(ivec_matrix3, nrmin, nrmax, ncmin, ncmax, ndmin, sizeof(struct s_ivec));}void **alloc_matrix(int nrmin, int nrmax, int ncmin, int ncmax, size_t elsize){/* allocates an generic matrix with nrmax-nrmin + 1 rows and ncmax - * * ncmin + 1 columns, with each element of size elsize. i.e. * * returns a pointer to a storage block [nrmin..nrmax][ncmin..ncmax].* * Simply cast the returned array pointer to the proper type. */ int i; char **cptr; cptr = (char **)my_malloc((nrmax - nrmin + 1) * sizeof(char *)); cptr -= nrmin; for(i = nrmin; i <= nrmax; i++) { cptr[i] = (char *)my_malloc((ncmax - ncmin + 1) * elsize); cptr[i] -= ncmin * elsize / sizeof(char); /* sizeof(char) = 1 */ } return ((void **)cptr);}/* NB: need to make the pointer type void * instead of void ** to allow * * any pointer to be passed in without a cast. */voidfree_matrix(void *vptr, int nrmin, int nrmax, int ncmin, size_t elsize){ int i; char **cptr; cptr = (char **)vptr; for(i = nrmin; i <= nrmax; i++) free(cptr[i] + ncmin * elsize / sizeof(char)); free(cptr + nrmin);}void ***alloc_matrix3(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, size_t elsize){/* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - * * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each * * element of size elsize. i.e. returns a pointer to a storage block * * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the * * returned array pointer to the proper type. */ int i, j; char ***cptr; cptr = (char ***)my_malloc((nrmax - nrmin + 1) * sizeof(char **)); cptr -= nrmin; for(i = nrmin; i <= nrmax; i++) { cptr[i] = (char **)my_malloc((ncmax - ncmin + 1) * sizeof(char *)); cptr[i] -= ncmin; for(j = ncmin; j <= ncmax; j++) { cptr[i][j] = (char *)my_malloc((ndmax - ndmin + 1) * elsize); cptr[i][j] -= ndmin * elsize / sizeof(char); /* sizeof(char) = 1) */ } } return ((void ***)cptr);}void ****alloc_matrix4(int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, int nemax, size_t elsize){/* allocates a 3D generic matrix with nrmax-nrmin + 1 rows, ncmax - * * ncmin + 1 columns, and a depth of ndmax-ndmin + 1, with each * * element of size elsize. i.e. returns a pointer to a storage block * * [nrmin..nrmax][ncmin..ncmax][ndmin..ndmax]. Simply cast the * * returned array pointer to the proper type. */ int i, j, k; char ****cptr; cptr = (char ****)my_malloc((nrmax - nrmin + 1) * sizeof(char ***)); cptr -= nrmin; for(i = nrmin; i <= nrmax; i++) { cptr[i] = (char ***)my_malloc((ncmax - ncmin + 1) * sizeof(char **)); cptr[i] -= ncmin; for(j = ncmin; j <= ncmax; j++) { cptr[i][j] = (char **)my_malloc((ndmax - ndmin + 1) * sizeof(char *)); cptr[i][j] -= ndmin; for(k = ndmin; k <= ndmax; k++) { cptr[i][j][k] = (char *)my_malloc((nemax - nemin + 1) * elsize); cptr[i][j][k] -= nemin * elsize / sizeof(char); /* sizeof(char) = 1) */ } } } return ((void ****)cptr);}voidprint_int_matrix3(int ***vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, char *file){ FILE *outfile; int i, j, k; outfile = my_fopen(file, "w"); for(k = nrmin; k <= nrmax; ++k) { fprintf(outfile, "Plane %d\n", k); for(j = ncmin; j <= ncmax; ++j) { for(i = ndmin; i <= ndmax; ++i) { fprintf(outfile, "%d ", vptr[k][j][i]); } fprintf(outfile, "\n"); } fprintf(outfile, "\n"); } fclose(outfile);}voidfree_matrix3(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, size_t elsize){ int i, j; char ***cptr; cptr = (char ***)vptr; for(i = nrmin; i <= nrmax; i++) { for(j = ncmin; j <= ncmax; j++) free(cptr[i][j] + ndmin * elsize / sizeof(char)); free(cptr[i] + ncmin); } free(cptr + nrmin);}voidfree_matrix4(void *vptr, int nrmin, int nrmax, int ncmin, int ncmax, int ndmin, int ndmax, int nemin, size_t elsize){ int i, j, k; char ****cptr; cptr = (char ****)vptr; for(i = nrmin; i <= nrmax; i++) { for(j = ncmin; j <= ncmax; j++) { for(k = ndmin; k <= ndmax; k++) free(cptr[i][j][k] + nemin * elsize / sizeof(char)); free(cptr[i][j] + ndmin * elsize / sizeof(char)); } free(cptr[i] + ncmin); } free(cptr + nrmin);}/* Portable random number generator defined below. Taken from ANSI C by * * K & R. Not a great generator, but fast, and good enough for my needs. */#define IA 1103515245u#define IC 12345u#define IM 2147483648u#define CHECK_RANDstatic unsigned int current_random = 0;voidmy_srandom(int seed){ current_random = (unsigned int)seed;}intmy_irand(int imax){/* Creates a random integer between 0 and imax, inclusive. i.e. [0..imax] */ int ival;/* current_random = (current_random * IA + IC) % IM; */ current_random = current_random * IA + IC; /* Use overflow to wrap */ ival = current_random & (IM - 1); /* Modulus */ ival = (int)((float)ival * (float)(imax + 0.999) / (float)IM);#ifdef CHECK_RAND if((ival < 0) || (ival > imax)) { printf("Bad value in my_irand, imax = %d ival = %d\n", imax, ival); exit(1); }#endif return (ival);}floatmy_frand(void){/* Creates a random float between 0 and 1. i.e. [0..1). */ float fval; int ival; current_random = current_random * IA + IC; /* Use overflow to wrap */ ival = current_random & (IM - 1); /* Modulus */ fval = (float)ival / (float)IM;#ifdef CHECK_RAND if((fval < 0) || (fval > 1.)) { printf("Bad value in my_frand, fval = %g\n", fval); exit(1); }#endif return (fval);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -