📄 usage.c
字号:
input_file = NULL;
/* A. */
strcpy(input_file_name, input_name);
if(has_dot_in_name(input_file_name)){
printf("\nTry to open %s\n", input_file_name);
if((input_file =
fopen(input_file_name, "rt")) == NULL){
printf("\nusage: Cannot open input file %s\n",
input_file_name);
} /* ends if input_file */
} /* ends if has_dot_in_name */
/* B. */
else{
/* C. */
/* Try .c */
strcpy(temp_string, input_file_name);
strcat(temp_string, ".c");
printf("\nTry to open %s\n", temp_string);
if((input_file =
fopen(temp_string, "rt")) == NULL){
printf("\nusage: Could not open file %s",
temp_string);
} /* ends if input_file .c */
else
havent_found_file = 0;
/* D. */
/* Try .cpp */
if(havent_found_file){
strcpy(temp_string, input_file_name);
strcat(temp_string, ".cpp");
printf("\nTry to open %s\n", temp_string);
if((input_file =
fopen(temp_string, "rt")) == NULL){
printf("\nusage: Could not open file %s",
temp_string);
} /* ends if input_file .cpp */
else
havent_found_file = 0;
} /* ends if havent_found_file */
/* E. */
/* Try .f or anything other extension */
if(havent_found_file){
strcpy(temp_string, input_file_name);
strcat(temp_string, ".f");
printf("\nTry to open %s\n", temp_string);
if((input_file =
fopen(temp_string, "rt")) == NULL){
printf("\nusage: Could not open file %s",
temp_string);
} /* ends if input_file .f */
else
havent_found_file = 0;
} /* ends if havent_found_file */
} /* ends else does not have dot in name */
return(input_file);
} /* ends find_and_open_input_file */
/**************************************************/
/*PAGE paginate_file function
paginate_file(...
This function breaks the input file into pages
and inserts spaces at the left edge of the page.
The output is sent to an output text file.
A. Read the date and time and put them into
the time string.
B. Print the header for the first page.
C. Read all the lines in the input file.
D. Look for the start marker.
E. Once you find the start marker, read the
information.
F. Send the line to the output file as long
as it is not the end marker.
G. If you send a full screen of text to the
output file, make a new page in the output
file.
H. When you hit the end marker, quit
sending the text to the output file.
I. At the end of the input file, fill the last
page of the output file with blank lines.
*/
void paginate_file(FILE *input_file,
FILE *output_file,
char *name_of_input_file,
char start[],
char end[])
{
char *f_result,
string[LENGTH],
time_string[LENGTH];
int i,
first_page = 1,
line_counter = 0,
page_counter = 1,
reading_usage,
toc_line_counter = 0;
struct tm *time_of_day;
time_t seconds;
/* A. */
time(&seconds);
time_of_day = localtime(&seconds);
sprintf(time_string, "%d-%d-%d\0",
time_of_day->tm_mon+1,
time_of_day->tm_mday,
time_of_day->tm_year);
/* B. */
write_3_line_header(output_file, name_of_input_file,
time_string, &page_counter);
line_counter = line_counter + 3;
blank_line(output_file);
++line_counter;
/* C. */
while(fgets(string, LENGTH+1, input_file)){
/* D. */
if(strncmp(string, start, 3) == 0){
reading_usage = 1;
/* E. */
while(reading_usage){
f_result = fgets(string,
LENGTH+1,
input_file);
if(f_result == NULL)
reading_usage = 0;
/* F. */
if((reading_usage == 1) &&
(strncmp(string, end, 6) != 0)){
pad_string(string, TAB_SPACE);
fputs(string, output_file);
++line_counter;
/* G. */
if(line_counter >= LPP-FOOT_SPACE)
make_new_page(input_file,
output_file,
name_of_input_file,
time_string,
&page_counter,
&line_counter);
} /* ends if */
/* H. */
if(strncmp(string, end, 6) == 0)
reading_usage = 0;
} /* ends while reading_usage */
} /* ends if found USAGE */
} /* end of while fgets */
/* I. */
for(i=line_counter; i<=LPP; i++)
blank_line(output_file);
} /* end of paginate_file function */
/**************************************************/
/*PAGE pad_string function
pad_string(...
This function inserts spaces into the beginning
(left side) of a string.
A. Declare variables.
B. Fill a temporary string with num_blanks spaces.
C. Concatenate input string onto temporary string.
D. Copy temporary string back to input string.
*/
void pad_string(char string[],
int num_blanks)
{
/* A. */
int count;
char temp[LENGTH];
/* B. */
for(count = 0; count < num_blanks; count++)
temp[count] = ' ';
temp[count] = '\0';
/* C. */
strcat(temp, string);
/* D. */
strcpy(string, temp);
} /* end of pad_string function */
/**************************************************/
/*PAGE write_3_line_header function
write_3_line_header(...
This function writes a three line header to the
top of a new page. The header contains the file
name, date, and page number.
A. Declare variables.
B. Fill strings with blanks.
C. Fill strings with information and send to
the output file.
*/
void write_3_line_header(FILE *file,
char *file_name,
char *time_string,
int *page_number)
{
/* A. */
char b1[LENGTH],
b2[LENGTH],
b3[LENGTH],
string[LENGTH];
int i;
/* B. */
for(i=0; i<LENGTH; i++){
b1[i] = ' ';
b2[i] = ' ';
b3[i] = ' ';
string[i] = ' ';
} /* end of loop over i */
b1[LENGTH-15] = '\0';
b2[LENGTH-15] = '\0';
b3[LENGTH-15] = '\0';
/* C. */
sprintf(string, "%s\n", file_name);
strcat(b1, string);
fputs(b1, file);
sprintf(string, "%s\n", time_string);
strcat(b2, string);
fputs(b2, file);
sprintf(string, "Page %d\n", *page_number);
strcat(b3, string);
fputs(b3, file);
} /* end of write_3_line_header function */
/**************************************************/
/*PAGE blank_line function
blank_line(...
This function sends a blank line to a file.
*/
void blank_line(FILE *file)
{
fputs(" \n", file);
} /* end of blank_line function */
/**************************************************/
/*PAGE make_new_page function
make_new_page(...
This function fills the remainder of a page with
blank lines and prints a header at the top of the
next page.
A. Declare variables.
B. Loop until the page runs out of lines.
C. If you reach the end of a page, then
D. Print FOOTER blank lines, jump to the
next page, and print a page header.
*/
void make_new_page(FILE *input_file,
FILE *output_file,
char *name_of_input_file,
char *time_string,
int *page_counter,
int *line_counter)
{
/* A. */
int i, not_finished = 1;
/* B. */
while(not_finished){
/* C. */
if(*line_counter >= LPP - FOOT_SPACE){
/* D. */
not_finished = 0;
for(i=0; i<FOOTER; i++)
blank_line(output_file);
*line_counter = 0;
*page_counter = *page_counter + 1;
blank_line(output_file);
*line_counter = *line_counter + 1;
write_3_line_header(output_file,
name_of_input_file,
time_string,
page_counter);
*line_counter = *line_counter + 3;
blank_line(output_file);
*line_counter = *line_counter + 1;
} /* end of if line counter >= LPP - 10 */
fputs(" \n", output_file);
*line_counter = *line_counter + 1;
} /* end of while not_finished */
} /* end of make_new_page function */
/**************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -