PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/xhp/xhpize.cpp

http://github.com/facebook/xhp
C++ | 88 lines | 62 code | 8 blank | 18 comment | 34 complexity | d39b436d7bf4ff231e4d74755c769594 MD5 | raw file
Possible License(s): MIT, MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2009 - 2010 Facebook, Inc. (http://www.facebook.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE.PHP, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "xhp_preprocess.hpp"
  17. #include <vector>
  18. #include <string.h>
  19. #include <iostream>
  20. #include <fstream>
  21. using namespace std;
  22. int main(int argc, char* argv[]) {
  23. bool in_place = false, dry_run = false;
  24. vector<string> files;
  25. // Parse args
  26. for (int ii = 1; ii < argc; ++ii) {
  27. if (strcmp(argv[ii], "-i") == 0) {
  28. in_place = true;
  29. } else if (strcmp(argv[ii], "-d") == 0) {
  30. dry_run = true;
  31. } else if (strcmp(argv[ii], "-h") == 0 || strcmp(argv[ii], "-?") == 0) {
  32. cerr<< argv[0] << " -i [files] | " << argv[0] << " [file]\n";
  33. return 1;
  34. } else {
  35. files.push_back(argv[ii]);
  36. }
  37. }
  38. // Sanity checking
  39. if (in_place && files.size() == 0) {
  40. cerr<< "In place mode must be used with at least one file.\n";
  41. return 1;
  42. } else if (!in_place && files.size() > 1) {
  43. cerr<< "Multiple files must be used with in place mode.\n";
  44. return 1;
  45. } else if (files.size() == 0) {
  46. files.push_back("-");
  47. }
  48. // Parse
  49. for (vector<string>::iterator ii = files.begin(); ii != files.end(); ++ii) {
  50. ifstream inputFile;
  51. istream *inputStream;
  52. if (*ii == "-") {
  53. inputStream = &cin;
  54. } else {
  55. inputFile.open(ii->c_str());
  56. inputStream = &inputFile;
  57. }
  58. string code, error;
  59. uint32_t errLine;
  60. XHPResult result = xhp_preprocess(*inputStream, code, false, error, errLine);
  61. inputFile.close();
  62. if (result == XHPRewrote) {
  63. if (in_place) {
  64. if (!dry_run) {
  65. ofstream outputFile(ii->c_str());
  66. outputFile<< code;
  67. outputFile.close();
  68. }
  69. } else {
  70. cout<< code;
  71. cout.flush();
  72. }
  73. cerr<< "File `"<<(*ii)<<"` xhpized.\n";
  74. } else if (result == XHPErred) {
  75. cerr<< "Error parsing file `"<<(*ii)<<"`!!\n" << error << " on line " <<
  76. errLine << endl;;
  77. }
  78. }
  79. return 0;
  80. }