From 9dbfa4d550ddd80d0a54fb1e7cc5da217b88ff2f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 4 Jan 2018 13:17:55 +0100 Subject: [PATCH] Use std::string in a lot of places. Makes a few things simpler. --- doc/tutorial/13_shader_builder.cpp | 2 +- src/audio/sample.cpp | 10 +-- src/image/codec/android-image.cpp | 20 ++--- src/image/codec/dummy-image.cpp | 16 ++-- src/image/codec/gdiplus-image.cpp | 53 ++++++------- src/image/codec/imlib2-image.cpp | 24 +++--- src/image/codec/ios-image.cpp | 18 +++-- src/image/codec/oric-image.cpp | 20 ++--- src/image/codec/sdl-image.cpp | 22 +++--- src/image/codec/zed-image.cpp | 10 +-- src/image/codec/zed-palette-image.cpp | 10 +-- src/image/image.cpp | 6 +- src/image/movie.cpp | 16 ++-- src/image/resource-private.h | 8 +- src/image/resource.cpp | 14 ++-- src/lol/image/image.h | 10 ++- src/lol/image/movie.h | 4 +- src/lol/image/resource.h | 6 +- src/lol/sys/file.h | 30 ++++---- src/lol/sys/init.h | 27 ++++--- src/lol/sys/threadtypes.h | 16 ++-- src/lolua/baselua.cpp | 10 +-- src/sys/file.cpp | 102 +++++++++++++------------- src/sys/init.cpp | 52 ++++++------- src/sys/threadtypes.cpp | 51 +++++++------ 25 files changed, 282 insertions(+), 275 deletions(-) diff --git a/doc/tutorial/13_shader_builder.cpp b/doc/tutorial/13_shader_builder.cpp index 88da91d1..e484c6d7 100644 --- a/doc/tutorial/13_shader_builder.cpp +++ b/doc/tutorial/13_shader_builder.cpp @@ -110,7 +110,7 @@ public: builder.Build(code); - file.WriteString(code.c_str()); + file.Write(code); //code = file.ReadString(); file.Close(); diff --git a/src/audio/sample.cpp b/src/audio/sample.cpp index d2fe62d6..a3e803d2 100644 --- a/src/audio/sample.cpp +++ b/src/audio/sample.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -41,7 +41,7 @@ class sample_data friend class sample; private: - String m_name; + std::string m_name; #if defined LOL_USE_SDL_MIXER Mix_Chunk *m_chunk; int m_channel; @@ -55,12 +55,12 @@ private: sample::sample(char const *path) : data(new sample_data()) { - data->m_name = String(" ") + path; + data->m_name = std::string(" ") + path; #if defined LOL_USE_SDL_MIXER for (auto candidate : sys::get_path_list(path)) { - data->m_chunk = Mix_LoadWAV(candidate.C()); + data->m_chunk = Mix_LoadWAV(candidate.c_str()); if (data->m_chunk) break; } @@ -88,7 +88,7 @@ void sample::TickGame(float seconds) char const *sample::GetName() { - return data->m_name.C(); + return data->m_name.c_str(); } void sample::play() diff --git a/src/image/codec/android-image.cpp b/src/image/codec/android-image.cpp index 0364b23b..798d1dbb 100644 --- a/src/image/codec/android-image.cpp +++ b/src/image/codec/android-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ #if defined __ANDROID__ +#include + #include #include @@ -35,9 +37,9 @@ extern ANativeActivity *g_activity; class AndroidImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); virtual bool Close(); virtual uint8_t *GetData() const; @@ -50,7 +52,7 @@ private: DECLARE_IMAGE_CODEC(AndroidImageCodec, 100) -ResourceCodecData* AndroidImageCodec::Load(char const *path) +ResourceCodecData* AndroidImageCodec::Load(std::string const &path) { JNIEnv *env; jint res = g_activity->vm->GetEnv((void **)&env, JNI_VERSION_1_2); @@ -64,7 +66,7 @@ ResourceCodecData* AndroidImageCodec::Load(char const *path) if (res < 0) { #if !LOL_BUILD_RELEASE - msg::error("JVM environment not found, cannot open image %s\n", path); + msg::error("JVM environment not found, cannot open image %s\n", path.c_str()); #endif return false; } @@ -73,13 +75,13 @@ ResourceCodecData* AndroidImageCodec::Load(char const *path) mid = env->GetMethodID(cls, "openImage", "(Ljava/lang/String;)Landroid/graphics/Bitmap;"); - jstring name = env->NewStringUTF(path); + jstring name = env->NewStringUTF(path.c_str()); m_bmp = env->CallObjectMethod(g_activity->clazz, mid, name); env->DeleteLocalRef(name); if (!m_bmp) { #if !LOL_BUILD_RELEASE - msg::error("could not load %s\n", path); + msg::error("could not load %s\n", path.c_str()); #endif return false; } @@ -109,7 +111,7 @@ ResourceCodecData* AndroidImageCodec::Load(char const *path) return new ResourceCodecData(); } -bool AndroidImageCodec::Save(char const *path, ResourceCodecData* data) +bool AndroidImageCodec::Save(std::string const &path, ResourceCodecData* data) { UNUSED(path, data); diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index bf1f6271..7885b390 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +12,8 @@ #include +#include + #include "../../image/resource-private.h" namespace lol @@ -24,9 +26,9 @@ namespace lol class DummyImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; //Priority 0 because it's supposed to be the last one @@ -36,9 +38,9 @@ DECLARE_IMAGE_CODEC(DummyImageCodec, 0) * Public Image class */ -ResourceCodecData* DummyImageCodec::Load(char const *path) +ResourceCodecData* DummyImageCodec::Load(std::string const &path) { - if (strcmp("DUMMY", path)) + if (path == "DUMMY") return nullptr; auto data = new ResourceImageData(new image(ivec2(256))); @@ -58,7 +60,7 @@ ResourceCodecData* DummyImageCodec::Load(char const *path) return data; } -bool DummyImageCodec::Save(char const *path, ResourceCodecData* data) +bool DummyImageCodec::Save(std::string const &path, ResourceCodecData* data) { UNUSED(path, data); diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index a4d94878..8989bc3d 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -10,21 +10,16 @@ // See http://www.wtfpl.net/ for more details. // -#if HAVE_CONFIG_H -# include "config.h" -#endif +#include + #if LOL_USE_GDIPLUS +# include # include using std::min; using std::max; # include # include // for DEFINE_GUID # include -#endif - -#include - -#if LOL_USE_GDIPLUS #include "../../image/resource-private.h" @@ -38,9 +33,9 @@ namespace lol class GdiPlusImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(GdiPlusImageCodec, 100) @@ -49,7 +44,7 @@ DECLARE_IMAGE_CODEC(GdiPlusImageCodec, 100) * Public Image class */ -ResourceCodecData* GdiPlusImageCodec::Load(char const *path) +ResourceCodecData* GdiPlusImageCodec::Load(std::string const &path) { Gdiplus::Status status; ULONG_PTR token; @@ -61,16 +56,16 @@ ResourceCodecData* GdiPlusImageCodec::Load(char const *path) return nullptr; } - array pathlist = sys::get_path_list(path); + array pathlist = sys::get_path_list(path); Gdiplus::Bitmap *bitmap = nullptr; - for (auto fullpath : pathlist) + for (auto const &fullpath : pathlist) { size_t len; - len = mbstowcs(nullptr, fullpath.C(), 0); + len = mbstowcs(nullptr, fullpath.c_str(), 0); wchar_t *wpath = new wchar_t[len + 1]; - if (mbstowcs(wpath, fullpath.C(), len + 1) == (size_t)-1) + if (mbstowcs(wpath, fullpath.c_str(), len + 1) == (size_t)-1) { - msg::error("invalid image name %s\n", fullpath.C()); + msg::error("invalid image name %s\n", fullpath.c_str()); delete[] wpath; continue; } @@ -85,7 +80,7 @@ ResourceCodecData* GdiPlusImageCodec::Load(char const *path) { if (status != Gdiplus::InvalidParameter) msg::error("error %d loading %s\n", - status, fullpath.C()); + status, fullpath.c_str()); delete bitmap; bitmap = nullptr; } @@ -130,7 +125,7 @@ ResourceCodecData* GdiPlusImageCodec::Load(char const *path) return data; } -bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) +bool GdiPlusImageCodec::Save(std::string const &path, ResourceCodecData* data) { auto data_image = dynamic_cast(data); if (data_image == nullptr) @@ -141,15 +136,15 @@ bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) Gdiplus::GdiplusStartup(&token, &input, nullptr); wchar_t const *fmt; - if (strstr(path, ".gif")) + if (ends_with(path, ".gif")) fmt = L"image/gif"; - else if (strstr(path, ".jpg") || strstr(path, ".jpeg")) + else if (ends_with(path, ".jpg") || ends_with(path, ".jpeg")) fmt = L"image/jpeg"; - else if (strstr(path, ".png")) + else if (ends_with(path, ".png")) fmt = L"image/png"; - else if (strstr(path, ".tiff")) + else if (ends_with(path, ".tiff")) fmt = L"image/tiff"; - else /* if (strstr(path, ".bmp")) */ + else /* if (ends_with(path, ".bmp")) */ fmt = L"image/bmp"; unsigned int num = 0, encoder_size = 0; @@ -173,11 +168,11 @@ bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) } size_t len; - len = mbstowcs(nullptr, path, 0); + len = mbstowcs(nullptr, path.c_str(), 0); wchar_t *wpath = new wchar_t[len + 1]; - if (mbstowcs(wpath, path, len + 1) == (size_t)-1) + if (mbstowcs(wpath, path.c_str(), len + 1) == (size_t)-1) { - msg::error("could not convert GDI+ filename '%s' to widechar\n", path); + msg::error("could not convert GDI+ filename '%s' to widechar\n", path.c_str()); delete[] wpath; delete[] codecs; return false; @@ -212,7 +207,7 @@ bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) if (b->Save(wpath, &clsid, nullptr) != Gdiplus::Ok) { - msg::error("could not save GDI+ image '%s'\n", path); + msg::error("could not save GDI+ image '%s'\n", path.c_str()); delete[] wpath; delete[] codecs; delete b; diff --git a/src/image/codec/imlib2-image.cpp b/src/image/codec/imlib2-image.cpp index d83f145c..437cd490 100644 --- a/src/image/codec/imlib2-image.cpp +++ b/src/image/codec/imlib2-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ #if defined LOL_USE_IMLIB2 +#include + #include /* Check that the Imlib2 types are safe */ @@ -34,21 +36,21 @@ namespace lol class Imlib2ImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; /* Set priority higher than SDL because we can save in many formats. */ DECLARE_IMAGE_CODEC(Imlib2ImageCodec, 70) -ResourceCodecData *Imlib2ImageCodec::Load(char const *path) +ResourceCodecData *Imlib2ImageCodec::Load(std::string const &path) { Imlib_Image im = nullptr; - for (auto candidate : sys::get_path_list(path)) + for (auto const &candidate : sys::get_path_list(path)) { - im = imlib_load_image(candidate.C()); + im = imlib_load_image(candidate.c_str()); if (im) break; } @@ -56,7 +58,7 @@ ResourceCodecData *Imlib2ImageCodec::Load(char const *path) if (!im) { #if !LOL_BUILD_RELEASE - msg::error("could not load image %s\n", path); + msg::error("could not load image %s\n", path.c_str()); #endif return nullptr; } @@ -68,7 +70,7 @@ ResourceCodecData *Imlib2ImageCodec::Load(char const *path) { imlib_free_image(); #if !LOL_BUILD_RELEASE - msg::error("could not get image data for %s\n", path); + msg::error("could not get image data for %s\n", path.c_str()); #endif return nullptr; } @@ -93,7 +95,7 @@ ResourceCodecData *Imlib2ImageCodec::Load(char const *path) return data; } -bool Imlib2ImageCodec::Save(char const *path, ResourceCodecData *data) +bool Imlib2ImageCodec::Save(std::string const &path, ResourceCodecData *data) { auto data_image = dynamic_cast(data); if (data_image == nullptr) @@ -120,7 +122,7 @@ bool Imlib2ImageCodec::Save(char const *path, ResourceCodecData *data) imlib_image_put_back_data((DATA32 *)dstdata); image->unlock(srcdata); - imlib_save_image(path); + imlib_save_image(path.c_str()); imlib_free_image(); return true; diff --git a/src/image/codec/ios-image.cpp b/src/image/codec/ios-image.cpp index 33368308..18d3b6a2 100644 --- a/src/image/codec/ios-image.cpp +++ b/src/image/codec/ios-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ #if defined __APPLE__ && defined __MACH__ && defined __arm__ +#include + #import #include "../../image/resource-private.h" @@ -28,9 +30,9 @@ namespace lol class IosImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(IosImageCodec, 100) @@ -39,9 +41,9 @@ DECLARE_IMAGE_CODEC(IosImageCodec, 100) * Public Image class */ -ResourceCodecData* IosImageCodec::Load(char const *path) +ResourceCodecData* IosImageCodec::Load(std::string const &path) { - NSString *fullpath = [NSString stringWithUTF8String:path]; + NSString *fullpath = [NSString stringWithUTF8String:path.c_str()]; NSArray *chunks = [fullpath componentsSeparatedByString: @"/"]; NSString *filename = [chunks objectAtIndex: [chunks count] - 1]; chunks = [filename componentsSeparatedByString: @"."]; @@ -52,7 +54,7 @@ ResourceCodecData* IosImageCodec::Load(char const *path) if (!image) { #if !LOL_BUILD_RELEASE - msg::error("could not load %s\n", path); + msg::error("could not load %s\n", path.c_str()); #endif return nullptr; } @@ -78,7 +80,7 @@ ResourceCodecData* IosImageCodec::Load(char const *path) return new ResourceCodecData(); } -bool IosImageCodec::Save(char const *path, ResourceCodecData* data) +bool IosImageCodec::Save(std::string const &path, ResourceCodecData* data) { UNUSED(path, data); diff --git a/src/image/codec/oric-image.cpp b/src/image/codec/oric-image.cpp index d4120771..97a3abf9 100644 --- a/src/image/codec/oric-image.cpp +++ b/src/image/codec/oric-image.cpp @@ -33,12 +33,12 @@ namespace lol class OricImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); private: - static std::string ReadScreen(char const *name); + static std::string ReadScreen(std::string const &name); static void WriteScreen(image &image, array &result); }; @@ -48,7 +48,7 @@ DECLARE_IMAGE_CODEC(OricImageCodec, 100) * Public Image class */ -ResourceCodecData* OricImageCodec::Load(char const *path) +ResourceCodecData* OricImageCodec::Load(std::string const &path) { static u8vec4 const pal[8] = { @@ -109,13 +109,13 @@ ResourceCodecData* OricImageCodec::Load(char const *path) return data; } -bool OricImageCodec::Save(char const *path, ResourceCodecData* data) +bool OricImageCodec::Save(std::string const &path, ResourceCodecData* data) { auto data_image = dynamic_cast(data); if (data_image == nullptr) return false; - int len = (int)strlen(path); + int len = (int)path.length(); if (len < 4 || path[len - 4] != '.' || std::toupper(path[len - 3]) != 'T' || std::toupper(path[len - 2]) != 'A' @@ -128,7 +128,7 @@ bool OricImageCodec::Save(char const *path, ResourceCodecData* data) result << 0 << 0xff << 0x80 << 0 << 0xbf << 0x3f << 0xa0 << 0; /* Add filename, except the last 4 characters */ - for (char const *name = path; name[4]; ++name) + for (char const *name = path.c_str(); name[4]; ++name) result << (uint8_t)name[0]; result << 0; @@ -153,11 +153,11 @@ bool OricImageCodec::Save(char const *path, ResourceCodecData* data) return true; } -std::string OricImageCodec::ReadScreen(char const *name) +std::string OricImageCodec::ReadScreen(std::string const &name) { File f; f.Open(name, FileAccess::Read); - std::string data = f.ReadString().C(); + std::string data = f.ReadString(); f.Close(); /* Skip the sync bytes */ diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index e24e6dcb..f8f46ac3 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -25,6 +25,8 @@ # include #endif +#include + #include "../../image/resource-private.h" namespace lol @@ -37,22 +39,22 @@ namespace lol class SdlImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); static SDL_Surface *Create32BppSurface(ivec2 size); }; DECLARE_IMAGE_CODEC(SdlImageCodec, 50) -ResourceCodecData* SdlImageCodec::Load(char const *path) +ResourceCodecData* SdlImageCodec::Load(std::string const &path) { SDL_Surface *surface = nullptr; - for (auto candidate : sys::get_path_list(path)) + for (auto const &candidate : sys::get_path_list(path)) { - surface = IMG_Load(candidate.C()); + surface = IMG_Load(candidate.c_str()); if (surface) break; } @@ -60,7 +62,7 @@ ResourceCodecData* SdlImageCodec::Load(char const *path) if (!surface) { #if !LOL_BUILD_RELEASE - msg::error("could not load image %s\n", path); + msg::error("could not load image %s\n", path.c_str()); #endif return nullptr; } @@ -86,7 +88,7 @@ ResourceCodecData* SdlImageCodec::Load(char const *path) return data; } -bool SdlImageCodec::Save(char const *path, ResourceCodecData* data) +bool SdlImageCodec::Save(std::string const &path, ResourceCodecData* data) { auto data_image = dynamic_cast(data); if (data_image == nullptr) @@ -100,7 +102,7 @@ bool SdlImageCodec::Save(char const *path, ResourceCodecData* data) memcpy(surface->pixels, pixel_data, 4 * size.x * size.y); image->unlock(pixel_data); - int ret = SDL_SaveBMP(surface, path); + int ret = SDL_SaveBMP(surface, path.c_str()); SDL_FreeSurface(surface); return ret == 0; diff --git a/src/image/codec/zed-image.cpp b/src/image/codec/zed-image.cpp index 9f05c27b..1192a6d9 100644 --- a/src/image/codec/zed-image.cpp +++ b/src/image/codec/zed-image.cpp @@ -27,9 +27,9 @@ namespace lol class ZedImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(ZedImageCodec, 10) @@ -38,7 +38,7 @@ DECLARE_IMAGE_CODEC(ZedImageCodec, 10) * Public Image class */ -ResourceCodecData* ZedImageCodec::Load(char const *path) +ResourceCodecData* ZedImageCodec::Load(std::string const &path) { if (!ends_with(path, ".RSC")) return nullptr; @@ -295,7 +295,7 @@ ResourceCodecData* ZedImageCodec::Load(char const *path) return data; } -bool ZedImageCodec::Save(char const *path, ResourceCodecData* data) +bool ZedImageCodec::Save(std::string const &path, ResourceCodecData* data) { UNUSED(path, data); /* FIXME: do we need to implement this? */ diff --git a/src/image/codec/zed-palette-image.cpp b/src/image/codec/zed-palette-image.cpp index 79793334..2d724015 100644 --- a/src/image/codec/zed-palette-image.cpp +++ b/src/image/codec/zed-palette-image.cpp @@ -27,9 +27,9 @@ namespace lol class ZedPaletteImageCodec : public ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path); - virtual bool Save(char const *path, ResourceCodecData* data); + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path); + virtual bool Save(std::string const &path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10) @@ -38,7 +38,7 @@ DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10) * Public Image class */ -ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) +ResourceCodecData* ZedPaletteImageCodec::Load(std::string const &path) { if (!ends_with(path, ".pal")) return nullptr; @@ -83,7 +83,7 @@ ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) return data; } -bool ZedPaletteImageCodec::Save(char const *path, ResourceCodecData* data) +bool ZedPaletteImageCodec::Save(std::string const &path, ResourceCodecData* data) { UNUSED(path, data); /* FIXME: do we need to implement this? */ diff --git a/src/image/image.cpp b/src/image/image.cpp index 69d013bc..f9501b09 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -28,7 +28,7 @@ image::image() { } -image::image(char const *path) +image::image(std::string const &path) : m_data(new image_data()) { load(path); @@ -91,7 +91,7 @@ void image::DummyFill() load("DUMMY"); } -bool image::load(char const *path) +bool image::load(std::string const &path) { auto resource = ResourceLoader::Load(path); if (resource == nullptr) @@ -109,7 +109,7 @@ bool image::load(char const *path) return true; } -bool image::save(char const *path) +bool image::save(std::string const &path) { auto data = new ResourceImageData(new image(*this)); auto result = ResourceLoader::Save(path, data); diff --git a/src/image/movie.cpp b/src/image/movie.cpp index e148ce62..7b96a330 100644 --- a/src/image/movie.cpp +++ b/src/image/movie.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -29,12 +29,12 @@ namespace lol { #if LOL_USE_FFMPEG -#define ERROR_TO_STRING(errnum) (error2string((errnum)).C()) -static String error2string(int errnum) +#define ERROR_TO_STRING(errnum) (error2string((errnum)).c_str()) +static std::string error2string(int errnum) { char tmp[AV_ERROR_MAX_STRING_SIZE]; av_strerror(errnum, tmp, AV_ERROR_MAX_STRING_SIZE); - return String(tmp); + return std::string(tmp); } /*static void ffmpeg_logger(void *ptr, int level, const char *fmt, va_list vl) @@ -69,11 +69,11 @@ movie::movie(ivec2 size) #endif } -bool movie::open_file(char const *filename) +bool movie::open_file(std::string const &filename) { #if LOL_USE_FFMPEG /* Third argument specifies format */ - avformat_alloc_output_context2(&m_avformat, nullptr, "gif", filename); + avformat_alloc_output_context2(&m_avformat, nullptr, "gif", filename.c_str()); if (!m_avformat) { msg::debug("could not create output context"); @@ -85,10 +85,10 @@ bool movie::open_file(char const *filename) if (!(m_avformat->oformat->flags & AVFMT_NOFILE)) { - int ret = avio_open(&m_avformat->pb, filename, AVIO_FLAG_WRITE); + int ret = avio_open(&m_avformat->pb, filename.c_str(), AVIO_FLAG_WRITE); if (ret < 0) { - msg::error("could not open '%s': %s\n", filename, ERROR_TO_STRING(ret)); + msg::error("could not open '%s': %s\n", filename.c_str(), ERROR_TO_STRING(ret)); return false; } } diff --git a/src/image/resource-private.h b/src/image/resource-private.h index 2b855c99..616fdb0a 100644 --- a/src/image/resource-private.h +++ b/src/image/resource-private.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2016—2017 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -24,9 +24,9 @@ namespace lol class ResourceCodec { public: - virtual char const *GetName() { return ""; } - virtual ResourceCodecData* Load(char const *path) = 0; - virtual bool Save(char const *path, ResourceCodecData* data) = 0; + virtual std::string GetName() { return ""; } + virtual ResourceCodecData* Load(std::string const &path) = 0; + virtual bool Save(std::string const &path, ResourceCodecData* data) = 0; /* TODO: this should become more fine-grained */ int m_priority; diff --git a/src/image/resource.cpp b/src/image/resource.cpp index b3aad059..ab5ec34b 100644 --- a/src/image/resource.cpp +++ b/src/image/resource.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // Copyright © 2016—2017 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -79,7 +79,7 @@ g_resource_loader; * The public resource loader */ -ResourceCodecData* ResourceLoader::Load(char const *path) +ResourceCodecData* ResourceLoader::Load(std::string const &path) { ResourceCodec* last_codec = nullptr; for (auto codec : g_resource_loader.m_codecs) @@ -89,18 +89,18 @@ ResourceCodecData* ResourceLoader::Load(char const *path) if (data != nullptr) { msg::debug("image::load: codec %s succesfully loaded %s.\n", - codec->GetName(), path); + codec->GetName().c_str(), path.c_str()); return data; } } //Log error, because we shouldn't be here msg::error("image::load: last codec %s, error loading resource %s.\n", - last_codec->GetName(), path); + last_codec->GetName().c_str(), path.c_str()); return nullptr; } -bool ResourceLoader::Save(char const *path, ResourceCodecData* data) +bool ResourceLoader::Save(std::string const &path, ResourceCodecData* data) { ResourceCodec* last_codec = nullptr; for (auto codec : g_resource_loader.m_codecs) @@ -109,14 +109,14 @@ bool ResourceLoader::Save(char const *path, ResourceCodecData* data) if (codec->Save(path, data)) { msg::debug("image::save: codec %s succesfully saved %s.\n", - codec->GetName(), path); + codec->GetName().c_str(), path.c_str()); return true; } } //Log error, because we shouldn't be here msg::error("image::save: last codec %s, error saving resource %s.\n", - last_codec->GetName(), path); + last_codec->GetName().c_str(), path.c_str()); return false; } diff --git a/src/lol/image/image.h b/src/lol/image/image.h index c8276cf9..275cdd6b 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -22,6 +22,8 @@ #include #include +#include + namespace lol { @@ -66,7 +68,7 @@ public: image(ivec2 size); /* XXX: use of this ctor should be discouraged, as it will not * return information about a possible error. */ - image(char const *path); + image(std::string const &path); /* Rule of three */ image(image const &other); @@ -76,8 +78,8 @@ public: void DummyFill(); void Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt); void Copy(image const &other); - bool load(char const *path); - bool save(char const *path); + bool load(std::string const &path); + bool save(std::string const &path); /* Low level access */ ivec2 size() const; diff --git a/src/lol/image/movie.h b/src/lol/image/movie.h index e188999e..1aa5bb4e 100644 --- a/src/lol/image/movie.h +++ b/src/lol/image/movie.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -33,7 +33,7 @@ class movie public: movie(ivec2 size); - bool open_file(char const *filename); + bool open_file(std::string const &filename); bool push_image(image &im); void close(); diff --git a/src/lol/image/resource.h b/src/lol/image/resource.h index bb017600..a5f33998 100644 --- a/src/lol/image/resource.h +++ b/src/lol/image/resource.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2016—2017 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -66,8 +66,8 @@ namespace lol class ResourceLoader { public: - static ResourceCodecData* Load(char const *path); - static bool Save(char const *path, ResourceCodecData* data); + static ResourceCodecData* Load(std::string const &path); + static bool Save(std::string const &path, ResourceCodecData* data); }; } /* namespace lol */ diff --git a/src/lol/sys/file.h b/src/lol/sys/file.h index bbb51cfc..24989299 100644 --- a/src/lol/sys/file.h +++ b/src/lol/sys/file.h @@ -32,7 +32,7 @@ struct FileAccessBase : public StructSafeEnum Write }; protected: - virtual bool BuildEnumMap(std::map& enum_map) + virtual bool BuildEnumMap(std::map& enum_map) { enum_map[Read] = "Read"; enum_map[Write] = "Write"; @@ -53,7 +53,7 @@ struct StreamTypeBase : public StructSafeEnum FileBinary }; protected: - virtual bool BuildEnumMap(std::map& enum_map) + virtual bool BuildEnumMap(std::map& enum_map) { enum_map[StdIn] = "StdIn"; enum_map[StdOut] = "StdOut"; @@ -74,14 +74,14 @@ public: ~File(); void Open(StreamType stream); - void Open(String const &file, FileAccess mode, bool force_binary=false); + void Open(std::string const &file, FileAccess mode, bool force_binary=false); bool IsValid() const; void Close(); int Read(uint8_t *buf, int count); - String ReadString(); - int Write(uint8_t const *buf, int count); - int WriteString(const String &buf); + std::string ReadString(); + int Write(void const *buf, int count); + int Write(std::string const &buf); long int GetPosFromStart(); void SetPosFromStart(long int pos); long int size(); @@ -94,7 +94,7 @@ private: class Directory { public: - Directory(String const &name); + Directory(std::string const &name); Directory(Directory const &that); Directory &operator =(Directory const &that); ~Directory(); @@ -104,20 +104,20 @@ public: void Close(); private: - bool GetContent(array* files, array* directories); + bool GetContent(array* files, array* directories); public: - bool GetContent(array& files, array& directories); + bool GetContent(array& files, array& directories); bool GetContent(array& directories); - bool GetContent(array& files); - String GetName(); + bool GetContent(array& files); + std::string GetName(); long int GetModificationTime(); - static String GetCurrent(); - static bool SetCurrent(String directory); + static std::string GetCurrent(); + static bool SetCurrent(std::string directory); private: - class DirectoryData* m_data; - String m_name; + class DirectoryData* m_data; + std::string m_name; }; } /* namespace lol */ diff --git a/src/lol/sys/init.h b/src/lol/sys/init.h index 8a444c19..8571bb34 100644 --- a/src/lol/sys/init.h +++ b/src/lol/sys/init.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -15,7 +17,8 @@ // ------------------------------- // -#include +#include +#include namespace lol { @@ -46,12 +49,12 @@ namespace sys { extern void init(int argc, char *argv[], - String const &projectdir = LOL_CONFIG_PROJECTDIR, - String const &solutiondir = LOL_CONFIG_SOLUTIONDIR, - String const &sourcesubdir = LOL_CONFIG_SOURCESUBDIR); + std::string const &projectdir = LOL_CONFIG_PROJECTDIR, + std::string const &solutiondir = LOL_CONFIG_SOLUTIONDIR, + std::string const &sourcesubdir = LOL_CONFIG_SOURCESUBDIR); -extern void add_data_dir(String const &dir); -extern array get_path_list(String const &file); +extern void add_data_dir(std::string const &dir); +extern array get_path_list(std::string const &file); } /* namespace sys */ diff --git a/src/lol/sys/threadtypes.h b/src/lol/sys/threadtypes.h index 3638e8ab..bb213d01 100644 --- a/src/lol/sys/threadtypes.h +++ b/src/lol/sys/threadtypes.h @@ -37,7 +37,7 @@ struct ThreadStatusBase : public StructSafeEnum THREAD_STOPPED, }; protected: - virtual bool BuildEnumMap(std::map& enum_map) + virtual bool BuildEnumMap(std::map& enum_map) { enum_map[NOTHING] = "NOTHING"; enum_map[THREAD_STARTED] = "THREAD_STARTED"; @@ -59,7 +59,7 @@ struct ThreadJobTypeBase : public StructSafeEnum THREAD_STOP }; protected: - virtual bool BuildEnumMap(std::map& enum_map) + virtual bool BuildEnumMap(std::map& enum_map) { enum_map[NONE] = "NONE"; enum_map[WORK_TODO] = "WORK_TODO"; @@ -229,13 +229,13 @@ public: virtual ~FileUpdateTester(); //------------------------------------------------------------------------- - FileUpdateTester::Status* RegisterFile(String const& path); + FileUpdateTester::Status* RegisterFile(std::string const& path); protected: - void UnregisterFile(String const& path); + void UnregisterFile(std::string const& path); public: void UnregisterFile(FileUpdateTester::Status*& status); //TODO: Add directories - //FileUpdateTester::Status* RegisterDirectory(String const& path, bool check_files=false); + //FileUpdateTester::Status* RegisterDirectory(std::string const& path, bool check_files=false); virtual void TickGame(float seconds); virtual void TreatResult(ThreadJob* result); @@ -243,7 +243,7 @@ private: uint32_t m_frame_skip = 4; uint32_t m_frame_count = 0; array m_job_done; - std::map m_files; + std::map m_files; }; typedef FileUpdateTester::Status FileUpdateStatus; @@ -262,7 +262,7 @@ public: { } //Returns a dummy image, and start a job to load the image on a thread - image* Load(const lol::String& path); + image* Load(const std::string& path); bool CheckStatus(image* img); protected: @@ -270,7 +270,7 @@ protected: private: image m_dummy_image; - std::map m_images; + std::map m_images; array m_loaded_images; }; diff --git a/src/lolua/baselua.cpp b/src/lolua/baselua.cpp index 394ad3f7..4c48cb1e 100644 --- a/src/lolua/baselua.cpp +++ b/src/lolua/baselua.cpp @@ -60,26 +60,26 @@ class LuaBaseData return LUA_ERRFILE; auto stack = LuaStack::Begin(l); - char const *filename = stack.Get(); + std::string filename = stack.Get(); int status = LUA_ERRFILE; File f; - for (auto candidate : sys::get_path_list(filename)) + for (auto const &candidate : sys::get_path_list(filename)) { f.Open(candidate, FileAccess::Read); if (f.IsValid()) { - std::string s = f.ReadString().C(); + std::string s = f.ReadString(); f.Close(); - msg::debug("loading Lua file %s\n", candidate.C()); + msg::debug("loading Lua file %s\n", candidate.c_str()); status = LuaDoCode(l, s); break; } } if (status == LUA_ERRFILE) - msg::error("could not find Lua file %s\n", filename); + msg::error("could not find Lua file %s\n", filename.c_str()); else if (status == 1) { stack.SetIndex(-1); diff --git a/src/sys/file.cpp b/src/sys/file.cpp index 29a756e2..91221004 100644 --- a/src/sys/file.cpp +++ b/src/sys/file.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -30,6 +30,8 @@ #endif #include +#include +#include #include namespace lol @@ -68,18 +70,19 @@ class FileData } } - void Open(String const &file, FileAccess mode, bool force_binary) + void Open(std::string const &file, FileAccess mode, bool force_binary) { m_type = (force_binary) ? (StreamType::FileBinary) : (StreamType::File); #if __ANDROID__ ASSERT(g_assets); - m_asset = AAssetManager_open(g_assets, file.C(), AASSET_MODE_UNKNOWN); + m_asset = AAssetManager_open(g_assets, file.c_str(), AASSET_MODE_UNKNOWN); #elif HAVE_STDIO_H /* FIXME: no modes, no error checking, no nothing */ - stat(file.C(), &m_stat); - String access = (mode == FileAccess::Write) ? ("w") : ("r"); - if (force_binary) access += "b"; - m_fd = fopen(file.C(), access.C()); + stat(file.c_str(), &m_stat); + std::string access(mode == FileAccess::Write ? "w" : "r"); + if (force_binary) + access += "b"; + m_fd = fopen(file.c_str(), access.c_str()); #endif } @@ -125,11 +128,11 @@ class FileData #endif } - String ReadString() + std::string ReadString() { array buf; buf.resize(BUFSIZ); - String ret; + std::string ret; while (IsValid()) { int done = Read(&buf[0], buf.count()); @@ -137,7 +140,7 @@ class FileData if (done <= 0) break; - int oldsize = ret.count(); + int oldsize = ret.length(); ret.resize(oldsize + done); memcpy(&ret[oldsize], &buf[0], done); @@ -146,7 +149,7 @@ class FileData return ret; } - int Write(uint8_t const *buf, int count) + int Write(void const *buf, int count) { #if __ANDROID__ //return AAsset_read(m_asset, buf, count); @@ -162,11 +165,6 @@ class FileData #endif } - int WriteString(const String &buf) - { - return Write((uint8_t const *)buf.C(), buf.count()); - } - long int GetPosFromStart() { #if __ANDROID__ @@ -274,7 +272,7 @@ void File::Open(StreamType stream) } //-- -void File::Open(String const &file, FileAccess mode, bool force_binary) +void File::Open(std::string const &file, FileAccess mode, bool force_binary) { m_data->Open(file, mode, force_binary); } @@ -298,21 +296,21 @@ int File::Read(uint8_t *buf, int count) } //-- -String File::ReadString() +std::string File::ReadString() { return m_data->ReadString(); } //-- -int File::Write(uint8_t const *buf, int count) +int File::Write(void const *buf, int count) { return m_data->Write(buf, count); } //-- -int File::WriteString(const String &buf) +int File::Write(std::string const &buf) { - return m_data->WriteString(buf); + return m_data->Write(buf.c_str(), buf.length()); } //-- @@ -355,7 +353,7 @@ class DirectoryData #endif } - void Open(String const &directory, FileAccess mode) + void Open(std::string const &directory, FileAccess mode) { UNUSED(mode); /* FIXME */ @@ -364,14 +362,14 @@ class DirectoryData /* FIXME: not implemented */ #elif defined(_WIN32) m_directory = directory; - String filter = m_directory + String("*"); - filter.replace('/', '\\', true); + std::string filter = m_directory + "*"; + std::replace(filter.begin(), filter.end(), '/', '\\'); WIN32_FIND_DATA FindFileData; - m_handle = FindFirstFile(filter.C(), &FindFileData); - stat(directory.C(), &m_stat); + m_handle = FindFirstFile(filter.c_str(), &FindFileData); + stat(directory.c_str(), &m_stat); #elif HAVE_STDIO_H - m_dd = opendir(directory.C()); - stat(directory.C(), &m_stat); + m_dd = opendir(directory.c_str()); + stat(directory.c_str(), &m_stat); #endif } @@ -400,7 +398,7 @@ class DirectoryData #endif } - bool GetContentList(array* files, array* directories) + bool GetContentList(array* files, array* directories) { if (!IsValid()) return false; @@ -408,10 +406,10 @@ class DirectoryData #if __ANDROID__ /* FIXME: not implemented */ #elif defined(_WIN32) - String filter = m_directory + String("*"); - filter.replace('/', '\\', true); + std::string filter = m_directory + "*"; + std::replace(filter.begin(), filter.end(), '/', '\\'); WIN32_FIND_DATA find_data; - HANDLE handle = FindFirstFile(filter.C(), &find_data); + HANDLE handle = FindFirstFile(filter.c_str(), &find_data); bool file_valid = (handle != INVALID_HANDLE_VALUE); while (file_valid) @@ -422,12 +420,12 @@ class DirectoryData if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (directories) - *directories << String(std::string(find_data.cFileName).c_str()); + *directories << std::string(find_data.cFileName); } else { if (files) - *files << String(find_data.cFileName); + *files << std::string(find_data.cFileName); } } //Go for next one @@ -467,7 +465,7 @@ class DirectoryData /* FIXME: not implemented */ #elif defined(_WIN32) HANDLE m_handle; - String m_directory; + std::string m_directory; #elif HAVE_STDIO_H DIR *m_dd; #endif @@ -477,9 +475,9 @@ class DirectoryData }; //-- DIRECTORY -- -Directory::Directory(String const &name) +Directory::Directory(std::string const &name) : m_data(new DirectoryData), - m_name(name + String("/")) + m_name(name + "/") { ++m_data->m_refcount; } @@ -543,9 +541,9 @@ void Directory::Close() } //-- -bool Directory::GetContent(array* files, array* directories) +bool Directory::GetContent(array* files, array* directories) { - array sfiles, sdirectories; + array sfiles, sdirectories; bool found_some = m_data->GetContentList(&sfiles, &sdirectories); UNUSED(found_some); @@ -561,7 +559,7 @@ bool Directory::GetContent(array* files, array* directories) } //-- -bool Directory::GetContent(array& files, array& directories) +bool Directory::GetContent(array& files, array& directories) { return GetContent(&files, &directories); } @@ -573,13 +571,13 @@ bool Directory::GetContent(array& directories) } //-- -bool Directory::GetContent(array& files) +bool Directory::GetContent(array& files) { return GetContent(&files, nullptr); } //-- -String Directory::GetName() +std::string Directory::GetName() { return m_name; } @@ -591,33 +589,33 @@ long int Directory::GetModificationTime() } //-- -String Directory::GetCurrent() +std::string Directory::GetCurrent() { - String result; + std::string ret; #if __ANDROID__ /* FIXME: not implemented */ #elif defined(_WIN32) TCHAR buff[MAX_PATH * 2]; GetCurrentDirectory(MAX_PATH, buff); - result = buff; - result.replace('\\', '/', true); + ret = buff; + std::replace(ret.begin(), ret.end(), '\\', '/'); #elif HAVE_STDIO_H /* FIXME: not implemented */ #endif - return result; + return ret; } //-- -bool Directory::SetCurrent(String directory) +bool Directory::SetCurrent(std::string directory) { #if __ANDROID__ /* FIXME: not implemented */ #elif defined(_WIN32) - String result = directory; - result.replace('/', '\\', true); - return !!SetCurrentDirectory(result.C()); + std::string result = directory; + std::replace(result.begin(), result.end(), '/', '\\'); + return !!SetCurrentDirectory(result.c_str()); #elif HAVE_UNISTD_H - chdir(directory.C()); + chdir(directory.c_str()); #endif return false; } diff --git a/src/sys/init.cpp b/src/sys/init.cpp index 974aa9e1..9848da20 100644 --- a/src/sys/init.cpp +++ b/src/sys/init.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -36,18 +36,18 @@ namespace sys # define SEPARATOR '/' #endif -static array data_dir; +static array data_dir; void init(int argc, char *argv[], - String const &projectdir, - String const &solutiondir, - String const &sourcesubdir) + std::string const &projectdir, + std::string const &solutiondir, + std::string const &sourcesubdir) { using namespace std; - msg::debug("project dir: “%s”\n", projectdir.C()); - msg::debug("solution dir: “%s”\n", solutiondir.C()); - msg::debug("source subdir: “%s”\n", sourcesubdir.C()); + msg::debug("project dir: “%s”\n", projectdir.c_str()); + msg::debug("solution dir: “%s”\n", solutiondir.c_str()); + msg::debug("source subdir: “%s”\n", sourcesubdir.c_str()); /* * Retrieve binary directory, defaulting to no directory on Android @@ -55,9 +55,9 @@ void init(int argc, char *argv[], */ #if __ANDROID__ || EMSCRIPTEN - String binarydir = ""; + std::string binarydir = ""; #else - String binarydir = "."; + std::string binarydir = "."; char *cwd = nullptr; # if HAVE_GETCWD @@ -78,7 +78,7 @@ void init(int argc, char *argv[], { char const *last_sep = strrchr(argv[0], SEPARATOR); if (last_sep) - binarydir = String(argv[0], last_sep - argv[0] + 1); + binarydir = std::string(argv[0], last_sep - argv[0] + 1); } #endif @@ -89,31 +89,31 @@ void init(int argc, char *argv[], * add current directory in case we were launched from another place. */ - if (!got_rootdir && projectdir.count() && solutiondir.count()) + if (!got_rootdir && projectdir.length() && solutiondir.length()) { /* This data dir is for standalone executables */ - String rootdir = binarydir; - if (rootdir.count() && rootdir.last() != SEPARATOR) + std::string rootdir = binarydir; + if (rootdir.length() && rootdir.back() != SEPARATOR) rootdir += SEPARATOR; add_data_dir(rootdir); /* This data dir is for engine stuff */ rootdir = solutiondir; - if (rootdir.count() && rootdir.last() != SEPARATOR) + if (rootdir.length() && rootdir.back() != SEPARATOR) rootdir += SEPARATOR; rootdir += "../src/"; /* FIXME: use SEPARATOR? */ add_data_dir(rootdir); /* This data dir is for submodule support stuff */ rootdir = solutiondir; - if (rootdir.count() && rootdir.last() != SEPARATOR) + if (rootdir.length() && rootdir.back() != SEPARATOR) rootdir += SEPARATOR; rootdir += "./lol/src/"; /* FIXME: use SEPARATOR? */ add_data_dir(rootdir); /* This data dir is for project-specific stuff */ rootdir = projectdir; - if (rootdir.count() && rootdir.last() != SEPARATOR) + if (rootdir.length() && rootdir.back() != SEPARATOR) rootdir += SEPARATOR; add_data_dir(rootdir); @@ -128,14 +128,14 @@ void init(int argc, char *argv[], { /* First climb back the hierarchy to get to the engine root and * add a data dir for engine stuff. */ - String rootdir = binarydir; - if (rootdir.count() && rootdir.last() != SEPARATOR) + std::string rootdir = binarydir; + if (rootdir.length() && rootdir.back() != SEPARATOR) rootdir += SEPARATOR; - for (int i = 1; i < sourcesubdir.count(); ++i) + for (int i = 1; i < (int)sourcesubdir.length(); ++i) { if ((sourcesubdir[i] == SEPARATOR && sourcesubdir[i - 1] != SEPARATOR) - || i == sourcesubdir.count() - 1) + || i == (int)sourcesubdir.length() - 1) rootdir += "../"; } rootdir += "src/"; @@ -148,24 +148,24 @@ void init(int argc, char *argv[], got_rootdir = true; } - msg::debug("binary dir: “%s”\n", binarydir.C()); + msg::debug("binary dir: “%s”\n", binarydir.c_str()); for (int i = 0; i < data_dir.count(); ++i) msg::debug("data dir %d/%d: “%s”\n", i + 1, data_dir.count(), - data_dir[i].C()); + data_dir[i].c_str()); } /* * Data directory handling */ -void add_data_dir(String const &dir) +void add_data_dir(std::string const &dir) { data_dir << dir; } -array get_path_list(String const &file) +array get_path_list(std::string const &file) { - array ret; + array ret; /* If not an absolute path, look through known data directories */ if (file[0] != '/') diff --git a/src/sys/threadtypes.cpp b/src/sys/threadtypes.cpp index 9e55047b..446b6860 100644 --- a/src/sys/threadtypes.cpp +++ b/src/sys/threadtypes.cpp @@ -35,12 +35,12 @@ public: char const *GetName() { return ""; } FileUpdateTesterJob() : ThreadJob(ThreadJobType::NONE) { } - FileUpdateTesterJob(String path) + FileUpdateTesterJob(std::string const &path) : ThreadJob(ThreadJobType::WORK_TODO) { m_path = path; } - String& GetPath() { return m_path; } + std::string const &GetPath() { return m_path; } long int GetTime() { return m_time; } bool HasUpdated() { return m_updated; } void Restart() @@ -52,9 +52,9 @@ public: protected: virtual bool DoWork() { - array pathlist = sys::get_path_list(m_path); + array pathlist = sys::get_path_list(m_path); File f; - for (String path : pathlist) + for (auto const &path : pathlist) { f.Open(path, FileAccess::Read); if (f.IsValid()) @@ -77,10 +77,10 @@ protected: } //----------------- - bool m_ready = false; - String m_path = String(); - long int m_time = 0; - bool m_updated = false; + bool m_ready = false; + std::string m_path; + long int m_time = 0; + bool m_updated = false; }; //FileUpdateTester ------------------------------------------------------------ @@ -90,14 +90,14 @@ FileUpdateTester::~FileUpdateTester() } //File interface -------------------------------------------------------------- -FileUpdateTester::Status* FileUpdateTester::RegisterFile(String const& path) +FileUpdateTester::Status* FileUpdateTester::RegisterFile(std::string const& path) { DispatchJob(new FileUpdateTesterJob(path)); m_files[path] = new FileUpdateTester::Status(); return m_files[path]; } -void FileUpdateTester::UnregisterFile(String const& path) +void FileUpdateTester::UnregisterFile(std::string const& path) { ASSERT(has_key(m_files, path)); delete m_files[path]; @@ -107,8 +107,7 @@ void FileUpdateTester::UnregisterFile(String const& path) void FileUpdateTester::UnregisterFile(FileUpdateTester::Status*& status) { ASSERT(status); - array all_keys = keys(m_files); - for (String key : all_keys) + for (auto const &key : keys(m_files)) { if (m_files[key] == status) { @@ -123,10 +122,9 @@ void FileUpdateTester::UnregisterFile(FileUpdateTester::Status*& status) //----------------------------------------------------------------------------- void FileUpdateTester::TickGame(float seconds) { - //Reset update for this frame - array all_keys = keys(m_files); - for (String key : all_keys) - m_files[key]->SetUpdated(false); + // Reset update for this frame + for (auto &kv : m_files) + kv.second->SetUpdated(false); super::TickGame(seconds); @@ -160,30 +158,31 @@ class AsyncImageJob : public ThreadJob public: char const *GetName() { return ""; } AsyncImageJob() - : ThreadJob(ThreadJobType::NONE) + : ThreadJob(ThreadJobType::NONE) { - m_path = String(); } - AsyncImageJob(String path) - : ThreadJob(ThreadJobType::WORK_TODO) + + AsyncImageJob(std::string const &path) + : ThreadJob(ThreadJobType::WORK_TODO), + m_path(path) { - m_path = path; } - String const& GetPath() { return m_path; } + + std::string const& GetPath() { return m_path; } Image const& GetImage() { return m_image; } protected: virtual bool DoWork() { - return m_image.load(m_path.C()); + return m_image.load(m_path); } - String m_path; - Image m_image; + std::string m_path; + Image m_image; }; //----------------------------------------------------------------------------- -Image* AsyncImageLoader::Load(const lol::String& path) +Image* AsyncImageLoader::Load(std::string const &path) { //Create a job AsyncImageJob* job = new AsyncImageJob(path);