filecopier.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef FILECOPIER_H
  2. #define FILECOPIER_H
  3. class QDir;
  4. class QFileInfo;
  5. #include <QMap>
  6. #include <QSet>
  7. #include <QString>
  8. #include "generatoroutputview.h"
  9. /**
  10. * This class is for copying files or entire directory hierarchies
  11. * from a source to a destination, while possibly replacing filenames and/or
  12. * keywords in text file contents along the way.
  13. */
  14. class FileCopier
  15. {
  16. private:
  17. //
  18. // private members
  19. //
  20. const QString m_source;
  21. const QString m_destination;
  22. QDir m_srcBaseDir;
  23. QDir m_dstBaseDir;
  24. const QMap<QString, QString> * m_filenameSubstitutions;
  25. // OBS const QSet<QString> * m_ignorePatterns;
  26. const QMap<QString, QString> * m_keywordSubstitutions;
  27. GeneratorOutputView & m_outputView;
  28. const QRegExp m_keywordRegExp;
  29. QList<QRegExp> m_ignoreRegExps;
  30. public:
  31. /**
  32. * Instantiates a copier that can copy files from one place to another
  33. * while possibly swapping their names and/or keywords in their text
  34. * contents.
  35. *
  36. * (a) If the source is a directory and if the destination exists,
  37. * it also must be a directory.
  38. * (b) If the source is a directory and if the destination does not
  39. * exist but it looks like a directory (no suffix or a
  40. * trailing dir-separator), then destination is interpreted
  41. * as directory and will be created.
  42. * (c) If the source is a file, then destination may be an existing
  43. * directory or a nonexisting entity that looks like a directory.
  44. * (d) If the source is a file, then the destination may be an
  45. * existing file or a nonexisting entity that looks like a file
  46. * but in this case they must have the same suffix. NOTE: that the
  47. * actual filename produced is subject to filename substitution,
  48. * ie. even if the destination file existed, a totally new file
  49. * may be created with the substituted filename.
  50. *
  51. * In case source and destination point to directories, the directory
  52. * hierarchy starting with source is traversed. Corresponding subdirectories
  53. * under destination are created if they don't yet exist. Corresponding
  54. * files under destination are created or overwritten.
  55. *
  56. * @param source the place to copy files from
  57. *
  58. * @param destination the place to copy files to
  59. *
  60. * @param filenameSubstitutions if a filename (without extension) matches
  61. * any of the keys in the map, then the filename in the destination
  62. * directory hierarchy will be the value for that key (plus
  63. * extension of the original file. Optional, may be NULL.
  64. *
  65. * @param ignorePatterns is the set of filename patterns that are
  66. * exceptions from keyword substitution. Optional, may be NULL,
  67. * in which case no files are ignored. Pattern is the usual
  68. * wildcard pattern: it can be a full file name or may have
  69. * wildcard characters in it.
  70. *
  71. * @param keywordSubstitutions all files that are not to be ignored
  72. * (cf ignorePatterns) will have all their lines scanned for
  73. * occurrences of any of the keywords in the keywordSubstitutions
  74. * map. All those occurrences will be swapped for the value for the
  75. * key found. This process is done repeatedly, until no keyword is
  76. * found in the line. Optional, may be NULL.
  77. *
  78. * @param outputView progress is reported through
  79. */
  80. FileCopier(const QString & source,
  81. const QString & destination,
  82. const QMap<QString, QString> * filenameSubstitutions,
  83. const QSet<QString> * ignorePatterns,
  84. const QMap<QString, QString> * keywordSubstitutions,
  85. GeneratorOutputView & outputView);
  86. /**
  87. * Starts the copying process.
  88. *
  89. * @return if all files were successfully copied.
  90. */
  91. bool copy();
  92. private:
  93. //
  94. // implementation details
  95. //
  96. /**
  97. * @return true if the path exists and is a directory, or
  98. * it does not exist, but the path does not have
  99. * any suffixes or has a trailing directory separator.
  100. */
  101. static bool LooksLikeDir(const QFileInfo & file);
  102. /**
  103. * Copies a file to one place to another - verbatim.
  104. */
  105. bool copyFileVerbatim(const QFileInfo & srcFileInfo,
  106. const QFileInfo & dstFileInfo);
  107. /**
  108. * @return a replacement for parameter line, with any
  109. * keywords substituted in it.
  110. */
  111. QString substitute(const QString & line);
  112. /**
  113. * Copies a text file, performing keyword substituation on all
  114. * of its lines.
  115. */
  116. bool copyFileSubstituting(const QFileInfo & srcFileInfo,
  117. const QFileInfo & dstFileInfo);
  118. /**
  119. * @return true if a given file is exempt from keyword substitution.
  120. */
  121. bool fileToBeIgnored(const QFileInfo & dstFileInfo);
  122. /**
  123. * Copies a file to destination directory. Precondition:
  124. * destination directory must exist.
  125. *
  126. * Filename and keyword translation happen here, if enabled.
  127. */
  128. bool copyFile(const QFileInfo & srcFileInfo,
  129. const QDir & dstBaseDir);
  130. /**
  131. * Copies the contents of source directory into another,
  132. * destination directory. Destination directory need not exist,
  133. * it will be created.
  134. */
  135. bool copyDir(const QDir & srcBaseDir,
  136. const QDir & dstBaseDir);
  137. };
  138. #endif // FILECOPIER_H