00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "helper.h"
00024
00025
00026 #ifdef FAUDES_WINEXTRA
00027 #include "winextra.h"
00028 #endif
00029
00030
00031 namespace faudes {
00032
00033
00034 std::string ToStringInteger(long int number) {
00035 std::string res;
00036 std::stringstream sstr;
00037 sstr << number;
00038 sstr >> res;
00039 return res;
00040 }
00041
00042
00043 std::string ToStringInteger16(long int number) {
00044 std::string res;
00045 std::stringstream sstr;
00046 sstr << "0x" << std::setbase(16) << number;
00047 sstr >> res;
00048 return res;
00049 }
00050
00051
00052
00053 std::string ToStringFloat(double number) {
00054 if(number == (long int) number)
00055 return(ToStringInteger((long int) number));
00056 std::string res;
00057 std::stringstream sstr;
00058 sstr << std::fixed;
00059 sstr << number;
00060 sstr >> res;
00061 return res;
00062 }
00063
00064
00065 std::string ExpandString(const std::string& rString, unsigned int len) {
00066 std::string res;
00067 res = rString;
00068 std::string::size_type xtra = (std::string::size_type) len - rString.length();
00069 if ((xtra > 0) && (xtra < 10000)) {
00070 res.append(xtra, ' ');
00071 }
00072 return res;
00073 }
00074
00075
00076 std::string CollapsString(const std::string& rString, unsigned int len) {
00077 if(len <0) return rString;
00078 if(rString.length() < len) return rString;
00079 int head = len/2;
00080 int tail = len/2;
00081 return rString.substr(0,head) + "..." + rString.substr(len-tail,tail);
00082 }
00083
00084
00085 Idx ToIdx(const std::string& rString) {
00086 char * end;
00087 unsigned long ul = strtoul (rString.c_str(), &end, 0);
00088 unsigned long idxmax = std::numeric_limits<Idx>::max();
00089 if (ul > idxmax) {
00090 throw Exception("atoidx", "Idx overflow", 600);
00091 }
00092 else {
00093 return (Idx) ul;
00094 }
00095 }
00096
00097
00098 std::string FDVersionString() {
00099 return std::string(FAUDES_VERSION);
00100 }
00101
00102
00103 std::string FDPluginsString() {
00104 return std::string(FAUDES_PLUGINS);
00105 }
00106
00107
00108 std::string FDContributorsString() {
00109 return
00110 "Berndt, Breindel, Doerr, Duevel, Franchi, Hellenschmidt, Moor, Musunoi, "
00111 "Opitz, Perk, Rempel, Ritter, Schlein, Schmidt, Singer, Wittmann, Zaddach, et al";
00112 }
00113
00114
00115
00116 void ProcessDot(const std::string& rDotFile,
00117 const std::string& rOutFile, const std::string& rOutFormat, const std::string& rDotExec)
00118 {
00119 std::string format=rOutFormat;
00120
00121 if(format=="") {
00122 if(rOutFile.rfind('.')+1 < rOutFile.size()) {
00123 format=rOutFile.substr(rOutFile.rfind('.')+1);
00124 }
00125 }
00126
00127 if (format == "canon");
00128 else if (format == "dot");
00129 else if (format == "xdot");
00130 else if (format == "cmap");
00131 else if (format == "dia");
00132 else if (format == "fig");
00133 else if (format == "gd");
00134 else if (format == "gd2");
00135 else if (format == "gif");
00136 else if (format == "hpgl");
00137 else if (format == "imap");
00138 else if (format == "cmapx");
00139 else if (format == "ismap");
00140 else if (format == "jpg");
00141 else if (format == "jpeg");
00142 else if (format == "mif");
00143 else if (format == "mp");
00144 else if (format == "pcl");
00145 else if (format == "pic");
00146 else if (format == "plain");
00147 else if (format == "plain-ext");
00148 else if (format == "png");
00149 else if (format == "ps");
00150 else if (format == "ps2");
00151 else if (format == "svg");
00152 else if (format == "svgz");
00153 else if (format == "vrml");
00154 else if (format == "vtx");
00155 else if (format == "wbmp");
00156 else {
00157 std::stringstream errstr;
00158 errstr << "Dot output format \"" << format << "\" unknown";
00159 throw Exception("faudes::ProcessDot", errstr.str(), 3);
00160 }
00161 std::string dotcommand = rDotExec + " -T"+format+" \""+rDotFile+"\" -o \""+rOutFile+"\"";
00162 if (system(dotcommand.c_str()) != 0) {
00163 throw Exception("faudes::ProcessDot",
00164 "Error in running dot", 3);
00165 }
00166 }
00167
00168
00169
00170
00171 std::string CreateTempFile(void) {
00172 char filename[]= "faudes_temp_XXXXXX";
00173 int filedes = mkstemp(filename);
00174 if(filedes==-1) {
00175 FD_DF("faudes::CreateTempFile(): error");
00176 return "";
00177 }
00178 close(filedes);
00179 std::string res(filename);
00180 FD_DF("faudes::CreateTempFile(): " << res);
00181 return(res);
00182 }
00183
00184
00185
00186
00187 bool RemoveFile(const std::string& rFileName) {
00188 return std::remove(rFileName.c_str()) == 0;
00189 }
00190
00191
00192 #ifndef FAUDES_DIR_SEPARATORS
00193 #define FAUDES_DIR_SEPARATORS "/"
00194 #endif
00195
00196
00197 std::string ExtractPath(const std::string& rFullName) {
00198 std::string res="";
00199 unsigned int seppos = rFullName.find_last_of(FAUDES_DIR_SEPARATORS);
00200 if(seppos==std::string::npos) return res;
00201 res=rFullName.substr(0,seppos+1);
00202 return res;
00203 }
00204
00205
00206 std::string PrependPath(const std::string& rPathName, const std::string& rFileName) {
00207 std::string res=rPathName;
00208 char sepchar=std::string(FAUDES_DIR_SEPARATORS).at(0);
00209 if(res!="")
00210 if(res.at(res.length()-1)!=sepchar)
00211 res.append(1,sepchar);
00212 res.append(rFileName);
00213 return res;
00214 }
00215
00216
00217 std::ofstream* pDebugFileStream=NULL;
00218 std::ostream* DebugStream(void) { return pDebugFileStream ? pDebugFileStream : &std::cerr;}
00219 void DebugToFile(const std::string& rFilename) {
00220 if(pDebugFileStream) delete pDebugFileStream;
00221 pDebugFileStream = new std::ofstream();
00222 pDebugFileStream->open(rFilename.c_str());
00223 }
00224
00225 }