C++/copying a function
Expert: Ralph McArdell - 7/23/2005
Questionplzz help.
how a can i write a program to copy function (as a whole) from one c++ file to another c++ file ,(appending to its end)
the inputs of the program shold be source file name,function name and destination file name.
its not homework.its part of a code automation programme
AnswerThis seems easy in concept, but is in fact quite when you sit down and consider all the details.
The concept is easy:
- Open the source source-code file for reading
- Read it to locate the function in question.
- Open the target source-code file for write-appending
- Copy the whole function text from the source source-code file to the end of the target source-code file.
- Close both files.
Job done.
The complexity comes in reading and processing the contents of the source source-code file. First let us look at where you find functions. In C++ functions can be stand alone (in the global namespace), part of a class (which includes structs) or part of a namespace. They may be templated - as function templates, member functions of class templates, or member function templates of a class or a class template. Functions may be overloaded - that is more than one function of the same name may exist in a given namespace (which in this context includes classes). Functions can have many declarations (e.g. function prototypes), but only a single definition. Often these occur in a separate header file. Do you also need to copy these to some other header file?
So you may have to specify the fully qualified name of the function (e.g. OuterNamespace::InnerNamespace::OuterClass::InnerClass::FunctionName), the parameters for the function: (e.g. int n, std::string const & text ) and for member functions whether the function is const. The return type may also be useful.
Now the definition of the above function could look be written in the code in several ways for example:
void OuterNamespace::InnerNamespace:: OuterClass::InnerClass::FunctionName
(int n, std::string const & text )
{
// ...
}
or like so:
namespace OuterNamespace
{
// ...
namespace InnerNamespace
{
// ...
void OuterClass::InnerClass::FunctionName
(int n, std::string const & text )
{
// ...
}
// ...
}
// ...
}
Or maybe :
namespace OuterNamespace
{
// ...
namespace InnerNamespace
{
// ...
class OuterClass
{
// ...
class InnerClass
{
// ...
void FunctionName
(int n, std::string const & text )
{
// ...
}
// ...
};
// ...
};
// ...
}
// ...
}
In which the // ... show places other source-text could be. You will notice that the definition can be contained within open namespace or classes definitions (in the latter case the member function would be inline) or can be specified by qualification - in which case a declaration would be expected in the open namespace or class:
namespace OuterNamespace
{
// ...
namespace InnerNamespace
{
// ...
class OuterClass
{
// ...
class InnerClass
{
// ...
void FunctionName
(int n, std::string const & text );
// ...
};
// ...
};
// ...
}
// ...
}
Then there are cases where for example, the types are specified slightly differently - std::string const & might be specified as const std::string & for example, and if the author decided to be using std::string then maybe the std qualification might be missing.
You will have to decide how to handle such cases.
I have purposely chosen some awkward cases to make you think on what you are doing - as you give no hint as to the scope of the problem.
If, for example, your intention is to add boilerplate code to classes from template code files then this complexity can be ignored by copying whole template code files - probably performing some form of substitution for things like the correct class name in the process. The mechanism is dumb - it just copies whatever is in the file and substitutes special tags in the template code for the particular circumstances in effect at the time (such as the target's class name). Note the use of template here is the more generic meaning and not the C++ meaning of class or function templates.
However, if it is your wish to locate a function in an arbitrary piece of source code and copy it to another source code file then the problem becomes much more complicated as I tried to indicate ablove!
Your mention of code automation makes me think you require something much more like the former than the latter. So consider if your code automation functionality could be performed using files containing code snippets with special tags in where values need to be substituted. A side effect of such an approach is that is not restricted to just functions or just C++. For example you might create a file containing a boilerplate event handler for a framework:
// OnLeftMouseButtonClick
// Mouse left button click handler for class $$CLASS$$
void $$CLASS$$::OnLeftMouseButtonClick
{
$$$BASECLASS$$::OnLeftMouseButtonClick()
// TODO: Add your left mouse button click handling code here
}
Your application opens this file, reads it character by character and writing most of them straight to the target source code file (which you have opened for write-appending). However, on detecting the string $$ the application interprets the characters up to the closing $$ sequence as a keyword that need to be substituted - in which case the substituted text is written out instead.
Now all you need to do is to associate the various functions that ca be automatically added to your code with the files that contain the code template(s) and determine the values that vary from case to case - define then as substitutable values - with a keyword that is placed in code templates and a value for each case that is available for substitution.
Note that I suspect you will find you will not always wish to place the code at the end of a file. In some cases - member function declarations for example - the text will have to go in some sensible place within the source file.
I suspect you can refine this technique some what but is should be good enough to get you going - if this is the sort of thing you require. Sorry if I guessed wrong but you did not give me a lot to go on.