filecopier.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include <QDir>
  2. #include <QFile>
  3. #include <QFileInfo>
  4. #include <QList>
  5. #include <QRegExp>
  6. #include <QStack>
  7. #include <QTextStream>
  8. #include "filecopier.h"
  9. #include "common/fstraverseiterator.h"
  10. // THIS IS NOT THE PLACE FOR THIS DEFINE!
  11. #define TXTSUBST_KWORDREGEXP "@[a-zA-Z]+@"
  12. FileCopier::FileCopier(const QString & source,
  13. const QString & destination,
  14. const QMap<QString, QString> * filenameSubstitutions,
  15. const QSet<QString> * ignorePatterns,
  16. const QMap<QString, QString> * keywordSubstitutions,
  17. GeneratorOutputView & outputView)
  18. : m_source(source)
  19. , m_destination(destination)
  20. , m_filenameSubstitutions(filenameSubstitutions)
  21. , m_keywordSubstitutions(keywordSubstitutions)
  22. , m_outputView(outputView)
  23. , m_keywordRegExp(TXTSUBST_KWORDREGEXP)
  24. {
  25. if (ignorePatterns != NULL)
  26. {
  27. foreach (QString ignorePattern, *ignorePatterns)
  28. {
  29. // TODO should the case sensitivity and wildcard / wildcardunix
  30. // be made platform dependent too?
  31. QRegExp
  32. ignoreRegExp(ignorePattern,
  33. Qt::CaseSensitive,
  34. QRegExp::Wildcard);
  35. m_ignoreRegExps.push_back(ignoreRegExp);
  36. }
  37. }
  38. }
  39. bool FileCopier::LooksLikeDir(const QFileInfo & file)
  40. {
  41. bool
  42. rv = true;
  43. if (file.exists())
  44. {
  45. rv = file.isDir();
  46. }
  47. else
  48. {
  49. // path does not exist, so we have to guess
  50. // based on its look whether it can be thought
  51. // as a directory
  52. QString
  53. path = file.absoluteFilePath();
  54. bool
  55. suffixLess = file.suffix().length() == 0,
  56. hasTrailingDirSeparator = path.at(path.length() - 1) == QDir::separator();
  57. rv = suffixLess || hasTrailingDirSeparator;
  58. }
  59. return rv;
  60. }
  61. bool FileCopier::copyFileVerbatim(const QFileInfo & srcFileInfo,
  62. const QFileInfo & dstFileInfo)
  63. {
  64. bool
  65. rv = true;
  66. if (dstFileInfo.exists())
  67. {
  68. rv = QFile(dstFileInfo.absoluteFilePath()).remove();
  69. }
  70. rv = rv && QFile::copy(srcFileInfo.absoluteFilePath(),
  71. dstFileInfo.absoluteFilePath());
  72. if (!rv)
  73. {
  74. QString
  75. msg("Error: Could not copy %1 to %2 verbatim.");
  76. m_outputView.printOutput(msg.arg(srcFileInfo.absoluteFilePath(),
  77. dstFileInfo.absoluteFilePath()));
  78. }
  79. return rv;
  80. }
  81. QString FileCopier::substitute(const QString & line)
  82. {
  83. QString
  84. rv = line;
  85. int
  86. pos;
  87. while ((pos = m_keywordRegExp.indexIn(rv)) != -1)
  88. {
  89. QString
  90. key = m_keywordRegExp.cap(0);
  91. if (m_keywordSubstitutions->contains(key))
  92. {
  93. QString
  94. value = (*m_keywordSubstitutions)[key];
  95. rv.replace(pos,
  96. key.length(),
  97. value);
  98. }
  99. else
  100. {
  101. QString
  102. msg("Warning: No substitution defined for keyword '%1'.");
  103. m_outputView.printOutput(msg.arg(key));
  104. break;
  105. }
  106. }
  107. return rv;
  108. }
  109. bool FileCopier::copyFileSubstituting(const QFileInfo & srcFileInfo,
  110. const QFileInfo & dstFileInfo)
  111. {
  112. bool
  113. rv = false;
  114. QFile
  115. srcFile(srcFileInfo.absoluteFilePath());
  116. rv = srcFile.open(QIODevice::ReadOnly | QIODevice::Text);
  117. if (rv)
  118. {
  119. QFile
  120. dstFile(dstFileInfo.absoluteFilePath());
  121. rv = dstFile.open(QIODevice::WriteOnly | QIODevice::Text);
  122. if (rv)
  123. {
  124. QTextStream
  125. srcStream(&srcFile);
  126. QTextStream
  127. dstStream(&dstFile);
  128. while (rv && !srcStream.atEnd())
  129. {
  130. QString
  131. line = srcStream.readLine();
  132. rv = srcStream.status() == QTextStream::Ok;
  133. if (rv)
  134. {
  135. line = substitute(line);
  136. dstStream << line << endl;
  137. rv = dstStream.status() == QTextStream::Ok;
  138. if (!rv)
  139. {
  140. QString
  141. msg("Error: Could not write to %1.");
  142. m_outputView.printOutput(msg.arg(dstFileInfo.absoluteFilePath()));
  143. }
  144. }
  145. else
  146. {
  147. QString
  148. msg("Error: Could not read from %1.");
  149. m_outputView.printOutput(msg.arg(srcFileInfo.absoluteFilePath()));
  150. }
  151. }
  152. }
  153. else
  154. {
  155. QString
  156. msg("Error: Could not open destination %1 for writing.");
  157. m_outputView.printOutput(msg.arg(dstFileInfo.absoluteFilePath()));
  158. }
  159. }
  160. else
  161. {
  162. QString
  163. msg("Error: Could not open source %1 for reading.");
  164. m_outputView.printOutput(msg.arg(srcFileInfo.absoluteFilePath()));
  165. }
  166. return rv;
  167. }
  168. bool FileCopier::fileToBeIgnored(const QFileInfo & dstFileInfo)
  169. {
  170. bool
  171. rv = false;
  172. QString
  173. fileName = dstFileInfo.fileName();
  174. foreach (QRegExp ignoreRegExp, m_ignoreRegExps)
  175. {
  176. if ((rv = (ignoreRegExp.indexIn(fileName) != -1)))
  177. {
  178. break;
  179. }
  180. }
  181. return rv;
  182. }
  183. bool FileCopier::copyFile(const QFileInfo & srcFileInfo,
  184. const QDir & dstBaseDir)
  185. {
  186. QString
  187. msg("Copying file %1 to directory %2");
  188. m_outputView.printOutput(msg.arg(srcFileInfo.absoluteFilePath(),
  189. dstBaseDir.absolutePath()));
  190. bool
  191. rv = true;
  192. // filename substitution, if enabled
  193. QString
  194. baseName(srcFileInfo.baseName()),
  195. suffix(srcFileInfo.suffix());
  196. if (m_filenameSubstitutions != NULL
  197. && m_filenameSubstitutions->contains(baseName))
  198. {
  199. baseName = (*m_filenameSubstitutions)[baseName];
  200. }
  201. QString
  202. dstFilePath = dstBaseDir.absolutePath();
  203. dstFilePath += QDir::separator();
  204. dstFilePath += baseName;
  205. if (suffix.length() > 0)
  206. {
  207. dstFilePath += '.';
  208. dstFilePath += suffix;
  209. }
  210. QFileInfo
  211. dstFileInfo(dstFilePath);
  212. if (m_keywordSubstitutions != NULL
  213. && !m_keywordSubstitutions->empty()
  214. && !fileToBeIgnored(dstFileInfo))
  215. {
  216. rv = copyFileSubstituting(srcFileInfo,
  217. dstFileInfo);
  218. }
  219. else
  220. {
  221. rv = copyFileVerbatim(srcFileInfo,
  222. dstFileInfo);
  223. }
  224. return rv;
  225. }
  226. bool FileCopier::copyDir(const QDir & srcBaseDir,
  227. const QDir & dstBaseDir)
  228. {
  229. QString
  230. msg("Copying directory content of %1 to %2");
  231. m_outputView.printOutput(msg.arg(srcBaseDir.absolutePath(),
  232. dstBaseDir.absolutePath()));
  233. bool
  234. rv = true,
  235. first = true;
  236. QDir
  237. curSrcDir = srcBaseDir,
  238. curDstDir = dstBaseDir;
  239. // Instead of recursive call here, we use an iterator that traverses
  240. // recursively - and keep tracking the current src/dst directories.
  241. // This iterator (DirsBoth) will enumerate each directory twice: once
  242. // before its content and once after it. We'll use the first even
  243. // to create dst dir and descend both src/dst dirs, and use the second
  244. // to go back up.
  245. FSTraverseIterator
  246. fsIt(srcBaseDir.absolutePath(),
  247. FSTraverseIterator::DirsBoth | FSTraverseIterator::Files),
  248. fsEnd;
  249. for (; fsIt != fsEnd && rv; ++fsIt)
  250. {
  251. QFileInfo
  252. fi = *fsIt;
  253. if (fi.isFile())
  254. {
  255. rv = copyFile(fi,
  256. curDstDir);
  257. }
  258. else // fi.isDir()
  259. {
  260. if (first) // the very beginning of iteration
  261. {
  262. first = false;
  263. if (!curDstDir.exists(curDstDir.absolutePath()))
  264. rv = curDstDir.mkdir(curDstDir.absolutePath());
  265. }
  266. else if (curSrcDir.absolutePath() == fi.absoluteFilePath())
  267. {
  268. // done with all the kids of cur src/dst dirs: "cd .."
  269. curSrcDir.cdUp();
  270. curDstDir.cdUp();
  271. }
  272. else
  273. {
  274. // for src/dst dirst: "cd <child-dir-name>"
  275. QString
  276. dirName = QDir(fi.absoluteFilePath()).dirName();
  277. rv = curSrcDir.cd(dirName);
  278. if (rv)
  279. {
  280. if (!curDstDir.exists(dirName))
  281. rv = curDstDir.mkdir(dirName);
  282. if (rv)
  283. rv = curDstDir.cd(dirName);
  284. }
  285. }
  286. }
  287. }
  288. return rv;
  289. }
  290. bool FileCopier::copy()
  291. {
  292. bool
  293. rv = false;
  294. QFileInfo
  295. srcFileInfo(m_source),
  296. dstFileInfo(m_destination);
  297. if (srcFileInfo.exists() && srcFileInfo.isDir())
  298. {
  299. if (dstFileInfo.exists() && dstFileInfo.isFile())
  300. {
  301. QString
  302. msg("Error: Source %1 is dir, destination %1 is file.");
  303. m_outputView.printOutput(msg.arg(m_source, m_destination));
  304. }
  305. else
  306. {
  307. m_srcBaseDir = QDir(m_source);
  308. m_dstBaseDir = QDir(m_destination);
  309. rv = copyDir(m_srcBaseDir, m_dstBaseDir);
  310. }
  311. }
  312. else if(srcFileInfo.exists() && srcFileInfo.isFile())
  313. {
  314. m_srcBaseDir = QDir(srcFileInfo.absoluteDir());
  315. if (LooksLikeDir(dstFileInfo))
  316. {
  317. m_dstBaseDir = QDir(dstFileInfo.absoluteFilePath());
  318. rv = m_dstBaseDir.mkpath(m_dstBaseDir.absolutePath())
  319. && copyFile(srcFileInfo,
  320. m_dstBaseDir);
  321. }
  322. else
  323. {
  324. if (srcFileInfo.suffix() != dstFileInfo.suffix())
  325. {
  326. QString
  327. msg("Error: Source file %1 suffix mismatches "
  328. "destination %2 file suffix.");
  329. m_outputView.printOutput(msg.arg(m_source,
  330. m_destination));
  331. }
  332. else
  333. {
  334. m_dstBaseDir = QDir(dstFileInfo.absoluteDir());
  335. rv = m_dstBaseDir.mkpath(m_dstBaseDir.absolutePath())
  336. && copyFile(srcFileInfo,
  337. m_dstBaseDir);
  338. }
  339. }
  340. }
  341. else
  342. {
  343. QString
  344. msg("Error: Source %1 does not exist or cannot handle it");
  345. m_outputView.printOutput(msg.arg(m_source));
  346. }
  347. return rv;
  348. }