Code style cleanup in C++ example.

This commit is contained in:
Geenz 2015-04-01 08:59:27 -04:00
parent 4c07491db6
commit 5af3411513
6 changed files with 129 additions and 132 deletions

View File

@ -20,14 +20,14 @@
#include "example_class.h" #include "example_class.h"
extern "C" { extern "C" {
int page(struct http_request *); int page(struct http_request *);
} }
int int
page(struct http_request *req) page(struct http_request *req)
{ {
example_class example; example_class example;
const char* str = example.a(); const char* str = example.a();
http_response(req, 200, static_cast<void*>(const_cast<char*>(str)), strlen(str)); http_response(req, 200, static_cast<void*>(const_cast<char*>(str)), strlen(str));
return (KORE_RESULT_OK); return (KORE_RESULT_OK);
} }

View File

@ -17,13 +17,14 @@
#include "example_class.h" #include "example_class.h"
example_class::example_class() { example_class::example_class() {
} }
example_class::~example_class() { example_class::~example_class() {
} }
const char* example_class::a() { const char*
return "Hello world!"; example_class::a() {
return "Hello world!";
} }

View File

@ -20,12 +20,13 @@
#include <stdio.h> #include <stdio.h>
class example_class { class example_class {
public: public:
example_class(); example_class();
~example_class(); ~example_class();
const char* a(); const char*
a();
}; };
#endif /* defined(____example_class__) */ #endif /* defined(____example_class__) */

View File

@ -68,7 +68,7 @@ struct spdy_stream {
}; };
extern const unsigned char SPDY_dictionary_txt[]; extern const unsigned char SPDY_dictionary_txt[];
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -76,7 +76,7 @@ void kore_task_set_result(struct kore_task *, int);
int kore_task_state(struct kore_task *); int kore_task_state(struct kore_task *);
int kore_task_result(struct kore_task *); int kore_task_result(struct kore_task *);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

231
src/cli.c
View File

@ -107,9 +107,9 @@ static void cli_add_cfile(char *, char *, char *,
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
static void cli_add_cppfile(char *, char *, char *, static void cli_add_cppfile(char *, char *, char *,
struct stat *, int); struct stat *, int);
static void cli_register_cppfile(char *, struct dirent *); static void cli_register_cppfile(char *, struct dirent *);
static void cli_compile_cppfile(void *); static void cli_compile_cppfile(void *);
#endif #endif
static void cli_run(int, char **); static void cli_run(int, char **);
@ -201,8 +201,8 @@ static int cfiles_count;
static struct cmd *command = NULL; static struct cmd *command = NULL;
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
static char *cppcompiler = "g++"; static char *cppcompiler = "g++";
static struct cfile_list cpp_files; static struct cfile_list cpp_files;
#endif #endif
void void
@ -312,17 +312,17 @@ cli_build(int argc, char **argv)
if ((p = getenv("CC")) != NULL) if ((p = getenv("CC")) != NULL)
compiler = p; compiler = p;
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
if ((p = getenv("CXX")) != NULL) if ((p = getenv("CXX")) != NULL)
cppcompiler = p; cppcompiler = p;
#endif #endif
cfiles_count = 0; cfiles_count = 0;
TAILQ_INIT(&source_files); TAILQ_INIT(&source_files);
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
TAILQ_INIT(&cpp_files); TAILQ_INIT(&cpp_files);
#endif #endif
(void)cli_vasprintf(&src_path, "%s/src", rootdir); (void)cli_vasprintf(&src_path, "%s/src", rootdir);
@ -356,12 +356,12 @@ cli_build(int argc, char **argv)
free(assets_path); free(assets_path);
/* Build all source files. */ /* Build all source files. */
cli_find_files(src_path, cli_register_cfile); cli_find_files(src_path, cli_register_cfile);
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
cli_find_files(src_path, cli_register_cppfile); cli_find_files(src_path, cli_register_cppfile);
#endif #endif
free(src_path); free(src_path);
requires_relink = 0; requires_relink = 0;
@ -381,24 +381,24 @@ cli_build(int argc, char **argv)
requires_relink++; requires_relink++;
} }
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
TAILQ_FOREACH(cf, &cpp_files, list) { TAILQ_FOREACH(cf, &cpp_files, list) {
if (cf->build == 0) if (cf->build == 0)
continue; continue;
printf("compiling %s\n", cf->name); printf("compiling %s\n", cf->name);
cli_spawn_proc(cli_compile_cppfile, cf); cli_spawn_proc(cli_compile_cppfile, cf);
times[0].tv_usec = 0; times[0].tv_usec = 0;
times[0].tv_sec = cf->st.st_mtime; times[0].tv_sec = cf->st.st_mtime;
times[1] = times[0]; times[1] = times[0];
if (utimes(cf->opath, times) == -1) if (utimes(cf->opath, times) == -1)
printf("utime(%s): %s\n", cf->opath, errno_s); printf("utime(%s): %s\n", cf->opath, errno_s);
requires_relink++; requires_relink++;
} }
#endif #endif
(void)unlink(assets_header); (void)unlink(assets_header);
@ -733,42 +733,39 @@ cli_add_cfile(char *name, char *fpath, char *opath, struct stat *st, int build)
static void static void
cli_add_cppfile(char *name, char *fpath, char *opath, struct stat *st, int build) cli_add_cppfile(char *name, char *fpath, char *opath, struct stat *st, int build)
{ {
struct cfile *cf; struct cfile *cf;
cfiles_count++; cfiles_count++;
cf = kore_malloc(sizeof(*cf)); cf = kore_malloc(sizeof(*cf));
cf->st = *st; cf->st = *st;
cf->build = build; cf->build = build;
cf->fpath = fpath; cf->fpath = fpath;
cf->opath = opath; cf->opath = opath;
cf->name = kore_strdup(name); cf->name = kore_strdup(name);
TAILQ_INSERT_TAIL(&cpp_files, cf, list); TAILQ_INSERT_TAIL(&cpp_files, cf, list);
} }
static void static void
cli_register_cppfile(char *fpath, struct dirent *dp) cli_register_cppfile(char *fpath, struct dirent *dp)
{ {
struct stat st; struct stat st;
char *ext, *opath; char *ext, *opath;
if ((ext = strrchr(fpath, '.')) == NULL || strcmp(ext, ".cpp")) { if ((ext = strrchr(fpath, '.')) == NULL || strcmp(ext, ".cpp"))
return; return;
}
if (stat(fpath, &st) == -1)
if (stat(fpath, &st) == -1) cli_fatal("stat(%s): %s", fpath, errno_s);
cli_fatal("stat(%s): %s", fpath, errno_s);
(void)cli_vasprintf(&opath, "%s/.objs/%s.o", rootdir, dp->d_name);
(void)cli_vasprintf(&opath, "%s/.objs/%s.o", rootdir, dp->d_name); if (!cli_file_requires_build(&st, opath)) {
if (!cli_file_requires_build(&st, opath)) { cli_add_cppfile(dp->d_name, fpath, opath, &st, 0);
return;
cli_add_cppfile(dp->d_name, fpath, opath, &st, 0); }
return; cli_add_cppfile(dp->d_name, fpath, opath, &st, 1);
}
cli_add_cppfile(dp->d_name, fpath, opath, &st, 1);
} }
#endif #endif
@ -778,21 +775,19 @@ cli_register_cfile(char *fpath, struct dirent *dp)
struct stat st; struct stat st;
char *ext, *opath; char *ext, *opath;
if ((ext = strrchr(fpath, '.')) == NULL || strcmp(ext, ".c")) { if ((ext = strrchr(fpath, '.')) == NULL || strcmp(ext, ".c"))
return; return;
}
if (stat(fpath, &st) == -1) if (stat(fpath, &st) == -1)
cli_fatal("stat(%s): %s", fpath, errno_s); cli_fatal("stat(%s): %s", fpath, errno_s);
(void)cli_vasprintf(&opath, "%s/.objs/%s.o", rootdir, dp->d_name); (void)cli_vasprintf(&opath, "%s/.objs/%s.o", rootdir, dp->d_name);
if (!cli_file_requires_build(&st, opath)) { if (!cli_file_requires_build(&st, opath)) {
cli_add_cfile(dp->d_name, fpath, opath, &st, 0); cli_add_cfile(dp->d_name, fpath, opath, &st, 0);
return; return;
} }
cli_add_cfile(dp->d_name, fpath, opath, &st, 1); cli_add_cfile(dp->d_name, fpath, opath, &st, 1);
} }
static void static void
@ -988,58 +983,58 @@ cli_compile_cfile(void *arg)
static void static void
cli_compile_cppfile(void *arg) cli_compile_cppfile(void *arg)
{ {
int idx; int idx;
struct cfile *cf = arg; struct cfile *cf = arg;
char *args[24], *ipath[2]; char *args[24], *ipath[2];
#if defined(KORE_USE_PGSQL) #if defined(KORE_USE_PGSQL)
char *ppath; char *ppath;
#endif #endif
(void)cli_vasprintf(&ipath[0], "-I%s/src", rootdir); (void)cli_vasprintf(&ipath[0], "-I%s/src", rootdir);
(void)cli_vasprintf(&ipath[1], "-I%s/src/includes", rootdir); (void)cli_vasprintf(&ipath[1], "-I%s/src/includes", rootdir);
/* /*
* These compiler options should be settable * These compiler options should be settable
* somehow by the user if they so choose. * somehow by the user if they so choose.
*/ */
idx = 0; idx = 0;
args[idx++] = cppcompiler; args[idx++] = cppcompiler;
args[idx++] = ipath[0]; args[idx++] = ipath[0];
args[idx++] = ipath[1]; args[idx++] = ipath[1];
#if defined(PREFIX) #if defined(PREFIX)
(void)cli_vasprintf(&args[idx++], "-I%s/include", PREFIX); (void)cli_vasprintf(&args[idx++], "-I%s/include", PREFIX);
#else #else
args[idx++] = "-I/usr/local/include"; args[idx++] = "-I/usr/local/include";
#endif #endif
#if defined(KORE_USE_PGSQL) #if defined(KORE_USE_PGSQL)
(void)cli_vasprintf(&ppath, "-I%s", PGSQL_INCLUDE_PATH); (void)cli_vasprintf(&ppath, "-I%s", PGSQL_INCLUDE_PATH);
args[idx++] = ppath; args[idx++] = ppath;
#endif #endif
args[idx++] = "-Wall"; args[idx++] = "-Wall";
args[idx++] = "-Wstrict-prototypes"; args[idx++] = "-Wstrict-prototypes";
args[idx++] = "-Wmissing-prototypes"; args[idx++] = "-Wmissing-prototypes";
args[idx++] = "-Wmissing-declarations"; args[idx++] = "-Wmissing-declarations";
args[idx++] = "-Wshadow"; args[idx++] = "-Wshadow";
args[idx++] = "-Wpointer-arith"; args[idx++] = "-Wpointer-arith";
args[idx++] = "-Wcast-qual"; args[idx++] = "-Wcast-qual";
args[idx++] = "-Wsign-compare"; args[idx++] = "-Wsign-compare";
args[idx++] = "-fPIC"; args[idx++] = "-fPIC";
args[idx++] = "-g"; args[idx++] = "-g";
args[idx++] = "-Woverloaded-virtual"; args[idx++] = "-Woverloaded-virtual";
args[idx++] = "-Wold-style-cast"; args[idx++] = "-Wold-style-cast";
args[idx++] = "-Wnon-virtual-dtor"; args[idx++] = "-Wnon-virtual-dtor";
args[idx++] = "-std=c++11"; args[idx++] = "-std=c++11";
args[idx++] = "-c"; args[idx++] = "-c";
args[idx++] = cf->fpath; args[idx++] = cf->fpath;
args[idx++] = "-o"; args[idx++] = "-o";
args[idx++] = cf->opath; args[idx++] = cf->opath;
args[idx] = NULL; args[idx] = NULL;
execvp(compiler, args); execvp(compiler, args);
} }
#endif #endif
@ -1072,14 +1067,14 @@ cli_link_library(void *arg)
TAILQ_FOREACH(cf, &source_files, list) TAILQ_FOREACH(cf, &source_files, list)
args[idx++] = cf->opath; args[idx++] = cf->opath;
#if defined(KORE_CPP_SUPPORT) #if defined(KORE_CPP_SUPPORT)
TAILQ_FOREACH(cf, &cpp_files, list) TAILQ_FOREACH(cf, &cpp_files, list)
args[idx++] = cf->opath; args[idx++] = cf->opath;
args[idx++] = "-lstdc++"; args[idx++] = "-lstdc++";
#endif #endif
for (i = 0; i < f; i++) for (i = 0; i < f; i++)
args[idx++] = flags[i]; args[idx++] = flags[i];