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 #else
00025 #include <sys/times.h>
00026 #include <sys/resource.h>
00027 #endif
00028
00029 #include <cstring>
00030 #include <cctype>
00031 #include <algorithm>
00032 #include <stdio.h>
00033 #include <iostream>
00034 #include <iomanip>
00035 #include "TypeDef.h"
00036 #include "Util.h"
00037 #include "Timer.h"
00038
00039 using namespace std;
00040
00041 namespace Moses
00042 {
00043
00044
00045 Timer g_timer;
00046
00047 string GetTempFolder()
00048 {
00049 #ifdef _WIN32
00050 char *tmpPath = getenv("TMP");
00051 string str(tmpPath);
00052 if (str.substr(str.size() - 1, 1) != "\\")
00053 str += "\\";
00054 return str;
00055 #else
00056 return "/tmp/";
00057 #endif
00058 }
00059
00060 void CreateTempFile(ofstream &fileStream, string &filePath)
00061 {
00062 #ifdef _WIN32
00063 char buffer[BUFSIZ];
00064 ::GetTempFileNameA(GetTempFolder().c_str(), "", 0, buffer);
00065 filePath = buffer;
00066 #else
00067 char buffer[L_tmpnam];
00068 strcpy(buffer, GetTempFolder().c_str());
00069 strcat(buffer, PROJECT_NAME);
00070 strcat(buffer, "--XXXXXX");
00071 mkstemp(buffer);
00072 filePath = buffer;
00073 #endif
00074 fileStream.open(filePath.c_str(), ofstream::out | ofstream::app);
00075 }
00076
00077
00078 const std::string ToLower(const std::string& str)
00079 {
00080 std::string lc(str);
00081 std::transform(lc.begin(), lc.end(), lc.begin(), (int(*)(int))std::tolower);
00082 return lc;
00083 }
00084
00085 template<>
00086 bool Scan<bool>(const std::string &input)
00087 {
00088 std::string lc = ToLower(input);
00089 if (lc == "yes" || lc == "y" || lc == "true" || lc == "1")
00090 return true;
00091 if (lc == "no" || lc == "n" || lc =="false" || lc == "0")
00092 return false;
00093 TRACE_ERR( "Scan<bool>: didn't understand '" << lc << "', returning false" << std::endl);
00094 return false;
00095 }
00096
00097 bool FileExists(const std::string& filePath)
00098 {
00099 ifstream ifs(filePath.c_str());
00100 return !ifs.fail();
00101 }
00102
00103 const std::string Trim(const std::string& str, const std::string dropChars)
00104 {
00105 std::string res = str;
00106 res.erase(str.find_last_not_of(dropChars)+1);
00107 return res.erase(0, res.find_first_not_of(dropChars));
00108 }
00109
00110 void ResetUserTime()
00111 {
00112 g_timer.start();
00113 };
00114
00115 void PrintUserTime(const std::string &message)
00116 {
00117 g_timer.check(message.c_str());
00118 }
00119
00120 double GetUserTime()
00121 {
00122 return g_timer.get_elapsed_time();
00123 }
00124
00125 std::map<std::string, std::string> ProcessAndStripSGML(std::string &line)
00126 {
00127 std::map<std::string, std::string> meta;
00128 std::string lline = ToLower(line);
00129 if (lline.find("<seg")!=0) return meta;
00130 size_t close = lline.find(">");
00131 if (close == std::string::npos) return meta;
00132 size_t end = lline.find("</seg>");
00133 std::string seg = Trim(lline.substr(4, close-4));
00134 std::string text = line.substr(close+1, end - close - 1);
00135 for (size_t i = 1; i < seg.size(); i++) {
00136 if (seg[i] == '=' && seg[i-1] == ' ') {
00137 std::string less = seg.substr(0, i-1) + seg.substr(i);
00138 seg = less;
00139 i = 0;
00140 continue;
00141 }
00142 if (seg[i] == '=' && seg[i+1] == ' ') {
00143 std::string less = seg.substr(0, i+1);
00144 if (i+2 < seg.size()) less += seg.substr(i+2);
00145 seg = less;
00146 i = 0;
00147 continue;
00148 }
00149 }
00150 line = Trim(text);
00151 if (seg == "") return meta;
00152 for (size_t i = 1; i < seg.size(); i++) {
00153 if (seg[i] == '=') {
00154 std::string label = seg.substr(0, i);
00155 std::string val = seg.substr(i+1);
00156 if (val[0] == '"') {
00157 val = val.substr(1);
00158 size_t close = val.find('"');
00159 if (close == std::string::npos) {
00160 TRACE_ERR("SGML parse error: missing \"\n");
00161 seg = "";
00162 i = 0;
00163 } else {
00164 seg = val.substr(close+1);
00165 val = val.substr(0, close);
00166 i = 0;
00167 }
00168 } else {
00169 size_t close = val.find(' ');
00170 if (close == std::string::npos) {
00171 seg = "";
00172 i = 0;
00173 } else {
00174 seg = val.substr(close+1);
00175 val = val.substr(0, close);
00176 }
00177 }
00178 label = Trim(label);
00179 seg = Trim(seg);
00180 meta[label] = val;
00181 }
00182 }
00183 return meta;
00184 }
00185
00186 }
00187
00188