/* %Src=tf.c %Dir=\rltraynh/c/stuff/ %Date=92/10/15-2:28pm */ /* Line-oriented file reads -- You MUST include tfile.h */ #ifndef TF_C #define TF_C 1 /*-----------------------*/ void Init_tf () /*-----------------------*/ { Src_fname = Alloc_256 ("Init_tfile", "Src_fname"); Src_tmp_fname = Alloc_256 ("Init_tfile", "Src_tmp_fname"); Src_eof = 1; Src_recnum = 0; Src_fcode = READ_ACCESS_UNDEFINED; Src_byte_count = 0; Out_fname = Alloc_256 ("Init_tfile", "Out_fname"); Out_tmp_fname = Alloc_256 ("Init_tfile", "Out_tmp_fname"); Out_eof = 1; Out_recnum = 0; Out_fcode = READ_ACCESS_UNDEFINED; Out_byte_count = 0; JunkF = (char *) malloc (STRING_SIZE+1); *JunkF = '\0'; Junkf = (char *) malloc (STRING_SIZE+1); *Junkf = '\0'; Fjunk = (char *) malloc (STRING_SIZE+1); *Fjunk = '\0'; Junko = (char *) malloc (STRING_SIZE+1); *Junko = '\0'; IO_err_msg = (char *) malloc (STRING_SIZE+1); *IO_err_msg = '\0'; } /*------------------------------------*/ short File_exists (char *Qfname) /*------------------------------------*/ { short Bad = 0; /** BEGIN **/ *IO_err_msg = '\0'; TF_exists_file = fopen (Qfname, "rb"); if (TF_exists_file == ((FILE *) NULL)) Bad = 1; el { if (feof(TF_exists_file)) Bad = 1; } if (Bad == 1) { sprintf (IO_err_msg, "*** Warn: File does NOT exist: %s", Qfname); return 0; } fclose (TF_exists_file); sprintf (IO_err_msg, "--- File exists: %s", Qfname); return 1; } /*------------------------------------------*/ void Fclose (char *File_ref_symbol) /*-----------------------------------------*/ { /** BEGIN **/ strcpy (Junk9, File_ref_symbol); Upper (Junk9); if (strncmp (Junk9, "SRC", 3) == 0) { if (Src_fcode == READ_ONLY_ACCESS) fclose (Src_file); el { sprintf (IO_err_msg, "*** WARN: Could NOT do logical close of SRC-file: %s", Src_fname); printf ("%s\n", IO_err_msg); PAUSE; } Src_fcode = READ_ACCESS_UNDEFINED; Src_eof = 1; Src_recnum = 0; return; } if (strncmp (Junk9, "OUT", 3) == 0) { if (Out_fcode == READ_WRITE_ACCESS) fclose (Out_file); el { sprintf (IO_err_msg, "*** WARN: Could NOT do logical close of OUT-file: %s", Out_fname); printf ("%s\n", IO_err_msg); PAUSE; } *Out_fname = '\0'; /* But leave Out_tmp_fname ALONE */ Out_fcode = READ_ACCESS_UNDEFINED; Out_eof = 1; Out_recnum = 0; return; } sprintf (IO_err_msg, "Unknown XCI-lang ref : %s", File_ref_symbol); printf ("*** ERR: %s\n", IO_err_msg); printf ("*** WARN: File NOT closed. No action taken! Problems MAY occur\n"); PAUSE; return; } /*---------------------------------------------------*/ long Count_src_file_non_blank_lines (char *Fname) /*---------------------------------------------------*/ { short Nz_count, Done; /** BEGIN **/ *IO_err_msg = '\0'; if (*Fname == '\0') { sprintf (IO_err_msg, "*** No File to count!"); return -1; } if (Open_src_file (Fname) != READ_ONLY_ACCESS) { sprintf (IO_err_msg, "*** No file: %15s", Fname); return -1; } Nz_count = 0; Done = 0; while (Done == 0) { GG_len = Pull_src_line (GG_xs); if (GG_len > 4) Nz_count++; if (GG_len < 0) Done = 1; /* Is LEN == -1 ? ==> EOF */ } sprintf (IO_err_msg,"--- %22s %7d NZ-lines", Fname, Nz_count); Fclose ("SRC"); /** FCLOSE **/ return Nz_count; } /*-------------------------------------------------------*/ void Warn_user_null_found_embedded_in_text_file (void) /*-------------------------------------------------------*/ { sprintf (IO_err_msg, "!*** Warn -- Src_file '%s' had an embeded //0 !!\n", Src_fname); printf ("%s\n", IO_err_msg); PAUSE; } /*----------------------------------*/ short Open_src_file (char *Fname) /*----------------------------------*/ { *IO_err_msg = '\0'; Src_file = fopen (Fname, "rb"); if ((Src_file == (FILE *) NULL) || (feof(Src_file))) { sprintf (IO_err_msg, "*** Err -- Could not open Src_file: '%s'.", Fname); return READ_ACCESS_UNDEFINED; } strcpy (Src_fname, Fname); Src_recnum = 0; Src_eof = 0; Src_fcode = READ_ONLY_ACCESS; Src_byte_count = 0; sprintf (IO_err_msg, "!--- Src_file opened ok (R/B): %s\n", Src_fname); return Src_fcode; /** opened READ-ONLY OK, normal RETURN **/ } /*--------------------------------------*/ short Open_out_file (char *Fname) /*--------------------------------------*/ { *IO_err_msg = '\0'; Out_file = fopen (Fname, "wb"); if (Out_file == (FILE *) NULL) { sprintf (IO_err_msg, "*** Err -- Could not open Out_file: '%s' \n", Fname); return READ_ACCESS_UNDEFINED; } strcpy (Out_fname, Fname); Out_recnum = 0; Out_eof = 0; Out_fcode = READ_WRITE_ACCESS; sprintf (IO_err_msg, "!--- Out_file opened ok (W/B): %s\n", Out_fname); return Out_fcode; /** opened WRITE-ABLE OK, normal RETURN **/ } /*-------------------------------------------------------*/ short Open_src_and_out_file_in_place (char *Qfname) /*-------------------------------------------------------*/ /* Note: For unix, change the name to xxx.original */ /* For dos, copy it to .\back\ */ { Fclose ("SRC"); Fclose ("OUT"); if (File_exists (Qfname) != 1) { return -1; /* No such file */ } #ifdef __UNIX__ sprintf (Src_tmp_fname, "%s.original", Qfname); sprintf (GG_sys, "mv %s %s", Qfname, Src_tmp_fname); #else /*** DOS file system --------------------------------------------------*/ GG_sys_code = system ("mkdir back"); /* Note: retf is Discarded !! */ sprintf (Src_tmp_fname, ".\\back\\%s", Qfname); printf ("--- Attempting to create back-up file: %s\n", Src_tmp_fname); sprintf (GG_sys, "copy %s %s", Qfname, Src_tmp_fname); #endif GG_sys_code = system (GG_sys); printf ("--- Executing command: %s ...", GG_sys); if (GG_sys_code == 0) printf (" SUCCEEDED\n"); el printf (" FAILED, Code = %d\n", GG_sys_code); if (GG_sys_code != 0) { sprintf (IO_err_msg, "*** ERR: Rename-in-place failed. Command: %s", GG_sys); return -2; } if (Open_src_file (Src_tmp_fname) != READ_ONLY_ACCESS) { sprintf (IO_err_msg, "*** ERR: Could NOT open saved file: %s", Src_tmp_fname); printf ("%s\n", IO_err_msg); /*** Attempt_to_recover_src_file_in_place (Qfname); ***/ exit (-1010); } if (Open_out_file (Qfname) != READ_WRITE_ACCESS) { printf ("*** ERR: Could NOT open outfile: %s\n", Qfname); exit (-1011); } sprintf (IO_err_msg, "--- File %s -> %s (in-place)", Qfname, Src_tmp_fname); printf ("%s\n", IO_err_msg); return 1; /** Renamed, ok **/ } /*-------------------------------------------------------------------*/ short Open_src_via_argv_or_prompt (short Arg_num, char *Msg, short Argc, char *Argv[]) /*-------------------------------------------------------------------*/ /* This func will try and open argv[Arg_num], if it exists. If not */ /* Then the MSG string will be used to prompt user. */ /* NOTE: Calling this routine with Arg_num = 9999 always prompts */ { *IO_err_msg = '\0'; if (Argc <= Arg_num) { while (1==1) /* while forever */ { printf ("??? %s >", Msg); Input (JunkF); if (Quit (JunkF)) exit (909); if (Open_src_file (JunkF) == READ_ONLY_ACCESS) return READ_ONLY_ACCESS; /** Ok-RETURN **/ printf ("*** ERR: Open R/O failed for file: %s\n", JunkF); } /*forever */ } if (Open_src_file (Argv[Arg_num]) != READ_ONLY_ACCESS) return READ_ACCESS_UNDEFINED; /*** ERR-RETURN ***/ sprintf (IO_err_msg, "--- Srcfile opened for read: %s\n", Src_fname); printf ("%s\n", IO_err_msg); return READ_ONLY_ACCESS; /** Ok-RETURN **/ } /*-------------------------------------------------------------------*/ short Open_out_via_argv_or_prompt (int Arg_num, char *Msg, short Argc, char *Argv[]) /*-------------------------------------------------------------------*/ /* This func will try and open argv[Arg_num], if it exists. If not */ /* Then the MSG string will be used to prompt user. */ /* NOTE: Calling this routine with Arg_num = 9999 always prompts */ { *IO_err_msg = '\0'; if (Argc <= Arg_num) { while (1==1) /* while forever */ { printf ("??? %s >", Msg); Input (JunkF); if (Quit (JunkF)) exit (909); if (Open_out_file (JunkF) == READ_WRITE_ACCESS) return READ_WRITE_ACCESS; /** Ok-RETURN **/ printf ("*** ERR: Open R/W failed for file: %s\n", JunkF); } /*forever */ } if (Open_out_file (Argv[Arg_num]) != READ_WRITE_ACCESS) return READ_ACCESS_UNDEFINED; /*** ERR-RETURN ***/ sprintf (IO_err_msg, "--- Outfile opened for output: %s\n", Out_fname); printf ("%s\n", IO_err_msg); return READ_WRITE_ACCESS; /** Ok-RETURN **/ } /*------------------------------------------------*/ short Open_out_via_new_extn (char *Qfname, char *Extn) /*------------------------------------------------*/ /* Note: Uses the much-under-used Junko string */ { char *Xs, *Xd, *Xdot; /** BEGIN **/ *IO_err_msg = '\0'; /*---------- Locate where the very last "." is in the file name ------*/ Xs = Qfname; Xdot = NULL; /* In case there is NO dot in the the file name */ while (*Xs != '\0') { if (*Xs == '.') Xdot = Xs; /* HOLD it */ Xs++; } /* If there was in fact NO dot, then */ if (Xdot == NULL) Xdot = Xs; /* Mark it at the very end of the str */ /* ie, where the \0 is is the same as */ /* where the '.' would be anyway */ /*----------------- Copy up to that "." ----------------------------*/ Xs = Qfname; Xd = Junko; while (Xs != Xdot) *Xd++ = *Xs++; /*-------------------- Paste in a "." and then the new Extn ---------*/ *Xd++ = '.'; Xs = Extn; /* Get ready to append the Extn */ while (*Xs != '\0') *Xd++ = *Xs++; *Xd = '\0'; /*----------------------------------------------- Open Out_file ----------*/ if (Open_out_file (Junko) == READ_WRITE_ACCESS) return READ_WRITE_ACCESS; /*----------- COULD NOT OPEN -----------------------------------------------*/ sprintf (IO_err_msg, "*** ERR: Open of file %s with extn %s failed.", Qfname, Extn); printf ("%s\n", IO_err_msg); return READ_ACCESS_UNDEFINED; /*** ERR RETURN ***/ } /*---------------------------------------------------*/ long Copy_no_troff () /*---------------------------------------------------*/ /* Note: This function strips the irriating _^H */ /* sequence that occurs for TROFF / NROFF */ /* files on certain UNIX files (eg, man pages) */ { int Xchar, Next_char; long Lcount; /** BEGIN **/ Xchar = '\0'; Lcount = 0; Xchar = fgetc (Src_file); while (Xchar != EOF) { Next_char = fgetc (Src_file); if ((Xchar == '_') && (Next_char == '\010')) ; /* nop */ else if ((Xchar >= ' ') || (Xchar == '\r') || (Xchar == '\n')) fputc (Xchar, Out_file); if (Xchar == '\n') Lcount++; Xchar = Next_char; } return Lcount; } /*---------------------------------------------------*/ long Copy_and_strip_control_chars_completely () /*---------------------------------------------------*/ { int Xchar; long Lcount; /** BEGIN **/ Xchar = '\0'; Lcount = 0; while (Xchar != EOF) { Xchar = fgetc (Src_file); if (Xchar != EOF) { if ((Xchar >= ' ') || (Xchar == '\r') || (Xchar == '\n')) fputc (Xchar, Out_file); if (Xchar == '\n') Lcount++; } } return Lcount; } /*----------------------------------------*/ long Copy_ascii_format_src_to_out () /*----------------------------------------*/ { int Xchar; long Char_count = 0; Xchar = '\0'; while (Xchar != EOF) { Xchar = fgetc (Src_file); if (Xchar != EOF) { Char_count++; if (Xchar < ' ') fprintf (Out_file, "\\0%o", Xchar); else fputc (Xchar, Out_file); #ifdef __TURBOC__ if (Xchar == '\n') fprintf (Out_file,"\r\n"); PAUSE; #else if (Xchar == '\n') putc ('\n', Out_file); PAUSE; #endif } } return Char_count; } /*----------------------------------*/ long Copy_src_to_out () /*----------------------------------*/ { int Xchar; long Xcount; Xchar = '\0'; Xcount = 0; while (Xchar != EOF) { Xchar = fgetc (Src_file); if (Xchar != EOF) { fputc (Xchar, Out_file); Xcount++; } } return Xcount; } /*----------------------------------*/ void Show_file (FILE *File) /*----------------------------------*/ { int Xchar; Xchar = '\0'; while (Xchar != EOF) { Xchar = fgetc (File); if (Xchar != EOF) { fputc (Xchar, stdout); } } return; } /* tfile.c */ /*----------------------------------*/ short Pull_src_line (char *Xs) /*----------------------------------*/ /* Xs is the var that you must have ALREADY allocated, the text is */ /* returned in it. */ /* */ /* Note: If the number of chars exceeds STRING_SIZE, */ /* then IO_err_msg_STR_FLAG is set to 1 */ /* */ /* We return the char count as int (0 indicates a NULL line !!) */ /* -1 indicates EOF */ { char *Xd; short Xlen, Max_len; /* Current len (retf) and MAX str len of Xd */ char Xchar; int Xint; /* 16-bit WORD */ short Continue; /* BOOLEAN */ /** BEGIN **/ *IO_err_msg = '\0'; Xd = Xs; *Xd = '\0'; /* Default is no string at all */ Continue = 1; GG_string_size_violation_raised = 0; /* Reset the flag */ Max_len = STRING_SIZE - 3; /* Make sure we have enough room for \0 */ if ((Src_file == NULL) || (Src_eof != 0)) { *Xs = '\0'; Src_eof = 1; return -1; /* the file is closed !! */ } Xlen = 0; Xchar = '\0'; Xint = -1; Continue = 1; while ( (!feof (Src_file)) && (Xlen < STRING_LENGTH) && Continue ) { Xint = getc (Src_file); Xchar = Xint & 0x00ff; Src_byte_count++; if (Xint == EOF) { if (Xlen == 0) /* This occurs ONLY if PREVIOUS line read up to */ { /* and including the final \n and then EOF */ /* marker is immediately after that */ Fclose ("SRC"); return -1; /* the file is closed !! */ } Continue = 0; } if ( (Xchar == '\n') || (Xint == '\0') #ifdef __TURBOC__ || (Xint == '\032') /* Ctl-Z ==> EOF for DOS files */ #endif ) Continue = 0; else if ( (Xchar != '\n') && (Xchar != '\r') ) { if (Xchar == '\04') { fprintf (stderr, "*** Warn \\04 converted to \\03 at %d\n", Src_byte_count); Xchar = '\03'; } *Xd++ = Xchar; if (Xlen > Max_len) { GG_string_size_violation_raised = 1; Continue = 0; } else Xlen++; } } if ( feof (Src_file) || (Xint == EOF) #ifdef __TURBOC__ || (Xint == '\032') /* Ctl-Z ==> EOF for DOS files */ #endif ) { Fclose ("SRC"); sprintf (IO_err_msg, "--- Src file closed: %s\n", Src_fname); Src_eof = 1; #ifdef NOT_REDIRECTED PAUSE; #endif } Src_recnum++; *Xd = '\0'; /* NULL-terminate the string */ #ifdef TESTING printf ("<@%ld :: %s\n", Src_recnum, Xs); DEBUG_lines++; if (DEBUG_lines >= 19) { DEBUG_lines = 0; PAUSE; } #endif return Xlen; /* a-ok !! */ } /* tfile.c */ /*----------------------------------*/ void Show_line (char *Xs) /*----------------------------------*/ { char *P; P = Xs; printf (" >>"); while ((*P != '\0') && (*P != '\n')) { printf ("%c", *P); P++; }; printf ("<<\n"); } /*---------------------------------------------------*/ void Show_line_info (char *Xs, short Len, short Line_count) /*---------------------------------------------------*/ { char *P; P = Xs; printf (" %5d [%3d] >>", Line_count, Len); while ((*P != '\0') && (*P != '\n')) { printf ("%c",*P); P++; }; printf ("<<\n"); } #endif /* ifndef TF_C */