gfxconversion/mifconv/inc/mifconv_util.h
changeset 0 f453ebb75370
child 2 1f6339ced17d
equal deleted inserted replaced
-1:000000000000 0:f453ebb75370
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Mifconv utilities.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __MIFCONVUTIL_H__
       
    20 #define __MIFCONVUTIL_H__
       
    21 
       
    22 #include "mifconv.h"
       
    23 #include "mifconv_sourcefile.h"
       
    24 #include <sstream>
       
    25 
       
    26 class MifConvUtil
       
    27 {
       
    28 public:
       
    29 
       
    30     /**
       
    31      * Returns file extension of the given filename
       
    32      * @return String representing the file extension
       
    33      * @param fileName String representing the file name.
       
    34      */
       
    35 	static MifConvString FileExtension( const MifConvString& fileName );
       
    36 
       
    37     /**
       
    38      * Returns file name without the extension
       
    39      * @return String representing the file name without the extension
       
    40      * @param fileName String representing the file name.
       
    41      */
       
    42     static MifConvString FilenameWithoutExtension( const MifConvString& fileName );
       
    43 
       
    44     /**
       
    45      * Returns file name without the path
       
    46      * @return String representing the file name without the path
       
    47      * @param fileName String representing the file name.
       
    48      */
       
    49     static MifConvString FilenameWithoutPath( const MifConvString& fileName );
       
    50 
       
    51     /**
       
    52      * Tells whether the file with the given filename exists.
       
    53      * @return true if the given file exists, false otherwise.
       
    54      * @param fileName File name
       
    55      */
       
    56     static bool FileExists( const MifConvString& fileName ); 
       
    57 
       
    58     /**
       
    59      * Returns file size
       
    60      * @return File size
       
    61      * @param fileName File name
       
    62      */
       
    63     static size_t FileSize( const MifConvString& fileName );
       
    64 
       
    65     /**
       
    66      * Reads file contents and returns pointer to data buffer. Returns also
       
    67      * the data size in bytes.
       
    68      * @return std::pair object containing the pointer to the allocated data buffer and the 
       
    69      * data size in byte. The caller must take the ownership of the returned data pointer.
       
    70      * @param fileName Filename
       
    71      */
       
    72     static MifConvFileData FileContents( const MifConvString& fileName );
       
    73 
       
    74     /**
       
    75      * Converts the given object to string. Can be used for example to converting integers to string:
       
    76      * <code>
       
    77      * int i = 0;
       
    78      * MifConvString iAsString(MifConvUtil::ToString(i));
       
    79      * </code>
       
    80      *
       
    81      * @return String built from the given object.
       
    82      * @param s Reference to the object to be converted to string.
       
    83      */
       
    84     template <typename T>
       
    85     static MifConvString ToString(const T& s)
       
    86     {
       
    87         std::stringstream ss;
       
    88         ss << s;
       
    89         return MifConvString(ss.str());
       
    90     }
       
    91 
       
    92     /**
       
    93      * Replaces given characters to other one from the given string.
       
    94      * @param str String from which the characters will be replaced.
       
    95      * @param replaceFrom This character will be replaced by other one.
       
    96      * @param replaceTo Characters will be replaced by this.
       
    97      */
       
    98     static void ReplaceChar( MifConvString& str, char replaceFrom, char replaceTo);
       
    99 
       
   100     /** 
       
   101      * Tells whether the given character is a whitespace character or not.
       
   102      * @param c Charater to be investigated.
       
   103      * @return true if the given character is whitespace character, false otherwise.
       
   104      */
       
   105     static bool IsWhiteSpace(char c);
       
   106 
       
   107     /**
       
   108      * Splits the given path to drive and directory path parts.
       
   109      * @param sourcePath Directory path to be splitted.
       
   110      * @param drive Drive part of the given path, if any.
       
   111      * @param path Directory path of the given path, if any.
       
   112      */
       
   113     static void SplitPath( const MifConvString& sourcePath, MifConvString& drive, MifConvString& path);
       
   114 
       
   115     /**
       
   116      * Compares two string. Ignores the case of the letters.
       
   117      * @return zero if the strings are equal
       
   118      * @param lhs Left hand side string
       
   119      * @param rhs Right hand side string
       
   120      */
       
   121     static int CompareIgnoreCase( const MifConvString& lhs, const MifConvString& rhs );
       
   122 
       
   123     /**
       
   124      * Replaces all the charactes by lower case characters.
       
   125      * @return String with lower case letters.
       
   126      * @param str Input string
       
   127      */
       
   128     static MifConvString& ToLower( MifConvString& str );
       
   129 
       
   130     /**
       
   131      * Splits the given string using the given separator.
       
   132      * @param str Input string
       
   133      * @param separator The input string is splitted using this sub-string as a separator
       
   134      * @param components List of the sub-strings splitted from the original input string
       
   135      */
       
   136     static void SplitString( const MifConvString& str, const MifConvString& separator, MifConvStringList& components );
       
   137 
       
   138     /**
       
   139      * Ensures that the given path exists. Tries to create it if it does not exist.
       
   140      * @param destFileName Filename that is to be checked.
       
   141      * @param ignoreLast If true, last part of the <code>destFileName</code> is stripped of 
       
   142      * Directory separator used as a separator.
       
   143      */
       
   144     static void EnsurePathExists( const MifConvString& destFileName, bool ignoreLast = false );
       
   145 
       
   146     /**
       
   147      * Removes duplicated directory separators from the given string.
       
   148      * @param str String from which the duplicated directory separators are to be searched and removed.
       
   149      */
       
   150     static void RemoveDuplicateDirSeparators( MifConvString& str );
       
   151 
       
   152     /**
       
   153      * Creates a directory with given path
       
   154      * @return True if the directory was successfully created, false otherwise.
       
   155      * @param path Directory path
       
   156      */
       
   157     static bool CreateDirectory( const MifConvString& path );
       
   158 
       
   159     /**
       
   160      * Trims the filename so that first letter is capitalized and others are lower case.
       
   161      * @return Trimmed string
       
   162      * @param filename Filename to be trimmed.
       
   163      */
       
   164     static MifConvString UnadornedFilename( const MifConvString& filename );
       
   165 
       
   166     /**
       
   167      * Finds the correct mask file for the bitmap file and sets it to the given object.
       
   168      * @param srcFile Source file object.
       
   169      */
       
   170     static void FindAndSetBitmapMaskFile( MifConvSourceFile& srcFile );
       
   171 
       
   172     /**
       
   173      * Returns a unique temporary file name.
       
   174      * @return Temporary file name string.
       
   175      */
       
   176     static MifConvString TemporaryFilename();
       
   177 
       
   178     /**
       
   179      * Copies a file 
       
   180      * @return True if the copying was successful, false otherwise.
       
   181      * @param from File which is to be copied from
       
   182      * @param to Destination file.
       
   183      */
       
   184     static bool CopyFile(const MifConvString& from, const MifConvString& to);
       
   185 
       
   186     /**
       
   187      * Returns current working directory path.
       
   188      * @return Current working directory path.
       
   189      */
       
   190     static MifConvString CurrentPath();
       
   191 
       
   192     /**
       
   193      * Changes the working directory
       
   194      * @param dirName Working directory name
       
   195      */
       
   196     static void ChangeDirectory( const MifConvString& dirName );
       
   197 
       
   198     /**
       
   199      * Removes a directory
       
   200      * @return Zero if the removal was successful, non-zero otherwise.
       
   201      * @param dirName Directory name
       
   202      */
       
   203     static int RemoveDirectory( const MifConvString& dirName );
       
   204 
       
   205     /**
       
   206      * Removes a file
       
   207      * @param fileName Filename
       
   208      * @param maxTries Maximum number of file deletion times in case of failure.
       
   209      * @param noException If true, no exception is thrown in case of failure. Warning is 
       
   210      * printed in debug log. If false, <code>MifConvException</code> is thrown in case of failure.
       
   211      */
       
   212     static void RemoveFile( const MifConvString& fileName, int maxTries = 1, bool noException = false );
       
   213             
       
   214     /**
       
   215      * Returns MifConv debug filename specified in MIFCONV_DEBUG_FILE
       
   216      * @return Debug file name
       
   217      */
       
   218     static MifConvString DebugFile();
       
   219 
       
   220     /**
       
   221      * Logs string to debug file
       
   222      * @param debugStr Debug string to be logged.
       
   223      */
       
   224     static void DebugLog(const MifConvString& debugStr);
       
   225     
       
   226     /**
       
   227      * Returns default temp directory for intermediate and temporary files
       
   228      * @return Temporary directory name
       
   229      */
       
   230     static const MifConvString& DefaultTempDirectory();
       
   231 
       
   232 private:
       
   233     enum MifConvDebugMode
       
   234     {
       
   235         DebugMode_Unknown,
       
   236         DebugMode_NoDebug,
       
   237         DebugMode_Debug
       
   238     };
       
   239 
       
   240     static MifConvDebugMode iDebugMode;
       
   241     static MifConvString iDebugFile;
       
   242     static MifConvString iTempDirectory;
       
   243 };
       
   244 
       
   245 
       
   246 #endif