I often find myself writing the same structure for all of my class headers. I want to eliminate repetitive tasks, so I'd like to automate this by making it into a shell script. I'm using a Mac, and I understand there's something called the Automator, but I've never used it before so have no idea what it's for or how to use it, so thought I'd ask a question that people on OTHER platforms might find in the future and be able to use.
I'd like to create a shell script that will perform the following:
Create a filename (case-sensitive) from the supplied argument, plus ".h"
Fill the file with this text:
Create the source file (same filename but with a ".cpp" extension)
Fill the file with this text:
I'm not exactly certain the format (and the whole "$(filename)" format is borrowed from the makefile style), and I don't know how to supply arguments to shell scripts, so any help is appreciated. Even if the help is in the form of "RTM found at website ___"
Try this.
#!/bin/sh if [ "x$1" = "x" ]; then echo "Supply an argument" fi name=$1 sed template.h -e "s/\$(filename)/${name}/g" > $name.h
Use:
$ ./create foo $ cat foo.h #ifndef foo_h #define foo_h class foo { public: // Constructor foo(); // Destructor virtual ~foo(); }; #endif // foo_h
[bamccaig@sephiroth temp]$ ls -la
total 8
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:09 .
drwxrwxr-x 36 bamccaig bamccaig 4096 2009-07-23 11:10 ..
[bamccaig@sephiroth temp]$ addclass foo
[bamccaig@sephiroth temp]$ ls -la
total 16
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:10 .
drwxrwxr-x 36 bamccaig bamccaig 4096 2009-07-23 11:10 ..
-rw-rw-r-- 1 bamccaig bamccaig 85 2009-07-23 12:10 foo.cpp
-rw-rw-r-- 1 bamccaig bamccaig 118 2009-07-23 12:10 foo.hpp
[bamccaig@sephiroth temp]$ cat foo.hpp
#ifndef FOO_HPP
#define FOO_HPP
class foo
{
public:
foo(void);
virtual ~foo(void);
};
#endif //FOO_HPP
[bamccaig@sephiroth temp]$ cat foo.cpp
#include <foo.hpp>
foo::foo(void)
{
// TODO
}
foo::~foo(void)
{
// TODO
}
[bamccaig@sephiroth temp]$ mkdir include src
[bamccaig@sephiroth temp]$ addclass foo bar
[bamccaig@sephiroth temp]$ ls -Rla
.:
total 24
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 12:11 .
drwxrwxr-x 36 bamccaig bamccaig 4096 2009-07-23 11:10 ..
-rw-rw-r-- 1 bamccaig bamccaig 85 2009-07-23 12:10 foo.cpp
-rw-rw-r-- 1 bamccaig bamccaig 118 2009-07-23 12:10 foo.hpp
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:35 include
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:35 src
./include:
total 12
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:35 .
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 12:11 ..
-rw-rw-r-- 1 bamccaig bamccaig 160 2009-07-23 12:35 foo.hpp
./src:
total 12
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 12:35 .
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 12:11 ..
-rw-rw-r-- 1 bamccaig bamccaig 135 2009-07-23 12:35 foo.cpp
[bamccaig@sephiroth temp]$ cat include/foo.hpp
#ifndef FOO_HPP
#define FOO_HPP
namespace bar
{
class foo
{
public:
foo(void);
virtual ~foo(void);
};
}
#endif //FOO_HPP
[bamccaig@sephiroth temp]$ cat src/foo.cpp
#include <foo.hpp>
namespace bar
{
foo::foo(void)
{
// TODO
}
foo::~foo(void)
{
// TODO
}
}
That's actually kind of useful.
I might just go ahead and try to create a library of bash functions to create file stubs for different types of symbols (enums, structs, classes, functions, main, etc.).
Imagine stubbing out a whole project with just a series of commands...
That's actually kind of useful. I might just go ahead and try to create a library of bash functions to create file stubs for different types of symbols (enums, structs, classes, functions, main, etc.).
When you do, post them here. I'd certainly be interested in them.
I can even see derived classes being handy:
#ifndef FOO_HPP #define FOO_HPP #include "bar.hpp" class foo : public bar { public: foo(); virtual ~foo(); }; #endif //FOO_HPP
I did something similar with PHP some time ago.
Just a text input and a submit button, when you submit it creates .cpp and .h file for you both on screen and links so you can save them to your computer
I actually hadn't noticed that, with only minor modifications, bamccaig's code already DOES have all the necessary support for inheritance.
Now onto phase 2: the Allegro Timer template. You know the one... it won't exceed a certain FPS under light operation, but will start to dynamically skip frames based on refresh preferences under heavy loads. It also maintains a low CPU usage (around 1%) unless it starts really getting into heavy 3D rendering 
Updated to include namespace support in header guards and file paths (i.e., namespace foo { class bar; } becomes FOO_BAR_HPP, include/foo/bar.hpp, and src/foo/bar.cpp). Obviously everybody does this a little bit different, but this is how I would do it at this particular point in time.
[bamccaig@sephiroth temp]$ ls -la
total 8
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 36 bamccaig bamccaig 4096 2009-07-23 15:28 ..
[bamccaig@sephiroth temp]$ mkdir include src
[bamccaig@sephiroth temp]$ addclass foo bar::baz
[bamccaig@sephiroth temp]$ ls -Rla
.:
total 16
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 36 bamccaig bamccaig 4096 2009-07-23 15:28 ..
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 include
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 src
./include:
total 12
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 23:48 ..
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 bar
./include/bar:
total 12
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 ..
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 23:48 baz
./include/bar/baz:
total 12
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 ..
-rw-rw-r-- 1 bamccaig bamccaig 189 2009-07-23 23:48 foo.hpp
./src:
total 12
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 4 bamccaig bamccaig 4096 2009-07-23 23:48 ..
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 bar
./src/bar:
total 12
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 ..
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 23:48 baz
./src/bar/baz:
total 12
drwxrwxr-x 2 bamccaig bamccaig 4096 2009-07-23 23:48 .
drwxrwxr-x 3 bamccaig bamccaig 4096 2009-07-23 23:48 ..
-rw-rw-r-- 1 bamccaig bamccaig 140 2009-07-23 23:48 foo.cpp
[bamccaig@sephiroth temp]$ cat */*/*/*
#ifndef BAR_BAZ_FOO_HPP
#define BAR_BAZ_FOO_HPP
namespace bar::baz
{
class foo
{
public:
foo(void);
virtual ~foo(void);
};
}
#endif //BAR_BAZ_FOO_HPP
#include <bar/baz/foo.hpp>
namespace bar::baz
{
foo::foo(void)
{
// TODO
}
foo::~foo(void)
{
// TODO
}
}
[bamccaig@sephiroth temp]$
I probably find nested namespaces more structurally obvious than qualified ones, but that would require more manipulation of arguments so I just settled for QnD right now. 
** EDIT **
Note: One of the <code> lines (the highlighted line) apparently lost a backslash in the parsing stage. I think I fixed it by escaping it, but there could be more so I'm going to attach the script to make sure the code is available.
Don't forget here documents.
#!/bin/sh base=$(basename $1 .c) cat <<EOF #include "${base}.h" /* blah blah */ EOF
...
namespace bar::baz
{
...
I probably find nested namespaces more structurally obvious than qualified ones, but that would require more manipulation of arguments so I just settled for QnD right now. 
Hmmm, wait, maybe I'm confusing C# syntax with C++ syntax. Is this legal in C++?
namespace bar::baz { }
It isn't compiling...
[bamccaig@sephiroth temp]$ g++ -Iinclude src/bar/baz/foo.cpp -c
In file included from src/bar/baz/foo.cpp:2:
include/bar/baz/foo.hpp:4: error: expected `{' before ‘::’ token
include/bar/baz/foo.hpp:4: error: function definition does not declare parameters
src/bar/baz/foo.cpp:3: error: expected `{' before ‘::’ token
src/bar/baz/foo.cpp:3: error: function definition does not declare parameters
src/bar/baz/foo.cpp:14: error: expected `}' at end of input
src/bar/baz/foo.cpp:14: error: expected `}' at end of input
[bamccaig@sephiroth temp]$
...and I can't find any tutorials or guides that suggest this syntax is supported.
The following is me trying to type out from scratch my 'mksfp' (make single-file project) script:
The Makefile.template:
EXE= EXENAME OFILES= main.o $(EXE): $(OFILES) $(CC) -o $@ $(OFILES) .o.c: $(CC) -c -Wall $< clean: -rm *.o $(EXE)
I probably made some mistake or other typing it.