#include #if XVT_OS == XVT_OS_SCOUNIX // EDIT Printer File // Utility for printer configuration files editing // by MB SEP 1993 #include #include #define __BA2300_CPP #include "ba2300.h" FILE* mirror; FILE* mirror2; FILE* current; FILE* printerlist; FILE* codes; FILE* names; FILE* defcodes; FILE* defnames; int selprinter=1; void build_list_file (FILE* inputfile, TFilename& outputfile) { FILE* printerlist; TString junk; int prog=0; printerlist=fopen(outputfile, "w"); fseek(inputfile, 0, SEEK_SET); while (fgets(__tmp_string, 256, inputfile)) { junk=__tmp_string; if (junk.find("[")!=-1 && junk.find("[Default]")==-1 && junk.find("[End of File]")==-1) { junk.ltrim(1); junk.rtrim(2); fprintf(printerlist, " ITEM \"%i|%s\"\n", prog++, (const char*)junk); } } fclose(printerlist); } bool seek_default_printer (FILE* inputfile, TString& newprinter, long* position, bool justafter) { TString junk; fseek(inputfile, 0, SEEK_SET); fgets(__tmp_string, 256, inputfile); junk=__tmp_string; if (junk.find("[")!=-1) { if (junk.find("Default")!=-1) { fgets(__tmp_string, 256, inputfile); junk=__tmp_string; junk.rtrim(1); } else { junk.ltrim(1); junk.rtrim(2); } newprinter=junk; return seek_printer(inputfile, newprinter, position, justafter); } return FALSE; } bool seek_printer (FILE* inputfile, TString& printer, long* position, bool justafter) { TString junk; long pos; char* noteof; fseek(inputfile, 0, SEEK_SET); do { pos=ftell(inputfile); noteof=fgets(__tmp_string, 256, inputfile); junk=__tmp_string; } while (!((junk.find(printer)!=-1 && junk.find("[")!=-1) || !noteof)); if (!noteof) return FALSE; else if (justafter) { *position=ftell(inputfile); return TRUE; } else { *position=pos; fseek(inputfile, pos, SEEK_SET); return TRUE; } } bool erase_file (TFilename& filename) { FILE* junkfile; if ((junkfile=fopen(filename, "r"))!=0) { fclose (junkfile); #if XVT_OS == XVT_OS_SCOUNIX sprintf (__tmp_string, "rm %s", (char*)filename); system (__tmp_string); #else remove(filename); #endif return TRUE; } else return FALSE; } bool is_present (FILE* inputfile, TString& printer) { TFilename mirror("mirror"); TString junk; FILE* printerlist; bool found=FALSE; build_list_file (inputfile, mirror); printerlist=fopen(mirror, "r"); while (fgets(__tmp_string, 256, printerlist) && !found) { junk=__tmp_string; if (junk.find(printer)!=-1) found=TRUE; } fclose(printerlist); return found; } bool change_default_printer (TFilename& inputfilename, FILE* inputfile, TString& newdefprinter) { FILE* mirror; TFilename mirrorname("mirror"); TString junk; if (is_present(inputfile, newdefprinter)) { mirror=fopen (mirrorname, "w"); fseek (inputfile, 0, SEEK_SET); fgets (__tmp_string, 256, inputfile); junk=__tmp_string; if (junk.find("[Default]")!=-1) { fputs(__tmp_string, mirror); fgets(__tmp_string, 256, inputfile); fgets(__tmp_string, 256, inputfile); } else fprintf(mirror, "[Default]\n"); fprintf(mirror, "%s\n", (const char*)newdefprinter); fputs(__tmp_string, mirror); while (fgets(__tmp_string, 256, inputfile)) fputs (__tmp_string, mirror); fclose (mirror); fclose (inputfile); erase_file (inputfilename); rename (mirrorname, inputfilename); inputfile = fopen(inputfilename, "r"); return TRUE; } else return FALSE; } bool build_code_files (FILE* input, TFilename& outcdfile, TFilename& outnmfile, TString& printer) { TString junk; TToken_string j2; long pos; int i; FILE *codes, *names; if (is_present(input, printer)) { codes=fopen(outcdfile, "w"); names=fopen(outnmfile, "w"); seek_printer (input, printer, &pos, TRUE); for (i=0; i<7; i++) fgets(__tmp_string, 256, input); fgets(__tmp_string, 256, input); junk=__tmp_string; i=0; while (junk.find("[")==-1) { junk.ltrim(14); j2=junk; j2.restart(); fprintf(names, " ITEM \"%i|%s\"\n", i++, (char*)j2.get()); fputs((char*)j2.get(), codes); fgets(__tmp_string, 256, input); junk=__tmp_string; } fclose (names); fclose (codes); return TRUE; } else return FALSE; } void build_list_box (FILE* names, TMask& mask, short field) { TString replace1, replace2, junk; replace1.cut(0); replace2.cut(0); while(fgets(__tmp_string, 256, names)) { if (__tmp_string[0]!='\n') { if (replace1.not_empty()) replace1 << '|'; if (replace2.not_empty()) replace2 << '|'; junk=__tmp_string; junk.ltrim(8); while (junk[0] && junk[0]!='|') { replace1 << junk[0]; junk.ltrim(1); } junk.ltrim(1); junk.rtrim(2); replace2 << junk; } } ((TList_field&)mask.field(field)).replace_items(replace1, replace2); } void place_codes_on_file (FILE* names, FILE* codes, FILE* output) { TString name; TString junk; while (fgets(__tmp_string, 256, names)) { name=__tmp_string; item2prname(name, junk); name.cut(0); name << "Code = " << junk << "|"; if (fgets(__tmp_string, 256, codes)) if (!(__tmp_string=="\n"||__tmp_string=="")) fprintf(output, "%s%s", (const char*)name, __tmp_string); } } void item2prname (TString& item, TString& name) { name=item; name.ltrim(8); name.rtrim(2); if (name.not_empty()) while (name[0]!='|') name.ltrim(1); name.ltrim(1); } bool seek_first_printer(FILE* input, TString& printer, long* pos, bool justafter) { return seek_printer_no(input, 1, printer, pos, justafter); } bool seek_printer_no(FILE* input, int number, TString& printer, long* pos, bool justafter) { FILE* listfile; TString junk; char* noteof=0; if (number==0) return FALSE; build_list_file(input, (TFilename)"mirror"); listfile=fopen("mirror", "r"); for (int i=0; i