remove "namespace jes" wrapper around C++ content

This commit is contained in:
Josh Holtrop 2014-07-19 12:42:29 -04:00
parent b6c5e9e1d7
commit c3b4c33304
23 changed files with 969 additions and 1038 deletions

View File

@ -7,26 +7,24 @@
#include <unistd.h> #include <unistd.h>
#include <stdint.h> #include <stdint.h>
namespace jes FileLoader::FileLoader()
{ {
FileLoader::FileLoader()
{
m_buf = NULL; m_buf = NULL;
m_line_endings = LINE_ENDING_COUNT; m_line_endings = LINE_ENDING_COUNT;
m_lines = NULL; m_lines = NULL;
} }
FileLoader::~FileLoader() FileLoader::~FileLoader()
{ {
if (m_buf != NULL) if (m_buf != NULL)
{ {
delete[] m_buf; delete[] m_buf;
m_buf = NULL; m_buf = NULL;
} }
} }
bool FileLoader::load(const char * fname) bool FileLoader::load(const char * fname)
{ {
size_t size; size_t size;
if (!FileReader::load(fname, &m_buf, &size)) if (!FileReader::load(fname, &m_buf, &size))
@ -37,10 +35,10 @@ namespace jes
load_buf(size); load_buf(size);
return true; return true;
} }
void FileLoader::load_buf(size_t size) void FileLoader::load_buf(size_t size)
{ {
LineIndexPairVectorRef lines[LINE_ENDING_COUNT]; LineIndexPairVectorRef lines[LINE_ENDING_COUNT];
size_t line_start[LINE_ENDING_COUNT] = {0}; size_t line_start[LINE_ENDING_COUNT] = {0};
unsigned int n_cr = 0; unsigned int n_cr = 0;
@ -96,10 +94,9 @@ namespace jes
m_line_endings = LINE_ENDING_LF; m_line_endings = LINE_ENDING_LF;
m_lines = lines[LINE_ENDING_LF]; m_lines = lines[LINE_ENDING_LF];
} }
} }
TextRef FileLoader::get_line(unsigned int line_no) TextRef FileLoader::get_line(unsigned int line_no)
{ {
return new Text((*m_lines)[line_no].first, (*m_lines)[line_no].second); return new Text((*m_lines)[line_no].first, (*m_lines)[line_no].second);
}
} }

View File

@ -6,11 +6,9 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
namespace jes class FileLoader
{ {
class FileLoader public:
{
public:
enum enum
{ {
LINE_ENDING_LF, LINE_ENDING_LF,
@ -35,7 +33,7 @@ namespace jes
} }
TextRef get_line(unsigned int line_no); TextRef get_line(unsigned int line_no);
int get_line_endings() { return m_line_endings; } int get_line_endings() { return m_line_endings; }
protected: protected:
typedef std::pair<const uint8_t *, size_t> LineIndexPair; typedef std::pair<const uint8_t *, size_t> LineIndexPair;
typedef std::vector<LineIndexPair> LineIndexPairVector; typedef std::vector<LineIndexPair> LineIndexPairVector;
typedef Ref<LineIndexPairVector> LineIndexPairVectorRef; typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
@ -43,8 +41,7 @@ namespace jes
uint8_t * m_buf; uint8_t * m_buf;
int m_line_endings; int m_line_endings;
LineIndexPairVectorRef m_lines; LineIndexPairVectorRef m_lines;
}; };
typedef Ref<FileLoader> FileLoaderRef; typedef Ref<FileLoader> FileLoaderRef;
}
#endif #endif

View File

@ -10,10 +10,8 @@
#define JES_O_BINARY 0 #define JES_O_BINARY 0
#endif #endif
namespace jes bool FileReader::load(const char * fname, uint8_t ** buf, size_t * size)
{ {
bool FileReader::load(const char * fname, uint8_t ** buf, size_t * size)
{
struct stat st; struct stat st;
if (stat(fname, &st) != 0) if (stat(fname, &st) != 0)
{ {
@ -60,5 +58,4 @@ namespace jes
close(fd); close(fd);
return true; return true;
}
} }

View File

@ -4,13 +4,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
namespace jes class FileReader
{ {
class FileReader public:
{
public:
static bool load(const char * fname, uint8_t ** buf, size_t * size); static bool load(const char * fname, uint8_t ** buf, size_t * size);
}; };
}
#endif #endif

View File

@ -4,23 +4,21 @@
#define round_up_26_6(val) (((val) + 63) >> 6u) #define round_up_26_6(val) (((val) + 63) >> 6u)
namespace jes Font::Font()
{ {
Font::Font()
{
m_loaded = false; m_loaded = false;
} }
Font::~Font() Font::~Font()
{ {
if (m_loaded) if (m_loaded)
{ {
FT_Done_Face(m_face); FT_Done_Face(m_face);
} }
} }
bool Font::load(FT_Library ft, const char * fname, size_t size) bool Font::load(FT_Library ft, const char * fname, size_t size)
{ {
if (FT_New_Face(ft, fname, 0, &m_face) != 0) if (FT_New_Face(ft, fname, 0, &m_face) != 0)
{ {
std::cerr << "Could not load font " << fname << std::endl; std::cerr << "Could not load font " << fname << std::endl;
@ -46,23 +44,23 @@ namespace jes
m_loaded = true; m_loaded = true;
return true; return true;
} }
Font::GlyphRef Font::load_glyph(FT_ULong char_code) Font::GlyphRef Font::load_glyph(FT_ULong char_code)
{ {
GlyphRef glyph = new Glyph(); GlyphRef glyph = new Glyph();
if (glyph->load(m_face, char_code)) if (glyph->load(m_face, char_code))
return glyph; return glyph;
return NULL; return NULL;
} }
Font::Glyph::Glyph() Font::Glyph::Glyph()
{ {
m_loaded = false; m_loaded = false;
} }
bool Font::Glyph::load(FT_Face face, FT_ULong char_code) bool Font::Glyph::load(FT_Face face, FT_ULong char_code)
{ {
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0) if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
return false; return false;
@ -108,5 +106,4 @@ namespace jes
m_loaded = true; m_loaded = true;
return true; return true;
}
} }

View File

@ -8,11 +8,9 @@
#include <unordered_map> #include <unordered_map>
#include "GLTexture.h" #include "GLTexture.h"
namespace jes class Font
{ {
class Font public:
{
public:
class Glyph class Glyph
{ {
public: public:
@ -50,7 +48,7 @@ namespace jes
m_glyphs[char_code] = g; m_glyphs[char_code] = g;
return g; return g;
} }
protected: protected:
GlyphRef load_glyph(FT_ULong char_code); GlyphRef load_glyph(FT_ULong char_code);
FT_Face m_face; FT_Face m_face;
@ -59,9 +57,8 @@ namespace jes
int m_baseline_offset; int m_baseline_offset;
bool m_loaded; bool m_loaded;
std::unordered_map<FT_ULong, GlyphRef> m_glyphs; std::unordered_map<FT_ULong, GlyphRef> m_glyphs;
}; };
typedef Ref<Font> FontRef; typedef Ref<Font> FontRef;
}
#endif #endif

View File

@ -1,10 +1,8 @@
#include "FontManager.h" #include "FontManager.h"
#include <iostream> #include <iostream>
namespace jes bool FontManager::load()
{ {
bool FontManager::load()
{
if (FT_Init_FreeType(&m_ft) != 0) if (FT_Init_FreeType(&m_ft) != 0)
{ {
std::cerr << "Error initializing FreeType" << std::endl; std::cerr << "Error initializing FreeType" << std::endl;
@ -21,10 +19,10 @@ namespace jes
} }
return true; return true;
} }
FontRef FontManager::load_font(const char * fname, size_t size) FontRef FontManager::load_font(const char * fname, size_t size)
{ {
/* TODO */ /* TODO */
return NULL; return NULL;
#if 0 #if 0
@ -38,5 +36,4 @@ namespace jes
return font; return font;
#endif #endif
}
} }

View File

@ -6,20 +6,17 @@
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
namespace jes class FontManager
{ {
class FontManager public:
{
public:
bool load(); bool load();
FontRef load_font(const char * fname, size_t size); FontRef load_font(const char * fname, size_t size);
FontRef get_mono_font() { return m_mono_font; } FontRef get_mono_font() { return m_mono_font; }
protected: protected:
FT_Library m_ft; FT_Library m_ft;
FontRef m_mono_font; FontRef m_mono_font;
}; };
typedef Ref<FontManager> FontManagerRef; typedef Ref<FontManager> FontManagerRef;
}
#endif #endif

View File

@ -1,22 +1,20 @@
#include "GLBuffer.h" #include "GLBuffer.h"
namespace jes GLBuffer::GLBuffer()
{ {
GLBuffer::GLBuffer()
{
m_id = 0; m_id = 0;
} }
GLBuffer::~GLBuffer() GLBuffer::~GLBuffer()
{ {
if (m_id > 0) if (m_id > 0)
{ {
glDeleteBuffers(1, &m_id); glDeleteBuffers(1, &m_id);
} }
} }
bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz) bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz)
{ {
m_target = target; m_target = target;
glGenBuffers(1, &m_id); glGenBuffers(1, &m_id);
if (m_id > 0) if (m_id > 0)
@ -26,5 +24,4 @@ namespace jes
return true; return true;
} }
return false; return false;
}
} }

View File

@ -4,10 +4,8 @@
#include "Ref.h" #include "Ref.h"
#include "gl3w.h" #include "gl3w.h"
namespace jes class GLBuffer
{ {
class GLBuffer
{
public: public:
GLBuffer(); GLBuffer();
~GLBuffer(); ~GLBuffer();
@ -17,8 +15,7 @@ namespace jes
protected: protected:
GLuint m_id; GLuint m_id;
GLenum m_target; GLenum m_target;
}; };
typedef Ref<GLBuffer> GLBufferRef; typedef Ref<GLBuffer> GLBufferRef;
}
#endif #endif

View File

@ -4,45 +4,43 @@
using namespace std; using namespace std;
namespace jes GLProgram::GLProgram()
{ {
GLProgram::GLProgram()
{
m_id = glCreateProgram(); m_id = glCreateProgram();
if (m_id == 0u) if (m_id == 0u)
{ {
cerr << "Error allocating GL program object" << endl; cerr << "Error allocating GL program object" << endl;
} }
} }
GLProgram::~GLProgram() GLProgram::~GLProgram()
{ {
if (m_id > 0u) if (m_id > 0u)
{ {
glDeleteProgram(m_id); glDeleteProgram(m_id);
} }
} }
GLProgram & GLProgram::attach_shader(GLShaderRef shader) GLProgram & GLProgram::attach_shader(GLShaderRef shader)
{ {
if (m_id > 0u) if (m_id > 0u)
{ {
glAttachShader(m_id, shader->get_id()); glAttachShader(m_id, shader->get_id());
} }
return *this; return *this;
} }
GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index) GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index)
{ {
if (m_id > 0u) if (m_id > 0u)
{ {
glBindAttribLocation(m_id, index, attribute); glBindAttribLocation(m_id, index, attribute);
} }
return *this; return *this;
} }
bool GLProgram::link() bool GLProgram::link()
{ {
if (m_id == 0u) if (m_id == 0u)
return false; return false;
@ -65,10 +63,10 @@ namespace jes
} }
return true; return true;
} }
GLint GLProgram::get_uniform(const std::string & uniform_name) GLint GLProgram::get_uniform(const std::string & uniform_name)
{ {
if (m_id > 0u) if (m_id > 0u)
{ {
auto it = m_uniforms.find(uniform_name); auto it = m_uniforms.find(uniform_name);
@ -81,5 +79,4 @@ namespace jes
return loc; return loc;
} }
return -1; return -1;
}
} }

View File

@ -7,11 +7,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace jes class GLProgram
{ {
class GLProgram public:
{
public:
GLProgram(); GLProgram();
~GLProgram(); ~GLProgram();
GLProgram & attach_shader(GLShaderRef shader); GLProgram & attach_shader(GLShaderRef shader);
@ -20,11 +18,10 @@ namespace jes
GLuint get_id() { return m_id; } GLuint get_id() { return m_id; }
void use() { glUseProgram(m_id); } void use() { glUseProgram(m_id); }
GLint get_uniform(const std::string & uniform_name); GLint get_uniform(const std::string & uniform_name);
protected: protected:
GLuint m_id; GLuint m_id;
std::map<std::string, GLint> m_uniforms; std::map<std::string, GLint> m_uniforms;
}; };
typedef Ref<GLProgram> GLProgramRef; typedef Ref<GLProgram> GLProgramRef;
}
#endif #endif

View File

@ -3,18 +3,16 @@
using namespace std; using namespace std;
namespace jes GLShader::~GLShader()
{ {
GLShader::~GLShader()
{
if (m_id > 0) if (m_id > 0)
{ {
glDeleteShader(m_id); glDeleteShader(m_id);
} }
} }
bool GLShader::create(GLenum shader_type, const char *source, size_t size) bool GLShader::create(GLenum shader_type, const char *source, size_t size)
{ {
GLint status; GLint status;
m_id = glCreateShader(shader_type); m_id = glCreateShader(shader_type);
@ -54,10 +52,10 @@ namespace jes
glDeleteShader(m_id); glDeleteShader(m_id);
} }
return false; return false;
} }
bool GLShader::create(GLenum shader_type, PathRef path) bool GLShader::create(GLenum shader_type, PathRef path)
{ {
uint8_t * file; uint8_t * file;
size_t size; size_t size;
if (!path->read(&file, &size)) if (!path->read(&file, &size))
@ -68,5 +66,4 @@ namespace jes
bool result = create(shader_type, (const char *)file, size); bool result = create(shader_type, (const char *)file, size);
delete[] file; delete[] file;
return result; return result;
}
} }

View File

@ -5,21 +5,18 @@
#include "Path.h" #include "Path.h"
#include "gl3w.h" #include "gl3w.h"
namespace jes class GLShader
{ {
class GLShader public:
{
public:
GLShader() { m_id = 0u; } GLShader() { m_id = 0u; }
~GLShader(); ~GLShader();
bool create(GLenum shader_type, const char *source, size_t size); bool create(GLenum shader_type, const char *source, size_t size);
bool create(GLenum shader_type, PathRef path); bool create(GLenum shader_type, PathRef path);
GLuint get_id() { return m_id; } GLuint get_id() { return m_id; }
bool valid() { return m_id > 0; } bool valid() { return m_id > 0; }
protected: protected:
GLuint m_id; GLuint m_id;
}; };
typedef Ref<GLShader> GLShaderRef; typedef Ref<GLShader> GLShaderRef;
}
#endif #endif

View File

@ -4,22 +4,20 @@
using namespace std; using namespace std;
namespace jes GLTexture::GLTexture()
{ {
GLTexture::GLTexture()
{
glGenTextures(1, &m_id); glGenTextures(1, &m_id);
m_width = 0u; m_width = 0u;
m_height = 0u; m_height = 0u;
} }
GLTexture::~GLTexture() GLTexture::~GLTexture()
{ {
glDeleteTextures(1, &m_id); glDeleteTextures(1, &m_id);
} }
uint32_t GLTexture::next_power_of_2(uint32_t n) uint32_t GLTexture::next_power_of_2(uint32_t n)
{ {
n--; n--;
n |= n >> 1u; n |= n >> 1u;
n |= n >> 2u; n |= n >> 2u;
@ -28,10 +26,10 @@ namespace jes
n |= n >> 16u; n |= n >> 16u;
n++; n++;
return n; return n;
} }
void GLTexture::create(unsigned int width, unsigned int height) void GLTexture::create(unsigned int width, unsigned int height)
{ {
m_width = width = next_power_of_2(width); m_width = width = next_power_of_2(width);
m_height = height = next_power_of_2(height); m_height = height = next_power_of_2(height);
uint8_t * d = new uint8_t[width * height]; uint8_t * d = new uint8_t[width * height];
@ -40,5 +38,4 @@ namespace jes
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
delete[] d; delete[] d;
}
} }

View File

@ -5,11 +5,9 @@
#include "gl3w.h" #include "gl3w.h"
#include <stdint.h> #include <stdint.h>
namespace jes class GLTexture
{ {
class GLTexture public:
{
public:
GLTexture(); GLTexture();
~GLTexture(); ~GLTexture();
GLuint get_id() { return m_id; } GLuint get_id() { return m_id; }
@ -18,12 +16,11 @@ namespace jes
void create(unsigned int width, unsigned int height); void create(unsigned int width, unsigned int height);
unsigned int get_width() { return m_width; } unsigned int get_width() { return m_width; }
unsigned int get_height() { return m_height; } unsigned int get_height() { return m_height; }
protected: protected:
GLuint m_id; GLuint m_id;
unsigned int m_width; unsigned int m_width;
unsigned int m_height; unsigned int m_height;
}; };
typedef Ref<GLTexture> GLTextureRef; typedef Ref<GLTexture> GLTextureRef;
}
#endif #endif

View File

@ -5,10 +5,8 @@
#define WIDTH 500 #define WIDTH 500
#define HEIGHT 500 #define HEIGHT 500
namespace jes bool GUI::load()
{ {
bool GUI::load()
{
glClearColor (0.0, 0.0, 0.0, 0.0); glClearColor (0.0, 0.0, 0.0, 0.0);
m_font_manager = new FontManager(); m_font_manager = new FontManager();
@ -23,10 +21,10 @@ namespace jes
return false; return false;
return true; return true;
} }
bool GUI::load_shaders() bool GUI::load_shaders()
{ {
static const char * shader_sources[PROGRAM_COUNT][2] = { static const char * shader_sources[PROGRAM_COUNT][2] = {
{"text.v.glsl", "text.f.glsl"}, {"text.v.glsl", "text.f.glsl"},
{"basic.v.glsl", "basic.f.glsl"}, {"basic.v.glsl", "basic.f.glsl"},
@ -65,10 +63,10 @@ namespace jes
} }
return true; return true;
} }
bool GUI::load_buffers() bool GUI::load_buffers()
{ {
static const GLint rect_coords[4][2] = { static const GLint rect_coords[4][2] = {
{0, 0}, {0, 0},
{1, 0}, {1, 0},
@ -84,10 +82,10 @@ namespace jes
} }
return true; return true;
} }
int GUI::run() int GUI::run()
{ {
resize(); resize();
draw(); draw();
@ -117,10 +115,10 @@ namespace jes
} }
return 0; return 0;
} }
void GUI::resize() void GUI::resize()
{ {
GLint viewport_size[2]; GLint viewport_size[2];
SDL_GetWindowSize(m_window, &viewport_size[0], &viewport_size[1]); SDL_GetWindowSize(m_window, &viewport_size[0], &viewport_size[1]);
glViewport(0, 0, viewport_size[0], viewport_size[1]); glViewport(0, 0, viewport_size[0], viewport_size[1]);
@ -129,16 +127,16 @@ namespace jes
m_programs[i]->use(); m_programs[i]->use();
glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]); glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]);
} }
} }
void GUI::draw() void GUI::draw()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(m_window); SDL_GL_SwapWindow(m_window);
} }
void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a) void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a)
{ {
m_programs[PROGRAM_RECT]->use(); m_programs[PROGRAM_RECT]->use();
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y); glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y);
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("size"), width, height); glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("size"), width, height);
@ -147,5 +145,4 @@ namespace jes
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribIPointer(0, 2, GL_INT, 0, 0); glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
} }

View File

@ -7,11 +7,9 @@
#include "GLProgram.h" #include "GLProgram.h"
#include "GLBuffer.h" #include "GLBuffer.h"
namespace jes class GUI
{ {
class GUI public:
{
public:
enum enum
{ {
PROGRAM_TEXT, PROGRAM_TEXT,
@ -21,7 +19,7 @@ namespace jes
}; };
bool load(); bool load();
int run(); int run();
protected: protected:
bool load_shaders(); bool load_shaders();
bool load_buffers(); bool load_buffers();
void resize(); void resize();
@ -32,9 +30,8 @@ namespace jes
FontManagerRef m_font_manager; FontManagerRef m_font_manager;
GLProgramRef m_programs[PROGRAM_COUNT]; GLProgramRef m_programs[PROGRAM_COUNT];
GLBufferRef m_rect_vbo; GLBufferRef m_rect_vbo;
}; };
typedef Ref<GUI> GUIRef; typedef Ref<GUI> GUIRef;
}
#endif #endif

View File

@ -5,22 +5,20 @@
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
namespace jes Path::Path(const char * path)
{ {
Path::Path(const char * path)
{
m_path = path; m_path = path;
clean(); clean();
} }
Path::Path(const std::string & path) Path::Path(const std::string & path)
{ {
m_path = path; m_path = path;
clean(); clean();
} }
PathRef Path::dirname() PathRef Path::dirname()
{ {
if (m_path.size() == 0) if (m_path.size() == 0)
return new Path(""); return new Path("");
size_t i = m_path.size() - 1; size_t i = m_path.size() - 1;
@ -43,10 +41,10 @@ namespace jes
i--; i--;
} }
return new Path(std::string(m_path, 0, i + 1)); return new Path(std::string(m_path, 0, i + 1));
} }
PathRef Path::ext(const std::string & new_ext) PathRef Path::ext(const std::string & new_ext)
{ {
if (m_path.size() == 0) if (m_path.size() == 0)
return new Path(""); return new Path("");
size_t i = m_path.size() - 1; size_t i = m_path.size() - 1;
@ -71,24 +69,24 @@ namespace jes
{ {
return new Path(std::string(m_path, 0, i) + "." + new_ext); return new Path(std::string(m_path, 0, i) + "." + new_ext);
} }
} }
PathRef Path::join(const Path & other) PathRef Path::join(const Path & other)
{ {
if (m_path.size() > 0 && *m_path.rbegin() == '/') if (m_path.size() > 0 && *m_path.rbegin() == '/')
return new Path(m_path + other.m_path); return new Path(m_path + other.m_path);
else else
return new Path(m_path + '/' + other.m_path); return new Path(m_path + '/' + other.m_path);
} }
bool Path::exists() bool Path::exists()
{ {
struct stat st; struct stat st;
return stat(m_path.c_str(), &st) == 0; return stat(m_path.c_str(), &st) == 0;
} }
std::vector<std::string> Path::dir_entries() std::vector<std::string> Path::dir_entries()
{ {
std::vector<std::string> rv; std::vector<std::string> rv;
DIR * dir = opendir(m_path.c_str()); DIR * dir = opendir(m_path.c_str());
if (dir != NULL) if (dir != NULL)
@ -103,19 +101,18 @@ namespace jes
closedir(dir); closedir(dir);
} }
return rv; return rv;
} }
bool Path::read(uint8_t ** buf, size_t * size) bool Path::read(uint8_t ** buf, size_t * size)
{ {
return FileReader::load(m_path.c_str(), buf, size); return FileReader::load(m_path.c_str(), buf, size);
} }
void Path::clean() void Path::clean()
{ {
for (char & c : m_path) for (char & c : m_path)
{ {
if (c == '\\') if (c == '\\')
c = '/'; c = '/';
} }
}
} }

View File

@ -6,13 +6,11 @@
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
namespace jes class Path;
typedef Ref<Path> PathRef;
class Path
{ {
class Path; public:
typedef Ref<Path> PathRef;
class Path
{
public:
Path(const char * path); Path(const char * path);
Path(const std::string & path); Path(const std::string & path);
PathRef dirname(); PathRef dirname();
@ -22,10 +20,9 @@ namespace jes
bool exists(); bool exists();
std::vector<std::string> dir_entries(); std::vector<std::string> dir_entries();
bool read(uint8_t ** buf, size_t * size); bool read(uint8_t ** buf, size_t * size);
protected: protected:
void clean(); void clean();
std::string m_path; std::string m_path;
}; };
}
#endif #endif

View File

@ -3,11 +3,9 @@
#include <stdlib.h> #include <stdlib.h>
namespace jes template <typename T>
class Ref
{ {
template <typename T>
class Ref
{
public: public:
Ref<T>(); Ref<T>();
Ref<T>(T * ptr); Ref<T>(T * ptr);
@ -27,65 +25,65 @@ namespace jes
T * m_ptr; T * m_ptr;
unsigned int * m_ref_count; unsigned int * m_ref_count;
}; };
template <typename T> template <typename T>
Ref<T>::Ref() Ref<T>::Ref()
{ {
m_ptr = NULL; m_ptr = NULL;
m_ref_count = NULL; m_ref_count = NULL;
} }
template <typename T> template <typename T>
Ref<T>::Ref(T * ptr) Ref<T>::Ref(T * ptr)
{ {
m_ptr = ptr; m_ptr = ptr;
m_ref_count = new unsigned int; m_ref_count = new unsigned int;
*m_ref_count = 1u; *m_ref_count = 1u;
} }
template <typename T> template <typename T>
Ref<T>::Ref(const Ref<T> & orig) Ref<T>::Ref(const Ref<T> & orig)
{ {
clone_from(orig); clone_from(orig);
} }
template <typename T> template <typename T>
Ref<T> & Ref<T>::operator=(const Ref<T> & orig) Ref<T> & Ref<T>::operator=(const Ref<T> & orig)
{ {
destroy(); destroy();
clone_from(orig); clone_from(orig);
return *this; return *this;
} }
template <typename T> template <typename T>
Ref<T> & Ref<T>::operator=(T * ptr) Ref<T> & Ref<T>::operator=(T * ptr)
{ {
destroy(); destroy();
m_ptr = ptr; m_ptr = ptr;
m_ref_count = new unsigned int; m_ref_count = new unsigned int;
*m_ref_count = 1u; *m_ref_count = 1u;
return *this; return *this;
} }
template <typename T> template <typename T>
void Ref<T>::clone_from(const Ref<T> & orig) void Ref<T>::clone_from(const Ref<T> & orig)
{ {
this->m_ptr = orig.m_ptr; this->m_ptr = orig.m_ptr;
this->m_ref_count = orig.m_ref_count; this->m_ref_count = orig.m_ref_count;
if (m_ref_count != NULL) if (m_ref_count != NULL)
(*m_ref_count)++; (*m_ref_count)++;
} }
template <typename T> template <typename T>
Ref<T>::~Ref() Ref<T>::~Ref()
{ {
destroy(); destroy();
} }
template <typename T> template <typename T>
void Ref<T>::destroy() void Ref<T>::destroy()
{ {
if (m_ref_count != NULL) if (m_ref_count != NULL)
{ {
if (*m_ref_count <= 1u) if (*m_ref_count <= 1u)
@ -98,7 +96,6 @@ namespace jes
(*m_ref_count)--; (*m_ref_count)--;
} }
} }
}
} }
#endif #endif

View File

@ -1,41 +1,38 @@
#include "Text.h" #include "Text.h"
#include <string.h> #include <string.h>
namespace jes Text::Text()
{ {
Text::Text()
{
m_buffer = NULL; m_buffer = NULL;
m_size = 0; m_size = 0;
m_gap_index = 0; m_gap_index = 0;
m_gap_size = 0; m_gap_size = 0;
} }
Text::Text(const uint8_t buf[], size_t size) Text::Text(const uint8_t buf[], size_t size)
{ {
m_buffer = new uint8_t[size]; m_buffer = new uint8_t[size];
m_size = size; m_size = size;
m_gap_index = size; m_gap_index = size;
m_gap_size = 0; m_gap_size = 0;
memcpy(m_buffer, &buf[0], size); memcpy(m_buffer, &buf[0], size);
} }
std::string Text::to_s() std::string Text::to_s()
{ {
if (m_buffer == NULL) if (m_buffer == NULL)
{ {
return ""; return "";
} }
compact(); compact();
return std::string((const char *)m_buffer, m_size - m_gap_size); return std::string((const char *)m_buffer, m_size - m_gap_size);
} }
void Text::compact() void Text::compact()
{ {
if ((m_gap_index < m_size - m_gap_size) && (m_gap_size > 0)) if ((m_gap_index < m_size - m_gap_size) && (m_gap_size > 0))
{ {
memmove(&m_buffer[m_gap_index], &m_buffer[m_gap_index + m_gap_size], m_size - m_gap_index); memmove(&m_buffer[m_gap_index], &m_buffer[m_gap_index + m_gap_size], m_size - m_gap_index);
m_gap_index = m_size - m_gap_size; m_gap_index = m_size - m_gap_size;
} }
}
} }

View File

@ -5,11 +5,9 @@
#include "Ref.h" #include "Ref.h"
#include <string> #include <string>
namespace jes class Text
{ {
class Text public:
{
public:
typedef char Character; typedef char Character;
Text(); Text();
Text(const uint8_t buf[], size_t size); Text(const uint8_t buf[], size_t size);
@ -22,15 +20,14 @@ namespace jes
std::string to_s(); std::string to_s();
protected: protected:
void compact(); void compact();
uint8_t * m_buffer; uint8_t * m_buffer;
size_t m_size; size_t m_size;
size_t m_gap_index; size_t m_gap_index;
size_t m_gap_size; size_t m_gap_size;
}; };
typedef Ref<Text> TextRef; typedef Ref<Text> TextRef;
}
#endif #endif