Initialise elsewhere -- as simple as possible...

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
 
P

Puppet_Sock

Tomás wrote:
[snippers]
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).

Why can't it be local to main? Really delegate.

Here come some fragments.

class SourceData
{
public:
// yada yada ctor, dtor, copy ctor, op=, etc.
// the ctor includes closing the file in a tidy manner
int StartFile(std::string name);
private:
ifstream& source;
};

Then add the stuff you need to do to this file. Keep in mind the
notion
that one day you may not be fetching a disc file, but from a URL, or
from some network virtual connection, or SQL calls from a database,
or any old thing you could think of.

Here comes some more fragments. I'm typing this in at the terminal,
so things may not be syntactically correct.

int main(int argc, char* argv[])
{
try
{
CmdLineInfo const cmdline_info(argc,argv);

// someplace here you need to define input and
// output mode information, maybe it's done in CmdLineInfo
// maybe that's the 8to32 things, and maybe inMode and outMode
// are really calls to cmndline_info

SourceData mySource(cmdline_info.GetSourceName(), inMode);
DestData myDest(cmdline_info.GetDestName(), outMode);

// the ctor is where you put all the work of getting the file ready
// to start reading/writing

// Notice I said source and dest, not file names

//Now proceed to manipulate the data
mySource.PrepareToReadLines();
myDest.PrepareToWriteLines();
while(mySource.Done() != TRUE)
{
myDest.WriteOneLine(mySource.ReadOneLine());
}
}
catch(Excp_EndProg e)
{
return e.code;
}
}

Really delegate. Don't just use a class as a thing to hold stuff in.
Make it do the work.

Or maybe you need to read in the entire input file into some data
structure.

class Splorp8to32
{
// yada yada ctor dtor op= etc.
};

(Because you mentioned 8to32 and like in your post.)

Then you can do this in a few ways. But what you
want is for the internal data structure not to know about the
details of the file it was stored in. So you want to be able
to build up a Splorp from some kind of thing you can pull
from the file. Line by line, or groupings, or whatever.
So you put a "get one line" func in your input file class.
And you put an "accept one line to add to the Splorp"
func in your Splorp class.

Splorp32 mySplorp;
mySplorp.PrepareToAcceptLines();
while(mySource.Done() != TRUE)
{
mySplorp.AcceptOneLine(mySource.ReadOneLine());
}

And for writing it out, you just go in the other direction.
You put a "proffer up one line worth of the Splorp" in
your Splorp class, and a "write one line" func in
your output class.

Or you divide it into groups or icons or sprites or
whatever works for Splorps. And your input and output
file classes know how to get one of these chunks into
or out of the storage file. For example, it might be database
calls, and your input/output classes might have to do some
SQL calls to get/retrieve one chunk. But Splorp never gets
to see any of that.

And so on. Really delegate.

Then you can get as magnificently fancy, or as simple, as
your app requires. For example: You could buffer the read/write
ops, so the next, previous, and current line are available. Or
you could log all reads/writes and allow backup/restore.
Or you could add security to every step. Or CRC checks in
case the data line is suspect. And all that junk is going to
be hidden.
Socks
 
D

Daniel T.

"Tomás said:
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?

I would open the files in main, then pass them to other function(s) that
do the parsing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top