Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

utils.hpp File Reference

#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)


Function Documentation

std::string anyMealLanguage ( void   ) 

Wrapper for getting current language from KDE or using getenv. This method will first try to call

    KGlobal::locale()->language()
If there is no KLocale object, 'en' will be returned.
Returns:
The two-letter language code (ISO 639-1 standard).

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)

Create directory and make parent directories as needed. Similar to doing

    mkdir -p dirname
    
on the command-line.

Also see manpages mkdir(1) and mkdir(3p)

Parameters:
dirName Name of directory to create

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.

Returns:
fileName

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 )
Parameters:
type Type of ressource.
fileName Relative filename of file to be located.
Returns:
The full filename on success. Otherwise the unmodified relative filename is returned.

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.

Parameters:
a natural number
b natural number
Returns:
The greatest common divisor of a and b.

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.

Returns:
fileName

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.

Date:
Sat Mar 05 2005
Author:
Jan Wedekind (wedesoft@users.sourceforge.net)

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.

Parameters:
fileName Name of file to test for.
Returns:
0, if the file is readable or else the error number generated by the call to open.

std::string versionText ( int  version  ) 

Generate version-text to version number.

Parameters:
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[>.

Date:
Sat Mar 05 2005
Author:
Jan Wedekind (wedesoft@users.sourceforge.net)



anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:28 2008 - GNU Free Documentation License