diff --git a/source/code/sago/SagoMisc.cpp b/source/code/sago/SagoMisc.cpp index 581e750..f4a1235 100644 --- a/source/code/sago/SagoMisc.cpp +++ b/source/code/sago/SagoMisc.cpp @@ -58,19 +58,16 @@ std::string GetFileContent(const char* filename) { } PHYSFS_file* myfile = PHYSFS_openRead(filename); unsigned int m_size = PHYSFS_fileLength(myfile); - char* m_data = new char[m_size+1]; - int length_read = PHYSFS_read (myfile, m_data, 1, m_size); + std::unique_ptr m_data(new char[m_size+1]); + int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); if (length_read != (int)m_size) { - delete [] m_data; - m_data = nullptr; PHYSFS_close(myfile); cerr << "Error: Curropt data file: " << filename << "\n"; return ret; } PHYSFS_close(myfile); m_data[m_size] = 0; //ensure that we are 0 terminated - ret = m_data; - delete [] m_data; + ret = m_data.get(); return ret; } @@ -84,4 +81,9 @@ void WriteFileContent(const char* filename, const std::string& content) { PHYSFS_close(myfile); } +long int StrToLong(const char* c_string) { + auto ret = strtol(c_string, nullptr, 10); + return ret; +} + } diff --git a/source/code/sago/SagoMisc.hpp b/source/code/sago/SagoMisc.hpp index 3a8bf62..ec9b11b 100644 --- a/source/code/sago/SagoMisc.hpp +++ b/source/code/sago/SagoMisc.hpp @@ -50,6 +50,14 @@ namespace sago { void WriteFileContent(const char* filename, const std::string& content); + /** + * This functions convers a string on a best effort basis + * Unlike atol this does NOT cause undefined behavior if out of range + * @param c_string A string that may contain a number + * @return A number between LONG_MIN and LONG_MAX (both inclusive) + */ + long int StrToLong(const char* c_string); + } //namespace sago #endif /* SAGOMISC_HPP */