#include "include.hpp"#include "error.hpp"Go to the source code of this file.
Functions | |
| std::string | replaceAll (const std::string &text, const std::string &toReplace, const std::string &replaceBy) |
| std::string | xmlText (const std::string &text) |
| std::string | versionText (int version) |
| std::string | dictionaryFile (void) throw (Error) |
| std::string | mealmasterMapFile (void) throw (Error) |
| std::string | findAnyMealFile (const char *type, const char *fileName) |
| std::string | anyMealLanguage (void) |
| int | gcd (int a, int b) |
| int | testFile (const std::string &fileName) |
| void | createDirRecursively (const std::string &dirName) throw (Error) |
| std::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.
00161 { 00162 QString language, country, charset; 00163 KLocale::splitLocale( QTextCodec::locale(), 00164 language, country, charset ); 00165 00166 return language; 00167 }
| void createDirRecursively | ( | const std::string & | dirName | ) | throw (Error) |
| std::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.
References ERRORMACRO.
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 }
| std::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.
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.
References gcd().
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 }
| std::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.
References createUnitMap(), and ERRORMACRO.
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 }
| std::string replaceAll | ( | const std::string & | text, | |
| const std::string & | toReplace, | |||
| const std::string & | replaceBy | |||
| ) |
Replace all occurences of a substring.
| int testFile | ( | const std::string & | fileName | ) |
Try to open file. This function can be used for testing, wether a directory or file exists. See manpage open(2) for more details.
| fileName | Name of file to test for. |
open. | std::string versionText | ( | int | version | ) |
Generate version-text to version number.
| version | ( version * 1000 + subversion ) |
Definition at line 74 of file utils.cpp.
00075 { 00076 int 00077 mainVersion = version / 1000, 00078 subVersion = version % 1000; 00079 ostringstream text; 00080 text << mainVersion << '.' << subVersion; 00081 return text.str(); 00082 }
| std::string xmlText | ( | const std::string & | text | ) |
Prepare string for use in an XML document. The prefix <![CDATA[is added. The postfix ]]> is added. every occurence of ]]> is replaced by "]]]]><![CDATA[>.