By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

module stub generator for testing?

P: n/a
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

thanks

Marco
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Dec 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.


Not aware of anything that does precisely this, but it'd be trivial to
write in either C or Perl. Even in less then 2 days.
--
:wq
^X^Cy^K^X^C^C^C^C
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #2

P: n/a
Marco wrote:

I am looking for an open-source or free tool that parses a C
header file (.h) for a module and creates a .c file with the
functions stubbed out ( ideally with dummy returns for
non-void). This will be used for testing purposes.

I have googled with little success. ( for a C++ class example
google "stubgen").


C++ is off-topic here. The tool required is your editor. Simply
eliminate the macros, typedefs, etc. and append a variant of:

{
return 0;
}

to each function header. Rename the resultant file from .h to .c.
Add a line "#include <original.h>" at the head.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #3

P: n/a
"Marco" <pr**********@yahoo.com> wrote
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

Unfortunately it is not possible to write a general-case "stub generator".
C functions are allowed to modify values passed to them by pointers, and the
structures pointed to can be arbitrarily complex.
Also, it is permitted to call functions by indirection.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #4

P: n/a
Marco wrote:
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").


I think you can cannibalize this stubgen using a couple of scripts:

1. The first script adds a line
class TemporaryUglyClass {
before the .h file, and the line
};
after the .h file. Don't change the file, just send all the stuff to
to the standard output. It's just 3 lines of awk or shell script.
2. The 2nd script reads the result of 'stubgen', changes any
"Method: TemporaryUglyClass::" string to :Function: " string and
removes all other "TemporaryUglyClass::" strings. It's also 3 lines of
awk.
3. The 3rd script runs the first script on your .h file, runs 'stubgen'
on the result of the 1st script, and runs the 2nd script on the result
of 'stubgen'. This can be one line of a shell script.
1st_scr Input_file_name | stubgen | 2nd_scr > stubs_file_name.c

Michael
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #5

P: n/a
I would recomment getting a perl book. This is possible in a few lines...

"Marco" <pr**********@yahoo.com> wrote in message
news:cl****************@plethora.net...
I am looking for an open-source or free tool that parses a C header
file (.h) for a module and creates a .c file with the functions stubbed
out ( ideally with dummy returns for non-void). This will be used for
testing purposes.

I have googled with little success. ( for a C++ class example google
"stubgen").

Any leads would be appreciated.

thanks

Marco
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.

--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #6

P: n/a
Hi,

You can try the following logic :

1) Open your header file in Read mode.
2) Create the c stub file. (Open in write mode)
3) Read the header file line by line and search for character "(" or
")" in the line.
4) If you don't find this character then ignore the line, otherwise
write the whole line to the C file, Don't write the last character ";"
to the c file.
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;
}"

else
just write the following in the .c file
"{
return;
}"

HTH
Rahul
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 10 '06 #7

P: n/a
In message <cl****************@plethora.net>, Rahul Chandok
<ch*******@gmail.com> writes
5) Then search for the void in the beggining of the line, if it is not
there then
write the following in the c file
"{
return 0;
"


struct X {
int i;
int j;
} myfunc();

I think fails under your method.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Jan 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.