⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postmd.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 3 页
字号:
 * */    if ( rows == 0 )  {	pos = ftell(fp_in);	while ( fscanf(fp_in, "%s", buf) != EOF )	    count++;	rows = sqrt((double) count);	fseek(fp_in, pos, 0);    }	/* End if */    if ( columns <= 0 ) columns = rows;    if ( raster != NULL ) free(raster);    if ( (rptr = raster = malloc(columns)) == NULL )	error(FATAL, "no memory");    eptr = rptr + columns;    if ( rows <= 0 || columns <= 0 )	error(FATAL, "bad matrix dimensions");    if ( wlist[0] > wlist[2] || wlist[1] > wlist[3] )  {	wlist[0] = wlist[1] = 1;	wlist[2] = columns;	wlist[3] = rows;    }	/* End if */}   /* End of dimensions *//*****************************************************************************/buildilist(list)    char	*list;			/* use this as the interval list */{    static char	*templist = NULL;	/* a working copy of the list */    char	*ptr;			/* next number in *templist */    int		i;			/* loop index - for checking the list *//* * * Reads string *list and builds up the ilist[] that will be used in the next * matrix. Since strtok() modifies the string it's parsing we make a copy first. * The format of the interval list is described in detail in the comments at the * beginning of this program. Basically consists of a comma or space separated * list of floating point numbers that must be given in increasing numerical order. * The list determines how floating point numbers are mapped into integers in the * range 0 to 254. * */    if ( templist != NULL )		/* free the space used by the last list */	free(templist);    while ( isascii(*list) && isspace(*list) )	list++;    for ( ptr = list, regions = 3; *ptr != '\0'; ptr++ )  {	if ( *ptr == ',' || *ptr == '/' || isspace(*ptr) )	    regions += 2;	while ( isascii(*ptr) && isspace(*ptr) ) ptr++;    }	/* End for */    next = 0;    templist = savestring(list);    ptr = strtok(templist, ",/ \t\n");    while ( ptr != NULL )  {	ilist[next].count = 0;	ilist[next++].color = 254 * (regions - 1 - next) / (regions - 1);	ilist[next].val = atof(ptr);	ilist[next].count = 0;	ilist[next++].color = 254 * (regions - 1 - next) / (regions - 1);	ptr = strtok(NULL, ",/ \t\n");    }	/* End while */    ilist[next].count = 0;    ilist[next].color = 254 * (regions - 1 - next) / (regions - 1);    if ( next == 0 )			/* make sure we have a list */	error(FATAL, "missing interval list");    for ( i = 3; i < next; i += 2 )	/* that's in increasing numerical order */	if ( ilist[i].val <= ilist[i-2].val )	    error(FATAL, "bad interval list");}   /* End of buildilist *//*****************************************************************************/addcolormap(list)    char	*list;			/* use this color map */{    static char	*templist = NULL;	/* a working copy of the color list */    char	*ptr;			/* next color in *templist */    int		i = 0;			/* assigned to this region in ilist[] *//* * * Assigns the integers in *list to the color field for the regions defined in * ilist[]. Assumes ilist[] has already been setup. * */    if ( list != NULL )  {	if ( templist != NULL )	    free(templist);	templist = savestring(list);	ptr = strtok(templist, ",/ \t\n");	while ( ptr != NULL )  {	    ilist[i++].color = atoi(ptr) % 256;	    ptr = strtok(NULL, ",/ \t\n");	}   /* End while */    }	/* End if */}   /* End of addcolormap *//*****************************************************************************/setwindow(list)    char	*list;			/* corners of window into the matrix */{    static char	*templist = NULL;	/* a working copy of the window list */    char	*ptr;			/* next window coordinate in *templist */    int		i = 0;			/* assigned to this region in wlist[] *//* * * Sets up an optional window into the matrix. * */    wlist[0] = wlist[1] = 1;    wlist[2] = wlist[3] = 0;    if ( list != NULL )  {	if ( templist != NULL )	    free(templist);	templist = savestring(list);	ptr = strtok(templist, ",/ \t\n");	while ( ptr != NULL )  {	    wlist[i++] = atoi(ptr);	    ptr = strtok(NULL, ",/ \t\n");	}   /* End while */    }	/* End if */}   /* End of setwindow *//*****************************************************************************/inwindow(){    int		r;			/* row of the patcount element */    int		c;			/* column of the patcount element *//* * * Checks if the patcount element of the matrix is in the window. * */    r = (patcount/columns) + 1;    c = (patcount%columns) + 1;    return((c >= wlist[0]) && (r >= wlist[1]) && (c <= wlist[2]) && (r <= wlist[3]));}   /* End of inwindow *//*****************************************************************************/inrange(){/* * * Checks if the current row lies in the window. Used right before we output the * raster lines. * */    return(((patcount/columns) >= wlist[1]) && ((patcount/columns) <= wlist[3]));}   /* End of inrange *//*****************************************************************************/mapfloat(element)    double	element;		/* floating point matrix element */{    int		i;			/* loop index *//* * * Maps element into an integer in the range 0 to 255, and returns the result to * the caller. Mapping is done using the color map that was saved in ilist[]. Also * updates the count field for the region that contains element - not good! * */    for ( i = 1; i < next && ilist[i].val < element; i += 2 ) ;    if ( i > next || element < ilist[i].val )	i--;    ilist[i].count++;    return(ilist[i].color);}   /* End of mapfloat *//*****************************************************************************/putrow(){    char	*p1, *p2;		/* starting and ending columns */    int		n;			/* set to bytes per pattern */    int		i;			/* loop index *//* * * Takes the scanline that's been saved in *raster, encodes it according to the * value that's been assigned to bytespp, and writes the result to *fp_out. Each * line in the output bitmap is terminated by a 0 on a line by itself. * */    n = (bytespp <= 0) ? columns : bytespp;    for ( p1 = raster, p2 = raster + n; p1 < eptr; p1 = p2 )	if ( patncmp(p1, n) == TRUE )  {	    while ( patncmp(p2, n) == TRUE ) p2 += n;	    p2 += n;	    fprintf(fp_out, "%d ", n);	    for ( i = 0; i < n; i++, p1++ )		fprintf(fp_out, "%.2X", ((int) *p1) & 0377);	    fprintf(fp_out, " %d\n", (p2 - p1) / n);	} else {	    while ( p2 < eptr && patncmp(p2, n) == FALSE ) p2 += n;	    if ( p2 > eptr ) p2 = eptr;	    fprintf(fp_out, "%d ", p2 - p1);	    while ( p1 < p2 )		fprintf(fp_out, "%.2X", ((int) *p1++) & 0377);	    fprintf(fp_out, " 0\n");	}   /* End else */    fprintf(fp_out, "0\n");    rptr = raster;}   /* End of putrow *//*****************************************************************************/labelmatrix(){    int		total;			/* number of elements in the window */    int		i;			/* loop index *//* * * Responsible for generating the PostScript calls that label the matrix, generate * the legend, and print the matrix name. * */    fprintf(fp_out, "(%s) ((%d, %d) to (%d, %d)) labelmatrix\n", matrixname,			wlist[0], wlist[1], wlist[2], wlist[3]);    total = (wlist[2] - wlist[0] + 1) * (wlist[3] - wlist[1] + 1);    if ( nxtstat == OFF )	for ( i = 0; i < regions; i++ )	    ilist[i].count = 0;    for ( i = 1; i < next; i += 2 )	fprintf(fp_out, "(%g) ", ilist[i].val);    fprintf(fp_out, "%d ", (regions - 1) / 2);    for ( i = regions - 1; i >= 0; i-- )	fprintf(fp_out, "{(\\%.3o)} %d ", ilist[i].color, ilist[i].count);    fprintf(fp_out, "%d %d legend\n", total, regions);}   /* End of labelmatrix *//*****************************************************************************/patncmp(p1, n)    char	*p1;			/* first patterns starts here */    int		n;			/* and extends this many bytes */{    char	*p2;			/* address of the second pattern *//* * * Compares the two n byte patterns *p1 and *(p1+n). FALSE if returned is they're * different or extend past the end of the current raster line. * */    p2 = p1 + n;    for ( ; n > 0; n--, p1++, p2++ )	if ( p2 >= eptr || *p1 != *p2 )	    return(FALSE);    return(TRUE);}   /* End of patncmp *//*****************************************************************************/char *savestring(str)    char	*str;			/* save this string */{    char	*ptr = NULL;		/* at this address *//* * * Copies string *str to a permanent place and returns the address to the caller. * */    if ( str != NULL && *str != '\0' )  {	if ( (ptr = malloc(strlen(str) + 1)) == NULL )	    error(FATAL, "no memory available for string %s", str);	strcpy(ptr, str);    }	/* End if */    return(ptr);}   /* End of savestring *//*****************************************************************************/redirect(pg)    int		pg;			/* next page we're printing */{    static FILE	*fp_null = NULL;	/* if output is turned off *//* * * If we're not supposed to print page pg, fp_out will be directed to /dev/null, * otherwise output goes to stdout. * */    if ( pg >= 0 && in_olist(pg) == ON )	fp_out = stdout;    else if ( (fp_out = fp_null) == NULL )	fp_out = fp_null = fopen("/dev/null", "w");}   /* End of redirect *//*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -