git repos / blockattack-game

commit b84f3eee

Poul Sander · 2016-06-10 16:55
b84f3eeea1ac9ee91dd70a3b6d0af0711f5b0616 patch · browse files
parent 93f080d7c68b981446360b72bff1f980257fd0b5

Removed an old C-style pointer and added a StrToLong function

Changed files

M source/code/sago/SagoMisc.cpp before
M source/code/sago/SagoMisc.hpp before
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<char[]> 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 */