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
|
/*
* rtsystem.stdlib.cpp
*
* Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
* and Martin Trautmann (martintrautmann@gmx.de)
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
///// Stdlib implementation of System functions
#ifndef __LRT_SYSTEM_STDLIB__
#define __LRT_SYSTEM_STDLIB__
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include "rtsystem.h"
#include "rtfile.h"
namespace lrt {
Time::Time(const String& str) : sec(0), msec(0)
{
Array<String> parts = str.split(" :", "");
if(parts.length() != 7) // there should be 7 parts
return;
struct tm myTm;
myTm.tm_isdst = -1;
myTm.tm_mon = getMonthNum(parts[1]);
myTm.tm_mday = parts[2].intValue(1);
myTm.tm_hour = parts[3].intValue(0);
myTm.tm_min = parts[4].intValue(0);
myTm.tm_sec = parts[5].intValue(0);
myTm.tm_year = parts[6].intValue(1970) - 1900;
time_t myTime = mktime(&myTm);
sec = (int)myTime;
}
Time Time::getCurrentTime()
{
Time ret;
struct timeb tp;
ftime( &tp );
ret.sec = tp.time;
ret.msec = tp.millitm;
return ret;
}
String Time::toString()
{
time_t tm = (long)sec;
return String(ctime(&tm));
}
void System::message(const String& str)
{
println(str);
}
void System::exit(int errnum)
{
finalize();
::exit(errnum);
}
void System::exit(int errnum, const String &errmsg)
{
finalize();
::printf("%s\n", errmsg.cStr());
::fflush(stdout);
::exit(errnum);
}
// my signal handler - calls the user function instead
void mySignalHandler(int sig)
{
if(!lrt::System::lastBreakFunction) // no user func installed
return;
lrt::System::BreakType type = lrt::System::CTRL_C;
if(sig == SIGTERM)
type = lrt::System::TERMINATE;
// actually call user function
lrt::System::lastBreakFunction(type);
}
void System::setOnBreak(System::BreakFunction func, bool catchKill)
{
if(lastBreakFunction) { // remove my signal handler, unload the function
lastBreakFunction = 0;
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
}
if(func) { // add my new signal handler
lastBreakFunction = func;
signal(SIGINT, mySignalHandler);
if(catchKill)
signal(SIGTERM, mySignalHandler);
}
}
void System::initialize()
{
init = true;
}
void System::print(const char *cStr)
{
if(!init) initialize();
fputs(cStr, stdout);
}
void System::print(const String &str)
{
print((const char *)str.cStr());
}
void System::println()
{
if(!init) initialize();
fputs("\n", stdout);
fflush(stdout);
}
void System::println(const String &str)
{
if(!init) initialize();
fputs(str.cStr(), stdout);
fputs("\n", stdout);
fflush(stdout);
}
char System::read()
{
if(!init) initialize();
return (char) getchar();
}
String System::readLine(const String& query)
{
if(!init) initialize();
if(query.length() > 0) print(query);
fflush(stdout);
String ret;
char ch = 0;
while(true)
{
fread(&ch, 1, 1, stdin);
if(ch == '\n') break;
ret += ch;
}
return ret;
}
void System::setPos(int x, int y)
{
#ifdef __UNIX__ // Using Unix, we can assume that terminal emulation is supported.
::printf("\033[%d;%dH", y, x);
#endif
}
void System::copyMemory(const void* src, void* dest, int length)
{
#ifndef __NOCHECK__
if(length < 0)
System::exit(-1, "length < 0 in System::copyMemory()");
#endif
::memcpy(dest, src, length);
}
void System::fillMemory(void* ptr, int length, char data)
{
#ifndef __NOCHECK__
if(length < 0)
System::exit(-1, "length < 0 in System::fillMemory()");
#endif
::memset(ptr, data, length);
}
void System::finalize()
{
File::finalize();
init = false;
}
} // namespace
#endif
|