Exemple #1
0
static void
pass2_txt(struct text *txt) {
	struct position *pos;
	size_t old_nl_cnt;

	if (!txt->tx_pos)	/* no need to scan the file */
		return;

	/* Open_Text() initializes lex_nl_cnt and lex_tk_cnt */
	if (!Open_Text(Second_Pass, txt)) {
		fprintf(stderr, ">>>> File %s disappeared <<<<\n",
			txt->tx_fname
		);
		return;
	}

	/* Sort the positions so they can be matched to the file; the linked
	   list of struct positions snakes through the struct positions in the
	   struct chunks in the struct runs.
	*/
#ifdef	DB_POS
	db_print_pos_list("before sorting", txt);
#endif	/* DB_POS */

	sort_pos_list(&txt->tx_pos);

#ifdef	DB_POS
	db_print_pos_list("after sorting", txt);
#endif	/* DB_POS */

#ifdef	DB_NL_BUFF
	db_print_nl_buff(txt->tx_nl_start, txt->tx_nl_limit);
#endif	/* DB_NL_BUFF */

#ifdef	DB_POS
	fprintf(Debug_File, "\n**** DB_PRINT_SCAN of %s ****\n", txt->tx_fname);
#endif	/* DB_POS */

	old_nl_cnt = 1;
	pos = txt->tx_pos;
	while (pos) {
		/* we scan the pos list and the file in parallel */

		/* find the corresponding line */
		while (pos->ps_tk_cnt >= lex_tk_cnt) {
			/* pos does not refer to this line, try the next */

			/* shift the administration */
			old_nl_cnt = lex_nl_cnt;
			/* and get the next eol position */
			if (!Next_Text_EOL_Obtained()) {
				/* reached end of file without obtaining EOL */
				if (!txt->tx_EOL_terminated) {
					/* that's OK then */
				} else {
					fprintf(stderr,
						">>>> File %s modified <<<<\n",
						txt->tx_fname
					);
				}
				break;
			}
#ifdef	DB_POS
			db_print_lex(txt->tx_fname);
#endif	/* DB_POS */
		}

		/* fill in the pos */
		switch (pos->ps_type) {
		case 0:	/* first token of run */
			pos->ps_nl_cnt = old_nl_cnt;
			break;
		case 1:	/* last token of run */
			pos->ps_nl_cnt = lex_nl_cnt;
			break;
		}
		/* and get the next pos */
		pos = pos->ps_next;
	}

#ifdef	DB_POS
	db_print_pos_list("after scanning", txt);
#endif	/* DB_POS */

	/* Flush the flex buffers; it's easier than using YY_BUFFER_STATE. */
	while (Next_Text_EOL_Obtained());

	Close_Text(Second_Pass, txt);
}
Exemple #2
0
void
Read_Input_Files(int argc, const char *argv[], int round) {
	int n;

	Init_Text(argc);
	Init_Token_Array();

	/* Assume all texts to be new */
	Number_Of_New_Texts = Number_Of_Texts;

	/* Read the files */
	for (n = 0; n < Number_Of_Texts; n++) {
		const char *fname = argv[n];
		struct text *txt = &Text[n];

		if (round == 1 && !is_set_option('T')) {
			fprintf(Output_File, "File %s: ", fname);
		}

		txt->tx_fname = fname;
		txt->tx_pos = 0;
		txt->tx_start =
		txt->tx_limit = Text_Length();
		if (is_new_old_separator(fname)) {
			if (round == 1 && !is_set_option('T')) {
				fprintf(Output_File, "separator\n");
			}
			Number_Of_New_Texts = n;
		}
		else {
			if (!Open_Text(First, txt)) {
				if (round == 1 && !is_set_option('T')) {
					fprintf(Output_File,
						">>>> cannot open <<<< ");
				}
				/*	the file has still been opened
					with a null file for uniformity
				*/
			}
			while (Next_Text_Token_Obtained(First)) {
				if (!Token_EQ(lex_token, End_Of_Line)) {
					Store_Token(lex_token);
				}
			}
			Close_Text(First, txt);
			txt->tx_limit = Text_Length();

			/* report */
			if (round == 1 && !is_set_option('T')) {
				fprint_count(Output_File,
					     txt->tx_limit - txt->tx_start,
					     token_name
				);
				fprintf(Output_File, ", ");
				fprint_count(Output_File, lex_nl_cnt-1, "line");
				if (lex_non_ascii_cnt) {
					fprintf(Output_File, ", ");
					fprint_count(Output_File,
						     lex_non_ascii_cnt,
						     "non-ASCII character"
					);
				}
				fprintf(Output_File, "\n");
			}

#ifdef	DB_TEXT
			db_print_text(txt);
#endif	/* DB_TEXT */
		}
		fflush(Output_File);
	}

	/* report total */
	if (round == 1 && !is_set_option('T')) {
		fprintf(Output_File, "Total: ");
		fprint_count(Output_File, Text_Length() - 1, token_name);
		fprintf(Output_File, "\n\n");
		fflush(Output_File);
	}
}
Exemple #3
0
void
Read_Input_Files(int argc, const char *argv[]) {
	int n;

	Init_Text(argc);
	Init_Token_Array();

	/* Initially assume all texts to be new */
	Number_of_New_Texts = Number_of_Texts;

	/* Read the files */
	for (n = 0; n < Number_of_Texts; n++) {
		const char *fname = argv[n];
		struct text *txt = &Text[n];

		if (!is_set_option('T')) {
			fprintf(Output_File, "File %s: ", fname);
		}

		txt->tx_fname = fname;
		txt->tx_pos = 0;
		txt->tx_start = Token_Array_Length();
		txt->tx_limit = Token_Array_Length();

		if (is_new_old_separator(fname)) {
			if (!is_set_option('T')) {
				fprintf(Output_File, "new/old separator\n");
			}
			if (Number_of_New_Texts == Number_of_Texts) {
				Number_of_New_Texts = n;
			} else fatal("more than one new/old separator");
		}
		else {
			int file_opened = 0;
			if (Open_Text(First_Pass, txt)) {
				file_opened = 1;
			} else {
				/* print a warning */
				if (is_set_option('T')) {
					/* the file name has not yet been
					   printed; print it now
					*/
					fprintf(Output_File, "File %s: ",
						fname);
				}
				fprintf(Output_File,
					">>>> cannot open <<<<\n");
				/*	the file has still been opened
					with a null file for uniformity
				*/
			}
			while (Next_Text_Token_Obtained()) {
				if (!Token_EQ(lex_token, End_Of_Line)) {
					Store_Token(lex_token);
				}
			}
			Close_Text(First_Pass, txt);
			txt->tx_limit = Token_Array_Length();
			txt->tx_EOL_terminated =
				Token_EQ(lex_token, End_Of_Line);

			/* report */
			if (file_opened && !is_set_option('T')) {
				fprint_count(Output_File,
					     txt->tx_limit - txt->tx_start,
					     Token_Name
				);
				fprintf(Output_File, ", ");
				fprint_count(Output_File,
					lex_nl_cnt - 1 +
					     (!txt->tx_EOL_terminated ? 1 : 0),
					"line"
				);
				if (!txt->tx_EOL_terminated) {
					fprintf(Output_File,
						" (not NL-terminated)");
				}
				if (lex_non_ascii_cnt) {
					fprintf(Output_File, ", ");
					fprint_count(Output_File,
						     lex_non_ascii_cnt,
						     "non-ASCII character"
					);
				}
				fprintf(Output_File, "\n");
			}

#ifdef	DB_TEXT
			db_print_text(txt);
#endif	/* DB_TEXT */
		}
		fflush(Output_File);
	}

	/* report total */
	int sep_present = (Number_of_Texts != Number_of_New_Texts);
	fprintf(Output_File, "Total input: ");
	fprint_count(Output_File,
		     (!sep_present ? Number_of_Texts : Number_of_Texts - 1),
		     "file"
	);
	fprintf(Output_File, " (%d new, %d old), ",
		Number_of_New_Texts,
		(!sep_present ? 0 :  Number_of_Texts - Number_of_New_Texts - 1)
	);
	fprint_count(Output_File, Token_Array_Length() - 1, Token_Name);
	fprintf(Output_File, "\n\n");
	fflush(Output_File);
}