Ini File Class
-
Und was ist jetzt das Problem?
-
tenchou schrieb:
Und was ist jetzt das Problem?
Es gibt kein 's. Er präsentiert uns wohl einfach nur seine Arbeit

-
Tjo und die ist miserabel:
#if !defined(INI_FILE_H__INCLUDED) #define INI_FILE_H__INCLUDED #if (_MSC_VER >= 1300) #pragma once #endif // (_MSC_VER >= 1300) #include <windows.h> #include <cstdio> #include <string> #include <sstream> class IniFile { public: IniFile() : m_lpFilePath(NULL) {} IniFile(const char* const path) { set_file_path(path); } ~IniFile() { delete [] m_lpFilePath; } public: void set_file_path(const char* const path) { if (path != NULL) { delete [] m_lpFilePath; std::size_t len = std::strlen(path); m_lpFilePath = new char[len + 1]; std::strncpy(m_lpFilePath, path, len); if (file_exists() == false) throw std::runtime_error("invalid file path"); } } template<typename T> T read_value(const char* const lpSection, const char* const lpKey, const char* const lpDefault = "") const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); char buf[2048]; if (::GetPrivateProfileStringA(lpSection, lpKey, lpDefault, buf, 2047, m_lpFilePath) <= 0) throw std::runtime_error("data do not exist"); std::istringstream ss(buf); T tmp; ss >> tmp; if (!ss) throw std::runtime_error("invalid data type"); return tmp; } template<typename T> void write_value(const char* const lpSection, const char* const lpKey, T const& value) const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); std::ostringstream ss; ss << value; if (!ss) throw std::runtime_error("invalid data type"); if (::WritePrivateProfileStringA(lpSection, lpKey, ss.str().c_str(), m_lpFilePath) == FALSE) throw std::runtime_error("could not write data"); } void write_section(const char* const lpSection, const char* const lpValue) const { if (lpSection == NULL || lpKey == NULL) throw std::invalid_argument(""); if (file_exists() == false) throw std::runtime_error("file does not exist"); if (::WritePrivateProfileSectionA(lpSection, lpValue, m_lpFilePath) == FALSE) throw std::runtime_error("could not write data"); } private: bool file_exists() const { if (m_lpFilePath == NULL) return false; std::FILE* data = std::fopen(m_lpFilename, "r"); if (!data) return false; std::fclose(data); return true; } private: char* m_lpFilePath; }; #endif // INI_FILE_H__INCLUDED...
#include "inifile.h" #include <iostream> int main() { try { IniFile file("test.ini"); std::cout << file.read_value<unsigned int>("Beispiel", "Key A") << std::endl; } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } }
}
-
(D)Evil schrieb:
Tjo und die ist miserabel
Sowas finde ich unter aller Sau

-
Badestrand schrieb:
(D)Evil schrieb:
Tjo und die ist miserabel
Sowas finde ich unter aller Sau

Er meint doch seinen eigenen Kot.

-
Tja ist ja schön wenn du das so empfindest. Ok "miserabel" war etwas hart ausgedrückt ... jedenfalls beachtet er da einige Grundlagen nicht. Wenn er hier schon seine Klasse (fast) kommentarlos reinklatscht, sollte die wenigstens einigermaßen fehlerfrei sein.
-
Och ich find es schön, wenn man lokale variablen zurückgibt, das hat was von "hier kriegst du was, aber bitte benutze es nicht"
-
Hast schon recht, ich hatte einen schlechten Tag, tut mir Leid. Trotzdem, so eine Aussage wie "das ist miserabel" hätte man auch freundlicher gestalten können, z.B. auf Fehler hinweisen oder so
Nix für ungut!
-
GetCurrentDirectoryA würde ich in dem Zusammenhang auch nicht verwenden.
Aber harte Worte hier.....nicht nett. Aber sind ja immer die gleichen
die sowas sagen
-
Mir ist schlecht. Man bringe mir einen Küüübel