#include <cassert>#include <errno.h>#include <fcntl.h>#include <fstream>#include <iomanip>#include <iostream>#include <kglobal.h>#include <klocale.h>#include <kstandarddirs.h>#include <string.h>#include <qobject.h>#include <qtextcodec.h>#include <sstream>#include <sys/types.h>#include <sys/stat.h>#include "unitMap.hpp"#include "utils.hpp"Go to the source code of this file.
Functions | |
| string | replaceAll (const string &text, const string &toReplace, const string &replaceBy) |
| string | xmlText (const string &text) |
| string | versionText (int version) |
| string | dictionaryFile (void) throw (Error) |
| string | mealmasterMapFile (void) throw (Error) |
| string | findAnyMealFile (const char *type, const char *fileName) |
| string | anyMealLanguage (void) |
| int | gcd (int a, int b) |
| int | testFile (const string &fileName) |
| void | createDirRecursively (const string &dirName) throw (Error) |
| string anyMealLanguage | ( | void | ) |
Wrapper for getting current language from KDE or using getenv. This method will first try to call
KGlobal::locale()->language()
KLocale object, 'en' will be returned. Definition at line 160 of file utils.cpp.
Referenced by cli().
00161 { 00162 QString language, country, charset; 00163 KLocale::splitLocale( QTextCodec::locale(), 00164 language, country, charset ); 00165 00166 return language; 00167 }
| void createDirRecursively | ( | const string & | dirName | ) | throw (Error) |
Definition at line 197 of file utils.cpp.
References ERRORMACRO, and testFile().
00198 { 00199 int result = mkdir( dirName.c_str(), S_IRWXU ); 00200 if ( result != 0 ) { 00201 switch ( errno ) { 00202 case ENOENT: { 00203 unsigned int pos = dirName.find_last_of( "/" ); 00204 ERRORMACRO( pos != string::npos, Error, , 00205 "Failed to create directory '" << dirName << "': " 00206 << strerror( errno ) ); 00207 assert( pos < dirName.size() ); 00208 createDirRecursively( string( dirName, 0, pos ) ); 00209 createDirRecursively( dirName ); 00210 break;} 00211 case EEXIST: 00212 ERRORMACRO( testFile( dirName ) != EISDIR, Error, , 00213 "Failed to create directory '" << dirName << "': " 00214 << strerror( errno ) ); 00215 break; 00216 default: 00217 #ifndef NDEBUG 00218 cerr << "mkdir resulted in error number " << errno << '.' << endl; 00219 #endif 00220 ERRORMACRO( false, Error, , 00221 "Failed to create directory '" << dirName << "': " 00222 << strerror( errno ) ); 00223 }; 00224 }; 00225 }
| string dictionaryFile | ( | void | ) | throw (Error) |
Create temporary XML dictionary. Create a temporary XML file with translations, which is included by the XSL-file creating docbook-output from anymeal's internal XML format.
Sourcecode for using mkstemp and std::fstream in combination was found in the bayonne-project.
Definition at line 84 of file utils.cpp.
Referenced by cli().
00085 { 00086 char *fileName = strdup( "/tmp/anymeal-dictionary-xsl.XXXXXX" ); 00087 int fd = mkstemp( fileName ); 00088 string retVal = fileName; 00089 free( fileName ); 00090 ERRORMACRO( fd != -1, Error, , 00091 "Error creating temporary file." ); 00092 ofstream f( retVal.c_str() ); 00093 close( fd ); 00094 f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 00095 << "<dictionary>\n" 00096 << "<entry text=\"Amount\">" << i18n( "Amount" ) << "</entry>" 00097 << "<entry text=\"Cookbook\">" << i18n( "Cookbook" ) << "</entry>" 00098 << "<entry text=\"Categories\">" << i18n( "Categories" ) 00099 << "</entry>" 00100 << "<entry text=\"generated by AnyMeal\">" 00101 << i18n( "generated by AnyMeal" ) << "</entry>" 00102 << "<entry text=\"Ingredients\">" << i18n( "Ingredients" ) 00103 << "</entry>" 00104 << "<entry text=\"Instructions\">" << i18n( "Instructions" ) 00105 << "</entry>" 00106 << "<entry text=\"Name\">" << i18n( "Name" ) << "</entry>" 00107 << "<entry text=\"Preparation\">" << i18n( "Preparation" ) 00108 << "</entry>" 00109 << "<entry text=\"Unit\">" << i18n( "Unit" ) << "</entry>" 00110 << "<entry text=\"Yield\">" << i18n( "Yield" ) << "</entry>" 00111 << "</dictionary>"; 00112 return retVal; 00113 }
| string findAnyMealFile | ( | const char * | type, | |
| const char * | fileName | |||
| ) |
Wrapper for finding a ressource with KDE. This is a wrapper for the following method-call:
KGlobal::dirs()->findResource( type, fileName )
| type | Type of ressource. | |
| fileName | Relative filename of file to be located. |
Definition at line 136 of file utils.cpp.
Referenced by cli(), and gui().
00137 { 00138 string retVal; 00139 KStandardDirs *dirs = KGlobal::dirs(); 00140 if ( dirs != NULL ) { 00141 QString result = dirs->findResource( type, fileName ); 00142 if ( result != QString::null ) 00143 retVal = (const char *)result; 00144 else { 00145 #ifndef NDEBUG 00146 cerr << "Warning: KStandardDirs::findResource( \"" << type << "\", \"" 00147 << fileName << "\" ) returned QString::null." << endl; 00148 #endif 00149 retVal = fileName; 00150 }; 00151 } else { 00152 #ifndef NDEBUG 00153 cerr << "Warning: KGlobal::dirs() is null." << endl; 00154 #endif 00155 retVal = fileName; 00156 }; 00157 return retVal; 00158 }
| int gcd | ( | int | a, | |
| int | b | |||
| ) |
Greatest common divisor. Implementation of the Euclidean algorithm to find the greatest common divisor of two numbers.
| a | natural number | |
| b | natural number |
Definition at line 169 of file utils.cpp.
Referenced by gcd(), and IngredientViewItem::setAmount().
00170 { 00171 assert( a >= 0 ); 00172 assert( b >= 0 ); 00173 int retVal; 00174 if ( a < b ) 00175 retVal = gcd( b, a ); 00176 else { 00177 assert( a >= b ); 00178 if ( b == 0 ) 00179 retVal = a; 00180 else 00181 retVal = gcd( b, a % b ); 00182 }; 00183 return retVal; 00184 }
| string mealmasterMapFile | ( | void | ) | throw (Error) |
Create temporary XML map for units. Create a temporary XML file for mapping units on Mealmaster abbreviations, which is included by the XSL-file converting anymeal's internal XML format to Mealmaster.
Sourcecode for using mkstemp and std::fstream in combination was found in the bayonne-project.
Definition at line 115 of file utils.cpp.
Referenced by cli().
00116 { 00117 map< string, string > unitMap( createUnitMap() ); 00118 char *fileName = strdup( "/tmp/anymeal-mealmastermap-xsl.XXXXXX" ); 00119 int fd = mkstemp( fileName ); 00120 string retVal = fileName; 00121 free( fileName ); 00122 ERRORMACRO( fd != -1, Error, , 00123 "Error creating temporary file." ); 00124 ofstream f( retVal.c_str() ); 00125 close( fd ); 00126 f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl 00127 << "<mealmastermap>" << endl; 00128 for ( map< string, string >::const_iterator i = unitMap.begin(); 00129 i != unitMap.end(); i++ ) 00130 f << "<item text=\"" << i->first << "\">" << i->second 00131 << "</item>" << endl; 00132 f << "</mealmastermap>" << endl; 00133 return retVal; 00134 }
| string replaceAll | ( | const string & | text, | |
| const string & | toReplace, | |||
| const string & | replaceBy | |||
| ) |
Definition at line 36 of file utils.cpp.
References position.
Referenced by main(), and xmlText().
00039 { 00040 string retVal; 00041 00042 assert( toReplace.length() > 0 ); 00043 00044 int 00045 toReplaceLength = toReplace.length(), 00046 previous =0; 00047 00048 while ( true ) { 00049 00050 int position = text.find( toReplace, previous ); 00051 00052 if ( position < 0 ) { 00053 retVal += text.substr( previous, text.length() - previous ); 00054 break; 00055 }; 00056 00057 retVal += text.substr( previous, position - previous ); 00058 retVal += replaceBy; 00059 00060 previous = position + toReplaceLength; 00061 }; 00062 00063 return retVal; 00064 }
| int testFile | ( | const string & | fileName | ) |
Definition at line 186 of file utils.cpp.
Referenced by createDirRecursively().
00187 { 00188 int retVal = 0; 00189 int desc = open( fileName.c_str(), O_RDWR ); 00190 if ( desc == -1 ) 00191 retVal = errno; 00192 else 00193 close( desc ); 00194 return retVal; 00195 }
| string versionText | ( | int | version | ) |
Generate version-text to version number.
| version | ( version * 1000 + subversion ) |
Definition at line 74 of file utils.cpp.
Referenced by CookBook::update().
00075 { 00076 int 00077 mainVersion = version / 1000, 00078 subVersion = version % 1000; 00079 ostringstream text; 00080 text << mainVersion << '.' << subVersion; 00081 return text.str(); 00082 }
| string xmlText | ( | const string & | text | ) |
Definition at line 66 of file utils.cpp.
References replaceAll().
Referenced by XSU::translate().
00067 { 00068 string retVal = 00069 string( "<![CDATA[" ) + 00070 replaceAll( text, "]]>", "]]]]><![CDATA[>" ) + "]]>"; 00071 return retVal; 00072 }