summaryrefslogtreecommitdiff
path: root/library/globalFunctions.cpp
blob: 7c4a1b92d1a045f5924ebca21fe06f9fc14292a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "globalFunctions.h"
#include "resources.h"
#include <wx/msgdlg.h>
#include <wx/file.h>
#include <fstream>


std::string globalFunctions::numberToString(const unsigned int number)
{
    char result[100];
    sprintf(result, "%u", number);
    return std::string(result);
}


std::string globalFunctions::numberToString(const int number)
{
    char result[100];
    sprintf(result, "%d", number);
    return std::string(result);
}


std::string globalFunctions::numberToString(const long number)
{
    char result[100];
    sprintf(result, "%ld", number);
    return std::string(result);
}


std::string globalFunctions::numberToString(const float number)
{
    char result[100];
    sprintf(result, "%f", number);
    return std::string(result);
}


wxString globalFunctions::numberToWxString(const unsigned int number)
{
    return wxString::Format(wxT("%u"), number);
}


wxString globalFunctions::numberToWxString(const int number)
{
    return wxString::Format(wxT("%i"), number);
}


wxString globalFunctions::numberToWxString(const float number)
{
    return wxString::Format(wxT("%f"), number);
}


int globalFunctions::stringToInt(const std::string& number)
{
    return atoi(number.c_str());
}


long globalFunctions::stringToLong(const std::string& number)
{
    return atol(number.c_str());
}


double globalFunctions::stringToDouble(const std::string& number)
{
    return atof(number.c_str());
}


int globalFunctions::wxStringToInt(const wxString& number)
{
    long result = 0;
    if (number.ToLong(&result))
        return result;
    else
        throw RuntimeException(wxString(_("Conversion error:")) + wxT(" wxString -> long"));
}


double globalFunctions::wxStringToDouble(const wxString& number)
{
    double result = 0;
    if (number.ToDouble(&result))
        return result;
    else
        throw RuntimeException(wxString(_("Conversion error:")) + wxT(" wxString -> double"));
}


wxString globalFunctions::includeNumberSeparator(const wxString& number)
{
    wxString output(number);
    for (int i = output.size() - 3; i > 0; i -= 3)
        output.insert(i, GlobalResources::THOUSANDS_SEPARATOR);
    return output;
}


int globalFunctions::readInt(std::ifstream& stream)
{
    int result = 0;
    char* buffer = reinterpret_cast<char*>(&result);
    stream.read(buffer, sizeof(int));
    return result;
}


void globalFunctions::writeInt(std::ofstream& stream, const int number)
{
    const char* buffer = reinterpret_cast<const char*>(&number);
    stream.write(buffer, sizeof(int));
}


int globalFunctions::readInt(wxInputStream& stream)
{
    int result = 0;
    char* buffer = reinterpret_cast<char*>(&result);
    stream.Read(buffer, sizeof(int));
    return result;
}


void globalFunctions::writeInt(wxOutputStream& stream, const int number)
{
    const char* buffer = reinterpret_cast<const char*>(&number);
    stream.Write(buffer, sizeof(int));
}


//############################################################################
Performance::Performance() :
        resultWasShown(false)
{
    timer.Start();
}


Performance::~Performance()
{
    if (!resultWasShown)
        showResult();
}


void Performance::showResult()
{
    resultWasShown = true;
    wxMessageBox(globalFunctions::numberToWxString(unsigned(timer.Time())) + wxT(" ms"));
    timer.Start(); //reset timer
}


//############################################################################
DebugLog::DebugLog() :
        lineCount(0),
        logFile(NULL)
{
    logFile = new wxFile;
    logfileName = assembleFileName();
    logFile->Open(logfileName.c_str(), wxFile::write);
}


DebugLog::~DebugLog()
{
    delete logFile; //automatically closes file handle
}


wxString DebugLog::assembleFileName()
{
    wxString tmp = wxDateTime::Now().FormatISOTime();
    tmp.Replace(wxT(":"), wxEmptyString);
    return wxString(wxT("DEBUG_")) + wxDateTime::Now().FormatISODate() + wxChar('_') + tmp + wxT(".log");
}


void DebugLog::write(const wxString& logText)
{
    ++lineCount;
    if (lineCount % 50000 == 0) //prevent logfile from becoming too big
    {
        logFile->Close();
        wxRemoveFile(logfileName);

        logfileName = assembleFileName();
        logFile->Open(logfileName.c_str(), wxFile::write);
    }

    logFile->Write(wxString(wxT("[")) + wxDateTime::Now().FormatTime() + wxT("] "));
    logFile->Write(logText + wxChar('\n'));
}

//DebugLog logDebugInfo;


wxString getCodeLocation(const wxString file, const int line)
{
    return wxString(file).AfterLast(GlobalResources::FILE_NAME_SEPARATOR) + wxT(", LINE ") + globalFunctions::numberToWxString(line) + wxT(" | ");
}
bgstack15