|
ex6-2.cpp - Example 6-2 Function overloading - copyFile |
// File: ex6-2.cpp - overloaded functions
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#ifdef __GNUG__
#include <unistd.h>
#else
#include <io.h> // for access() function
#endif
// Overloaded function prototypes
void copyFile(const char* fn1, const char* fn2);
void copyFile(const char* fn1, const char* fn2, const char* option);
void copyFile(const char* fn1, const char* fn2, const char* fromto, int lineno);
void copyFile(const char* fn1, const char* fn2, const char* from, int line1,
const char* to, int line2);
void errorMessage(char* msg);
void errorMessage(char* msg, FILE* fp1);
void errorMessage(char* msg, FILE* fp1, FILE* fp2);
void displayCmdSyntax() {
fprintf(stderr,"Usage:\n");
fprintf(stderr,"\tcopyFile <file1> <file2>\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -append\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -upper\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -lower\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -noreplace\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -from <line#> -to <line#>\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -from <line#>\n");
fprintf(stderr,"\tcopyFile <file1> <file2> -to <line#>\n");
}
int main(int argc, char* argv[])
{
if (argc<3 || *argv[1] == '?') {
displayCmdSyntax();
exit(0);
}
switch (argc) {
case 3: copyFile(argv[1],argv[2]);
break;
case 4: copyFile(argv[1],argv[2],argv[3]);
break;
case 5: copyFile(argv[1],argv[2],argv[3],atoi(argv[4]));
break;
case 7: copyFile(argv[1],argv[2],argv[3],atoi(argv[4]),
argv[5],atoi(argv[6]));
break;
default: errorMessage("Error: Invalid syntax\n");
}
printf("Ok\n");
return 0;
}
// copies file fn1 to file fn2
void copyFile(const char* fn1, const char* fn2)
{
char buffer[256];
FILE * fp1, * fp2;
fp1 = fopen(fn1,"r");
if (!fp1) errorMessage("Unable to open input file\n");
fp2 = fopen(fn2,"w");
if (!fp2) errorMessage("Unable to open output file\n",fp1);
while (fgets(buffer,sizeof(buffer),fp1)) fputs(buffer,fp2);
fclose(fp1);
fclose(fp2);
}
// copies file fn1 to file fn2 using append, upper, lower or noreplace
void copyFile(const char* fn1, const char* fn2, const char* option) {
char buffer[256];
FILE * fp1, * fp2;
int status; // of fputs()
// check for valid option
if (strcmp(option,"-upper")==0 ||
strcmp(option,"-lower")==0 ||
strcmp(option,"-append")==0 ||
strcmp(option,"-noreplace")==0) /* keep going */ ;
else errorMessage("Invalid option");
fp1 = fopen(fn1,"r");
if (!fp1) errorMessage("Unable to open input file\n");
// append option
if (strcmp(option,"-append")==0) {
fp2 = fopen(fn2,"a");
if (!fp2) errorMessage("Unable to open output file\n",fp1);
else {
while (fgets(buffer,sizeof(buffer),fp1)) {
status = fputs(buffer,fp2);
if (status == EOF)
errorMessage("Unable to write to output file\n",fp1,fp2);
}
}
}
// upper & lower case options
if (strcmp(option,"-upper")==0 || strcmp(option,"-lower")==0) {
char ch;
fp2 = fopen(fn2,"w");
if (!fp2) errorMessage("Unable to open output file\n",fp1);
else {
while ((ch = getc(fp1)) != EOF) {
if (strcmp(option,"-upper")==0) putc(toupper(ch),fp2);
else putc(tolower(ch),fp2);
}
}
}
// noreplace option
if (strcmp(option,"-noreplace")==0) {
// check for the existance of file fn2
// Note: The access() function is non-ANSI, but available on Borland/UNIX compilers
// MS compilers use _access
if (access(fn2,0)==0) errorMessage("Noreplace error for output file\n",fp1);
else copyFile(fn1,fn2);
}
fclose(fp1);
fclose(fp2);
}
// copy part of fn1 to fn2 (from linto or to lineno)
void copyFile(const char* fn1, const char* fn2, const char* fromto, int lineno)
{
char buffer[256], *fgets_status, err_msg[80];
FILE * fp1, * fp2;
int i;
fp1 = fopen(fn1,"r");
if (!fp1) errorMessage("Unable to open input file\n");
fp2 = fopen(fn2,"w");
if (!fp2) errorMessage("Unable to open output file\n");
// copy "from" lineno to the end of file
if (strcmp(fromto,"-from")==0) {
// read records up to "from"
for (i = 1; i<lineno;i++) {
fgets_status = fgets(buffer,sizeof(buffer),fp1);
if (!fgets_status) { // make sure the records are read up to "from"
sprintf(err_msg,"Unable to read past record %d\n",i);
errorMessage(err_msg,fp1,fp2);
}
}
// read the rest of the file & write it out
while (fgets(buffer,sizeof(buffer),fp1)) fputs(buffer,fp2);
}
else if (strcmp(fromto,"-to")==0) {
// read records up to "to" & write them out
for (i = 0; i<lineno;i++) {
fgets_status = fgets(buffer,sizeof(buffer),fp1);
if (!fgets_status) { // make sure the records are read up to "from"
sprintf(err_msg,"Unable to read past record %d\n",i);
errorMessage(err_msg,fp1,fp2);
}
fputs(buffer,fp2);
}
}
else errorMessage("Invalid syntax",fp1,fp2);
fclose(fp1);
fclose(fp2);
}
void copyFile(const char* fn1, const char* fn2, const char* from, int line1,
const char* to, int line2)
{
char buffer[256], err_msg[80], *fgets_status;
FILE * fp1, * fp2;
int i;
// check the syntax
if (strcmp(from,"-from")||strcmp(to,"-to"))
errorMessage("Invalid from/to syntax\n");
if (line1>line2) errorMessage("Invalid 'from' > 'to'\n");
fp1 = fopen(fn1,"r");
if (!fp1) errorMessage("Unable to open input file\n");
fp2 = fopen(fn2,"w");
if (!fp2) errorMessage("Unable to open output file\n");
// read records up to "from" line1
for (i = 1; i<line1;i++) {
fgets_status = fgets(buffer,sizeof(buffer),fp1);
if (!fgets_status) { // make sure the records are read up to "from"
sprintf(err_msg,"Unable to read past record %d\n",i);
errorMessage(err_msg,fp1,fp2);
}
}
for (i = line1; i<=line2;i++) {
fgets_status = fgets(buffer,sizeof(buffer),fp1);
if (!fgets_status) { // make sure the records are read up to "to"
sprintf(err_msg,"Unable to read past record %d\n",i);
errorMessage(err_msg,fp1,fp2);
}
fputs(buffer,fp2);
}
fclose(fp1);
fclose(fp2);
}
void errorMessage(char* msg) {
fprintf(stderr,msg);
exit (-1);
}
void errorMessage(char* msg, FILE* fp1) {
fprintf(stderr,msg);
fclose(fp1);
exit (-1);
}
void errorMessage(char* msg, FILE* fp1, FILE* fp2) {
fprintf(stderr,msg);
fclose(fp1);
fclose(fp2);
exit (-1);
}
CIS27: Programming in C++ Instructor: Joe Bentley