<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>AlbumShaper: fileTools.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.9 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>fileTools.cpp</h1><a href="fileTools_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//==============================================</span> <a name="l00002"></a>00002 <span class="comment">// copyright : (C) 2003-2005 by Will Stokes</span> <a name="l00003"></a>00003 <span class="comment">//==============================================</span> <a name="l00004"></a>00004 <span class="comment">// This program is free software; you can redistribute it</span> <a name="l00005"></a>00005 <span class="comment">// and/or modify it under the terms of the GNU General</span> <a name="l00006"></a>00006 <span class="comment">// Public License as published by the Free Software</span> <a name="l00007"></a>00007 <span class="comment">// Foundation; either version 2 of the License, or</span> <a name="l00008"></a>00008 <span class="comment">// (at your option) any later version.</span> <a name="l00009"></a>00009 <span class="comment">//==============================================</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">//Systemwide includes</span> <a name="l00012"></a>00012 <span class="preprocessor">#include <fstream></span> <a name="l00013"></a>00013 <span class="preprocessor">#include <qstring.h></span> <a name="l00014"></a>00014 <span class="preprocessor">#include <qfile.h></span> <a name="l00015"></a>00015 <span class="preprocessor">#include <qdir.h></span> <a name="l00016"></a>00016 <span class="preprocessor">#include <qlibrary.h></span> <a name="l00017"></a>00017 <span class="comment">//#include <qglobal.h></span> <a name="l00018"></a>00018 <a name="l00019"></a>00019 <span class="comment">//Projectwide includes</span> <a name="l00020"></a>00020 <span class="preprocessor">#include "<a class="code" href="fileTools_8h.html">fileTools.h</a>"</span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="comment">//==============================================</span> <a name="l00023"></a>00023 <span class="comment">//PLATFORM_SPECIFIC_CODE</span> <a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="comment">//Includes and defines for getWindowsFolderLocation</span> <a name="l00026"></a>00026 <span class="preprocessor">#ifdef Q_OS_WIN</span> <a name="l00027"></a>00027 <span class="preprocessor"></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <shlobj.h></span> <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="preprocessor">#ifndef CSIDL_APPDATA</span> <a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor">#define CSIDL_APPDATA 0x001a</span> <a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00033"></a>00033 <span class="preprocessor"></span> <a name="l00034"></a>00034 <span class="preprocessor">#ifndef CSIDL_LOCAL_APPDATA</span> <a name="l00035"></a>00035 <span class="preprocessor"></span><span class="preprocessor">#define CSIDL_LOCAL_APPDATA 0x001c</span> <a name="l00036"></a>00036 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00037"></a>00037 <span class="preprocessor"></span> <a name="l00038"></a>00038 <span class="preprocessor">#endif //Q_OS_WIN</span> <a name="l00039"></a>00039 <span class="preprocessor"></span><span class="comment">//==============================================</span> <a name="l00040"></a><a class="code" href="fileTools_8h.html#303fab00942c01cb4872793ee0abe508">00040</a> <span class="keywordtype">bool</span> <a class="code" href="fileTools_8cpp.html#303fab00942c01cb4872793ee0abe508">moveFile</a>( QString oldName, QString newName) <a name="l00041"></a>00041 { <a name="l00042"></a>00042 QDir rootDir; <a name="l00043"></a>00043 <a name="l00044"></a>00044 <span class="comment">//attempt to rename file</span> <a name="l00045"></a>00045 <span class="keywordflow">if</span>(!rootDir.rename( oldName, newName)) <a name="l00046"></a>00046 { <a name="l00047"></a>00047 <span class="comment">//move failed, copy file and remove original</span> <a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="comment">//copy failed! sound alert and do not remove original!!!</span> <a name="l00050"></a>00050 <span class="keywordflow">if</span>(!<a class="code" href="fileTools_8cpp.html#f41848ecf008f6b6579f5b0975faea5a" title="Copies a file from one location to another.">copyFile</a>(oldName, newName)) <a name="l00051"></a>00051 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00052"></a>00052 <a name="l00053"></a>00053 <span class="comment">//copy succeded, remove original and return</span> <a name="l00054"></a>00054 rootDir.remove(oldName); <a name="l00055"></a>00055 } <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="comment">//move succeeded either directly or via copying and removing original file</span> <a name="l00058"></a>00058 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00059"></a>00059 } <a name="l00060"></a>00060 <span class="comment">//==============================================</span> <a name="l00061"></a><a class="code" href="fileTools_8h.html#130530443f6ebdf9ee59d1b419d4da24">00061</a> <span class="keywordtype">bool</span> <a class="code" href="fileTools_8cpp.html#f41848ecf008f6b6579f5b0975faea5a" title="Copies a file from one location to another.">copyFile</a>(QString oldFilePath, QString newFilePath) <a name="l00062"></a>00062 { <a name="l00063"></a>00063 <span class="comment">//same file, no need to copy</span> <a name="l00064"></a>00064 <span class="keywordflow">if</span>(oldFilePath.compare(newFilePath) == 0) <a name="l00065"></a>00065 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="comment">//load both files</span> <a name="l00068"></a>00068 QFile oldFile(oldFilePath); <a name="l00069"></a>00069 QFile newFile(newFilePath); <a name="l00070"></a>00070 <span class="keywordtype">bool</span> openOld = oldFile.open( IO_ReadOnly ); <a name="l00071"></a>00071 <span class="keywordtype">bool</span> openNew = newFile.open( IO_WriteOnly ); <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="comment">//if either file fails to open bail</span> <a name="l00074"></a>00074 <span class="keywordflow">if</span>(!openOld || !openNew) { <span class="keywordflow">return</span> <span class="keyword">false</span>; } <a name="l00075"></a>00075 <a name="l00076"></a>00076 <span class="comment">//copy contents</span> <a name="l00077"></a>00077 uint BUFFER_SIZE = 16000; <a name="l00078"></a>00078 <span class="keywordtype">char</span>* <a class="code" href="blur_8cpp.html#fa761c0753ecc94346e155a39bd9ed9c">buffer</a> = <span class="keyword">new</span> <span class="keywordtype">char</span>[BUFFER_SIZE]; <a name="l00079"></a>00079 <span class="keywordflow">while</span>(!oldFile.atEnd()) <a name="l00080"></a>00080 { <a name="l00081"></a>00081 Q_ULONG len = oldFile.readBlock( buffer, BUFFER_SIZE ); <a name="l00082"></a>00082 newFile.writeBlock( buffer, len ); <a name="l00083"></a>00083 } <a name="l00084"></a>00084 <a name="l00085"></a>00085 <span class="comment">//deallocate buffer</span> <a name="l00086"></a>00086 <span class="keyword">delete</span>[] buffer; <a name="l00087"></a>00087 buffer = NULL; <a name="l00088"></a>00088 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00089"></a>00089 } <a name="l00090"></a>00090 <span class="comment">//==============================================</span> <a name="l00091"></a>00091 <span class="comment">//PLATFORM_SPECIFIC_CODE</span> <a name="l00092"></a>00092 <span class="preprocessor">#ifdef Q_OS_WIN</span> <a name="l00093"></a>00093 <span class="preprocessor"></span><span class="keywordtype">bool</span> getWindowsFolderLocation(FOLDER_TYPE type, QString& path) <a name="l00094"></a>00094 { <a name="l00095"></a>00095 <span class="comment">//get pointer to item identifier</span> <a name="l00096"></a>00096 <span class="keywordtype">int</span> folder; <a name="l00097"></a>00097 <span class="keywordflow">switch</span>(type) <a name="l00098"></a>00098 { <a name="l00099"></a>00099 <span class="keywordflow">case</span> APPLICATION_DATA: <a name="l00100"></a>00100 folder = CSIDL_APPDATA; <span class="keywordflow">break</span>; <a name="l00101"></a>00101 <span class="keywordflow">case</span> LOCAL_SETTINGS_APPLICATION_DATA: <a name="l00102"></a>00102 folder = CSIDL_LOCAL_APPDATA; <span class="keywordflow">break</span>; <a name="l00103"></a>00103 <span class="comment">//unhandeled folder type</span> <a name="l00104"></a>00104 <span class="keywordflow">default</span>: <a name="l00105"></a>00105 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00106"></a>00106 } <a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="comment">//call unicode or local-specific functions as necessary to get folder path</span> <a name="l00109"></a>00109 <span class="keywordtype">bool</span> success = <span class="keyword">true</span>; <a name="l00110"></a>00110 QT_WA( <a name="l00111"></a>00111 { <a name="l00112"></a>00112 <span class="comment">//unicode version</span> <a name="l00113"></a>00113 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> folderPath[MAX_PATH]; <a name="l00114"></a>00114 <span class="keywordflow">if</span>( SHGetSpecialFolderPathW(0, folderPath, folder, <span class="keyword">false</span>) ) <a name="l00115"></a>00115 path = QString::fromUcs2( (ushort*)folderPath ); <a name="l00116"></a>00116 <span class="keywordflow">else</span> <a name="l00117"></a>00117 success = <span class="keyword">false</span>; <a name="l00118"></a>00118 }, <a name="l00119"></a>00119 { <a name="l00120"></a>00120 <span class="comment">//non-unicode version</span> <a name="l00121"></a>00121 <span class="keywordtype">char</span> folderPath[MAX_PATH]; <a name="l00122"></a>00122 <span class="keywordflow">if</span>( SHGetSpecialFolderPathA( 0, folderPath, folder, <span class="keyword">false</span> ) ) <a name="l00123"></a>00123 path = QString::fromLocal8Bit( folderPath ); <a name="l00124"></a>00124 <span class="keywordflow">else</span> <a name="l00125"></a>00125 success = <span class="keyword">false</span>; <a name="l00126"></a>00126 }); <a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="comment">//if failed to get LOCAL_SETTINGS_APPLICATION_DATA location then</span> <a name="l00129"></a>00129 <span class="comment">//try again using APPLICATION_DATA since this is likely Win95/Win98 (or ME?)</span> <a name="l00130"></a>00130 <span class="keywordflow">if</span>( (!success) && (type == LOCAL_SETTINGS_APPLICATION_DATA) ) <a name="l00131"></a>00131 <span class="keywordflow">return</span> getWindowsFolderLocation( APPLICATION_DATA, path ); <a name="l00132"></a>00132 <span class="keywordflow">else</span> <a name="l00133"></a>00133 <span class="keywordflow">return</span> success; <a name="l00134"></a>00134 } <a name="l00135"></a>00135 <span class="preprocessor">#endif //Q_OS_WIN</span> <a name="l00136"></a>00136 <span class="preprocessor"></span><span class="comment">//==============================================</span> <a name="l00137"></a><a class="code" href="fileTools_8h.html#1fe04c01b9364448467257879e4ba5ff">00137</a> QString <a class="code" href="fileTools_8cpp.html#1fe04c01b9364448467257879e4ba5ff" title="Replaces invalid characters in filenames with valid ones.">fixFilename</a>( QString filename ) <a name="l00138"></a>00138 { <a name="l00139"></a>00139 filename.replace( QChar(<span class="charliteral">' '</span>), <span class="stringliteral">"_"</span> ); <a name="l00140"></a>00140 filename.replace( <span class="stringliteral">"<"</span>, <span class="stringliteral">""</span> ); <a name="l00141"></a>00141 filename.replace( <span class="stringliteral">">"</span>, <span class="stringliteral">""</span> ); <a name="l00142"></a>00142 filename.replace( <span class="stringliteral">"&"</span>, <span class="stringliteral">"and"</span> ); <a name="l00143"></a>00143 filename.replace( <span class="stringliteral">"\""</span>, <span class="stringliteral">""</span> ); <a name="l00144"></a>00144 filename.replace( <span class="stringliteral">"\'"</span>, <span class="stringliteral">""</span> ); <a name="l00145"></a>00145 filename.replace( <span class="stringliteral">"?"</span>, <span class="stringliteral">""</span> ); <a name="l00146"></a>00146 <span class="keywordflow">return</span> filename; <a name="l00147"></a>00147 } <a name="l00148"></a>00148 <span class="comment">//==============================================</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Sun Aug 23 02:34:25 2009 for AlbumShaper by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> </body> </html>