Skip to content

Commit 84eb267

Browse files
committed
creating a stub for all public interface functions
creating some global utilities for core and a few creation stubs DEBUG define for debug builds reuse default_int as a way to index SETSTR variables
1 parent cf770ad commit 84eb267

File tree

14 files changed

+708
-34
lines changed

14 files changed

+708
-34
lines changed

cmd/cmd.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<ClCompile>
9999
<WarningLevel>Level4</WarningLevel>
100100
<SDLCheck>true</SDLCheck>
101-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
101+
<PreprocessorDefinitions>DEBUG;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
102102
<ConformanceMode>true</ConformanceMode>
103103
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
104104
<AdditionalIncludeDirectories>..\include\</AdditionalIncludeDirectories>
@@ -134,7 +134,7 @@
134134
<ClCompile>
135135
<WarningLevel>Level4</WarningLevel>
136136
<SDLCheck>true</SDLCheck>
137-
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
137+
<PreprocessorDefinitions>DEBUG;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
138138
<ConformanceMode>true</ConformanceMode>
139139
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
140140
<AdditionalIncludeDirectories>..\include\</AdditionalIncludeDirectories>

core/core.cpp

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,103 @@
11
// core.cpp
2-
// high level central operation of core functions
2+
// High level central operation of core functions.
3+
// Implementation of NSFCore members.
4+
// Global utilities.
35

46
#include "core.h"
57
#include "enums_data.h"
8+
9+
#include <cstdio> // std::fprintf, std::vsnprintf, stdout, stderr
10+
#include <cstdlib> // std::malloc, std::free, std::exit
11+
#include <cstring> // std:;memset, std::memcpy
12+
#include <cstdarg> // va_list, va_start
13+
14+
namespace nsfp {
15+
16+
void (*debug_print_callback)(const char* msg) = NULL;
17+
void (*fatal_callback)(const char* msg) = NULL;
18+
19+
void* alloc(size_t size)
20+
{
21+
void* a = std::malloc(size);
22+
if (a == NULL) nsfp::fatal("Out of memory.");
23+
NSFP_DEBUG("alloc(%z)=%p",size,a);
24+
return a;
25+
}
26+
27+
void free(void* ptr)
28+
{
29+
NSFP_DEBUG("free(%p)",ptr);
30+
std::free(ptr);
31+
}
32+
33+
void debug(const char* msg)
34+
{
35+
if (debug_print_callback) debug_print_callback(msg);
36+
else std::fprintf(stdout,"%s\n",msg);
37+
}
38+
39+
void debug_printf(const char* fmt,...)
40+
{
41+
#ifdef DEBUG
42+
static char msg[1024];
43+
va_list args;
44+
va_start(args,fmt);
45+
std::vsnprintf(msg,sizeof(msg),msg,args);
46+
#else
47+
(void)fmt;
48+
#endif
49+
}
50+
51+
void fatal(const char* msg)
52+
{
53+
if (fatal_callback) fatal_callback(msg);
54+
std::fprintf(stderr,"%s\n",msg);
55+
std::exit(-1);
56+
}
57+
58+
} // namespace nsfp
59+
60+
NSFCore* NSFCore::create()
61+
{
62+
NSFCore* core = (NSFCore*)nsfp::alloc(sizeof(NSFCore));
63+
NSFP_DEBUG("create()=%p",core);
64+
std::memset(core,0,sizeof(NSFCore));
65+
core->set_default();
66+
return core;
67+
}
68+
69+
void NSFCore::destroy(NSFCore* core)
70+
{
71+
NSFP_DEBUG("destroy(%p)",core);
72+
core->release();
73+
nsfp::free(core);
74+
}
75+
76+
void NSFCore::finalize()
77+
{
78+
// TODO make any allocations needed based on the settings
79+
}
80+
81+
void NSFCore::release()
82+
{
83+
// TODO release any allocations
84+
}
85+
86+
void NSFCore::set_default()
87+
{
88+
// TODO
89+
}
90+
91+
bool NSFCore::set_ini(const char* ini)
92+
{
93+
(void)ini;
94+
// TODO
95+
return false;
96+
}
97+
98+
bool NSFCore::set_init(const NSFSetInit* init)
99+
{
100+
(void)init;
101+
// TODO
102+
return false;
103+
}

core/core.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,46 @@ typedef uint16_t uint16;
2323
typedef uint32_t uint32;
2424
typedef uint64_t uint64;
2525

26+
#ifdef DEBUG
27+
#define NSFP_DEBUG(...) { nsfp::debug_printf(__VA_ARGS__); }
28+
#else
29+
#define NSFP_DEBUG(...) {}
30+
#endif
31+
32+
// NSFCore structure, code members defined in core.cpp
33+
34+
typedef struct NSFCore_
35+
{
36+
sint32 set[NSFP_SET_COUNT]; // all integer settings (values for string settings will index set_str)
37+
const char* set_str[NSFP_SETSTR_COUNT];
38+
bool set_str_free[NSFP_SETSTR_COUNT]; // true if string setting
39+
40+
static NSFCore* create(); // After create: ->set_... then ->finalize.
41+
static void destroy(NSFCore* core); // Calls ->release before freeing the core.
42+
void finalize(); // finishes creation after create and initial settings
43+
void release(); // called by destroy, releases all owned allocations
44+
45+
void set_default();
46+
bool set_ini(const char* ini);
47+
bool set_init(const NSFSetInit* init);
48+
49+
} NSFCore;
50+
51+
namespace nsfp {
52+
53+
// core.cpp
54+
55+
extern "C" {
56+
extern void (*debug_print_callback)(const char* msg);
57+
extern void (*fatal_callback)(const char* msg);
58+
}
59+
60+
void* alloc(size_t size);
61+
void free(void* ptr);
62+
void debug_printf(const char* fmt,...); // only works if DEBUG defined
63+
void debug(const char* msg);
64+
void fatal(const char* msg);
65+
66+
} // namespace nsfp
67+
2668
#endif // __CORE_PCH__

core/core.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<ClCompile>
9999
<WarningLevel>Level4</WarningLevel>
100100
<SDLCheck>true</SDLCheck>
101-
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
101+
<PreprocessorDefinitions>DEBUG;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
102102
<ConformanceMode>true</ConformanceMode>
103103
<PrecompiledHeader>Use</PrecompiledHeader>
104104
<PrecompiledHeaderFile>core.h</PrecompiledHeaderFile>
@@ -140,7 +140,7 @@
140140
<ClCompile>
141141
<WarningLevel>Level4</WarningLevel>
142142
<SDLCheck>true</SDLCheck>
143-
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
143+
<PreprocessorDefinitions>DEBUG;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
144144
<ConformanceMode>true</ConformanceMode>
145145
<PrecompiledHeader>Use</PrecompiledHeader>
146146
<PrecompiledHeaderFile>core.h</PrecompiledHeaderFile>

0 commit comments

Comments
 (0)