00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifdef WIN32
00023 #include <windows.h>
00024 #include <string.h>
00025 #include <io.h>
00026 #else
00027 #include <cstring>
00028 #include <cstdlib>
00029 #include <iostream>
00030 #include <sys/types.h>
00031 #include <sys/mman.h>
00032 #endif
00033
00034 #include "timer.h"
00035 #include "util.h"
00036
00037 using namespace std;
00038
00039 string gettempfolder()
00040 {
00041 #ifdef _WIN32
00042 char *tmpPath = getenv("TMP");
00043 string str(tmpPath);
00044 if (str.substr(str.size() - 1, 1) != "\\")
00045 str += "\\";
00046 return str;
00047 #else
00048 char *tmpPath = getenv("TMP");
00049 if (!tmpPath || !*tmpPath)
00050 return "/tmp/";
00051 string str(tmpPath);
00052 if (str.substr(str.size() - 1, 1) != "/")
00053 str += "/";
00054 return str;
00055 #endif
00056 }
00057
00058
00059 void createtempfile(ofstream &fileStream, string &filePath, std::ios_base::openmode flags)
00060 {
00061 #ifdef _WIN32
00062 char buffer[BUFSIZ];
00063 ::GetTempFileNameA(gettempfolder().c_str(), "", 0, buffer);
00064 filePath = buffer;
00065 #else
00066 string tmpfolder = gettempfolder();
00067 char buffer[tmpfolder.size() + 16];
00068 strcpy(buffer, tmpfolder.c_str());
00069 strcat(buffer, "dskbuff--XXXXXX");
00070 mkstemp(buffer);
00071 filePath = buffer;
00072 #endif
00073 fileStream.open(filePath.c_str(), flags);
00074 }
00075
00076 void removefile(const std::string &filePath)
00077 {
00078 #ifdef _WIN32
00079 ::DeleteFileA(filePath.c_str());
00080 #else
00081 char cmd[filePath.size() + 100];
00082 sprintf(cmd,"rm %s",filePath.c_str());
00083 system(cmd);
00084 #endif
00085 }
00086
00087
00088
00089 inputfilestream::inputfilestream(const std::string &filePath)
00090 : std::istream(0),
00091 m_streambuf(0)
00092 {
00093
00094 std::filebuf* fb = new std::filebuf();
00095 _good=(fb->open(filePath.c_str(), std::ios::in)!=NULL);
00096
00097 if (filePath.size() > 3 &&
00098 filePath.substr(filePath.size() - 3, 3) == ".gz")
00099 {
00100 fb->close(); delete fb;
00101 m_streambuf = new gzfilebuf(filePath.c_str());
00102 } else {
00103 m_streambuf = fb;
00104 }
00105 this->init(m_streambuf);
00106 }
00107
00108 inputfilestream::~inputfilestream()
00109 {
00110 delete m_streambuf; m_streambuf = 0;
00111 }
00112
00113 void inputfilestream::close()
00114 {
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 void *MMap(int fd, int access, off_t offset, size_t len, off_t *gap)
00133 {
00134 void *p;
00135 int pgsz,g=0;
00136
00137 #ifdef _WIN32
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 #else
00163 if(offset) {
00164 pgsz = sysconf(_SC_PAGESIZE);
00165 g = *gap = offset%pgsz;
00166 } else if(gap) {
00167 *gap=0;
00168 }
00169 p = mmap((void*)0, len+g, access,
00170 MAP_SHARED|MAP_FILE,
00171 fd, offset-g);
00172 if((long)p==-1L) {
00173 perror("mmap failed");
00174 p=0;
00175 }
00176 #endif
00177 return p;
00178 }
00179
00180
00181 int Munmap(void *p,size_t len,int sync)
00182 {
00183 int r=0;
00184
00185 #ifdef _WIN32
00186
00187
00188
00189
00190
00191 #else
00192 cerr << "len = " << len << endl;
00193 cerr << "sync = " << sync << endl;
00194 cerr << "running msync..." << endl;
00195 if(sync) msync(p, len, MS_SYNC);
00196 cerr << "done. Running munmap..." << endl;
00197 if((r=munmap((void*)p, len))) perror("munmap() failed");
00198 cerr << "done" << endl;
00199
00200 #endif
00201 return r;
00202 }
00203
00204
00205
00206 Timer g_timer;
00207
00208
00209 void ResetUserTime()
00210 {
00211 g_timer.start();
00212 };
00213
00214 void PrintUserTime(const std::string &message)
00215 {
00216 g_timer.check(message.c_str());
00217 }
00218
00219 double GetUserTime()
00220 {
00221 return g_timer.get_elapsed_time();
00222 }
00223
00224
00225