源程序分页打印程序
程序编写完成之后,如要打印输出,对BASICA中,可用LLIST命令完成,对其它高级语言,则缺少特定打印
命令,不过还可以用DOS功能来实现,但对源程序的输出求较高时,则无通用的命令。为解决这处困难,我
用Turbo C 2.0编写了一个源程序打印程序ASL.C。
一、程序的使用方法
ASL可以按每页50行,每行80个字符,整齐地在打印机上输出源程序文本,在每页的页头还打出程序名称,
打印时的日期和时间,在每页的页尾打印出页号。该程序的命令行格式为:
ASL /H --- 获得ASL帮助文本
ASL 文件名 --- 分页打印源程序
ASL 文件名 /D --- 分页显示源程序
说明: “文件名”为DOS的有效文件名,必须写全名。如果要批量打印一批扩展名为.PAS的文件,则可以执
行:
For %a in (*.PAS) do ASL %a
二、源程序清单
/********************************************************/
/* 程序名称: ASL.C 1.3 */
/* 作 者: 董占山 */
/* 完成日期: 1990,1995 */
/* 用 途: 分页打印源程序 */
/* 编译方法: 用下列命令编译连接可以得到ASL.COM: */
/* tcc -mt asl */
/* tlink c:\tc\lib\c0t+asl,asl,,c:\tc\lib\cs\lib /t */
/********************************************************/
#include <stdio.h>
#include <dos.h>
#include <string.h>
unsigned int w1,pagenumber,counter,sp1,sp2;
char lin[256],flnm[81],sw[3];
FILE *f1,*f2;
struct date mydate;
/* 检查文件是否存在 */
int exist(flnm)
char *flnm;
{
unsigned char i;
i = 1;
if ((fopen(flnm,"r"))==NULL) i = 0;
return i;
}
/* 填充指定个数的特定字符的字符串 */
char *fillchartostr(len,ch)
unsigned int len;
char ch;
{
static char s[256];
strcpy(s,"");
memset(s,ch,len);
s[len]='\0';
return s;
}
/* 填充空格字符串 */
char *space(len)
unsigned int len;
{
return fillchartostr(len,' ');
}
/* 打印每页的页头 */
void PrintPageHead(f3)
FILE *f3;
{
struct time mytime;
char sp1s[80],sp2s[80],sp3s[80];
gettime(&mytime);
strcpy(sp1s,space(sp1));
strcpy(sp2s,space(sp2));
strcpy(sp3s,space(3));
fprintf(f3,"Advanced Print Program%s%s%s%04u-%02u-%02u%s%02u:%02u:%02u\n",
sp1s, flnm, sp2s, mydate.da_year, mydate.da_mon, mydate.da_day, sp3s,
mytime.ti_hour, mytime.ti_min, mytime.ti_sec);
fprintf(f3,"%s\n", fillchartostr(80 , '_') );
}
/* 打印每页的页尾 */
void PrintPageTail(f3)
FILE *f3;
{
unsigned int i;
pagenumber++;
fprintf( f3 ,"%s\n", fillchartostr(80 , '_') );
fprintf( f3 ,"%s", fillchartostr(36 , ' ') );
fprintf( f3 , "----%03u----\n",pagenumber);
for (i=1;i<=12;i++) fprintf(f3,"\n");
counter = 0;
}
/* 打印文本 */
void print(f3)
FILE *f3;
{
counter++;
fprintf(f3, "%s",lin);
if (counter == 50) {
PrintPageTail(f3);
PrintPageHead(f3);
}
}
/* 处理最后一页 */
void LastPageProcess(f3)
FILE *f3;
{
unsigned int i;
for(i=counter + 1;i<=50;i++) fprintf(f3,"\n");
PrintPageTail(f3);
}
/* 处理TAB符 */
void ProcessTab(lin)
char *lin;
{
char temp[256],temp1[256];
int i;
char ch;
strcpy(temp,"");
for (i=0;i<strlen(lin);i++) {
ch = lin[i];
if (ch == '\t') strcpy(temp1,fillchartostr(8,' '));
else {
temp1[0]=ch;
temp1[1]='\0';
}
strcat(temp,temp1);
}
strcpy(lin,temp);
}
/* 打印一行 */
void p(lin)
char *lin;
{
char *strp,lin1[256];
if (strlen(lin) > 81) {
strcpy(lin1,lin);
lin[80] = '\n';
print(f2);
strp = &lin1[80];
strcat(strcpy(lin,fillchartostr(4 , ' ')),strp);
p(lin);
}
else
print(f2);
}
/* 主程序 */
main(argv,argc)
unsigned int argv;
char *argc[];
{
printf("ASL Version 1.3 Copyright (c) 1990,95 Dong Zhanshan\n");
if (strcmp(strupr(argc[1]),"/H")==0) {
printf("Advanced Source Lister Usage:\n");
printf(" ASL /H --- ASL help messenge\n");
printf(" ASL filename --- Print the file to printer\n");
printf(" ASL filename /D --- Display the file to screen\n");
exit();
}
switch (argv) {
case 1 : printf("Filename : ");
scanf("%s",flnm);
strcpy(sw,"");
break;
case 2 : strcpy(flnm,argc[1]);
strcpy(sw,"");
break;
case 3 : strcpy(flnm,argc[1]);
strcpy(sw,argc[2]);
}
strupr(sw);
if (exist(flnm)==0) {
printf("File not found !\n");
exit();
}
sp1 = 18 - strlen(flnm) / 2;
sp2 = 36 - (sp1 + strlen(flnm));
strupr(flnm);
f1 = fopen(flnm,"r");
if (strcmp(sw,"/D")==0) f2 = fopen("con","w");
else f2 = fopen("LPT1","w");
pagenumber = 0;
counter = 0;
getdate(&mydate);
PrintPageHead(f2);
do {
fgets(lin,256,f1);
ProcessTab(lin);
p(lin);
} while (!feof(f1));
LastPageProcess(f2);
fclose(f1);
fclose(f2);
}
©董占山Zhanshan Dong
|