T
Tomás
Up until lately I've been writing all mad kinds of code for accomplishing
things, but lately I've decided to lean more toward the whole readability,
etc. side of things.
I have a command line application, which I intend to be used like so:
unicon.exe -8to32 source.txt target.txt
There are three command line arguments. The first is something like:
-8to16 or -16to8 or -8to32
The next two arguments are filenames.
I want my "main" function to be as simple as possible; I achieve this by
delegating the work to other functions. Here's how it looks so far:
struct CmdLineInfo
{
class Bad_Option {};
enum ConversionType { EightToSixteen, EightToThirtytwo,
SixteenToEight, SixteenToThirtytwo,
ThirtytwoToEight, ThirtytwoToSixteen };
ConversionType conversion;
const char* source;
const char* target;
CmdLineInfo( ConversionType const a, const char* const b, const char*
const c )
: conversion(a), source(b), target(c) {}
};
CmdLineInfo GetCmdLineInfo( int argc, char * argv[] );
//This function analyses the arguments and returns a structure
//by value.
//If the first argument is invalid, it throws an exception
//of type, "Excp_EndProg".
int main(int argc, char* argv[])
{
try
{
CmdLineInfo const &cmdline_info = GetCmdLineInfo(argc, argv);
}
catch(Excp_EndProg e)
{
return e.code;
}
}
Next thing I want to do is open the two files, but again, I want to delegate
this work. I shall be using objects of the type "fstream". Here's how I
would like my code to look:
int main(int argc, char* argv[])
{
try
{
CmdLineInfo const &cmdline_info = GetCmdLineInfo(argc, argv);
ifstream& source = OpenSourceFile( argv[1] );
ofstream& target = OpenTargetFile( argv[2] );
//Now proceed to manipulate the files
}
catch(Excp_EndProg e)
{
return e.code;
}
}
If either "OpenSourceFile" or "OpenTargetFile" fail to open the files
successfully, I'll make them throw an exception.
The problem with the code above is that, for the "OpenSourceFile" function
to be able to initialise the "ifstream" object, the "ifstream" object
_can't_ be a local variable of main (which is what I want).
I know I can dynamically allocate it in "OpenSourceFile", and even use an
auto_pointer, or I could even use "placement new", but is there not a nice
clean way to do it.
Here's the general idea:
class SomeType
{
public:
int k;
SomeType( int const a ) : k(a) {}
};
SomeType& OtherFunction()
{
//In here we initialise the object
}
int main()
{
SomeType k; //But somehow don't initalise it.
k = Otherfunction();
}
See what I'm trying to do?
-Tomás
things, but lately I've decided to lean more toward the whole readability,
etc. side of things.
I have a command line application, which I intend to be used like so:
unicon.exe -8to32 source.txt target.txt
There are three command line arguments. The first is something like:
-8to16 or -16to8 or -8to32
The next two arguments are filenames.
I want my "main" function to be as simple as possible; I achieve this by
delegating the work to other functions. Here's how it looks so far:
struct CmdLineInfo
{
class Bad_Option {};
enum ConversionType { EightToSixteen, EightToThirtytwo,
SixteenToEight, SixteenToThirtytwo,
ThirtytwoToEight, ThirtytwoToSixteen };
ConversionType conversion;
const char* source;
const char* target;
CmdLineInfo( ConversionType const a, const char* const b, const char*
const c )
: conversion(a), source(b), target(c) {}
};
CmdLineInfo GetCmdLineInfo( int argc, char * argv[] );
//This function analyses the arguments and returns a structure
//by value.
//If the first argument is invalid, it throws an exception
//of type, "Excp_EndProg".
int main(int argc, char* argv[])
{
try
{
CmdLineInfo const &cmdline_info = GetCmdLineInfo(argc, argv);
}
catch(Excp_EndProg e)
{
return e.code;
}
}
Next thing I want to do is open the two files, but again, I want to delegate
this work. I shall be using objects of the type "fstream". Here's how I
would like my code to look:
int main(int argc, char* argv[])
{
try
{
CmdLineInfo const &cmdline_info = GetCmdLineInfo(argc, argv);
ifstream& source = OpenSourceFile( argv[1] );
ofstream& target = OpenTargetFile( argv[2] );
//Now proceed to manipulate the files
}
catch(Excp_EndProg e)
{
return e.code;
}
}
If either "OpenSourceFile" or "OpenTargetFile" fail to open the files
successfully, I'll make them throw an exception.
The problem with the code above is that, for the "OpenSourceFile" function
to be able to initialise the "ifstream" object, the "ifstream" object
_can't_ be a local variable of main (which is what I want).
I know I can dynamically allocate it in "OpenSourceFile", and even use an
auto_pointer, or I could even use "placement new", but is there not a nice
clean way to do it.
Here's the general idea:
class SomeType
{
public:
int k;
SomeType( int const a ) : k(a) {}
};
SomeType& OtherFunction()
{
//In here we initialise the object
}
int main()
{
SomeType k; //But somehow don't initalise it.
k = Otherfunction();
}
See what I'm trying to do?
-Tomás