main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include <stdio.h>
  11. #if defined(_WIN32)
  12. #include "WindowsIncludes.h" // Sleep and CreateProcess
  13. #include <process.h> // system
  14. #else
  15. #include <unistd.h> // usleep
  16. #include <cstdio>
  17. #include <signal.h> //kill
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #endif
  22. #include "Gets.h"
  23. #include <iostream>
  24. using namespace std;
  25. // This is a simple tool to take the output of PatchApplication::restartOutputFilename
  26. // Perform file operations while that application is not running, and then relaunch that application
  27. int main(int argc, char **argv)
  28. {
  29. // Run commands on argv[1] and launch argv[2];
  30. // Run commands on argv[1] and launch argv[2];
  31. if (argc!=2)
  32. {
  33. printf("Usage: FileContainingCommands\n");
  34. return 1;
  35. }
  36. bool deleteFile=false;
  37. FILE *fp;
  38. fp = fopen(argv[1], "rt");
  39. if (fp==0)
  40. {
  41. printf("Error: Cannot open %s\n", argv[1]);
  42. return 1;
  43. }
  44. char buff[256];
  45. int offset=0;
  46. if (fgets(buff,255,fp)==0)
  47. return 1;
  48. buff[strlen(buff)]=0;
  49. while (buff[0])
  50. {
  51. if (strncmp(buff, "#Sleep ", 7)==0)
  52. {
  53. int sleepTime=atoi(buff+7);
  54. #ifdef _WIN32
  55. Sleep(sleepTime);
  56. #else
  57. usleep(sleepTime * 1000);
  58. #endif
  59. }
  60. else if (strncmp(buff, "#DeleteThisFile", 15)==0)
  61. deleteFile=true;
  62. else if (strncmp(buff, "#CreateProcess ", 15)==0)
  63. {
  64. #ifdef _WIN32
  65. PROCESS_INFORMATION pi;
  66. STARTUPINFO si;
  67. // Set up the start up info struct.
  68. memset(&si, 0, sizeof(STARTUPINFO));
  69. si.cb = sizeof(STARTUPINFO);
  70. // Launch the child process.
  71. if (!CreateProcess(
  72. NULL,
  73. buff+15,
  74. NULL, NULL,
  75. TRUE,
  76. CREATE_NEW_CONSOLE,
  77. NULL, NULL,
  78. &si,
  79. &pi))
  80. return 1;
  81. CloseHandle( pi.hProcess );
  82. CloseHandle( pi.hThread );
  83. #else
  84. char PathName[255];
  85. strcpy(PathName, buff+15);
  86. system(PathName); //This actually runs the application.
  87. #endif
  88. }
  89. else
  90. {
  91. system(buff);
  92. }
  93. if (fgets(buff,255,fp)==0)
  94. break;
  95. buff[strlen(buff)]=0;
  96. }
  97. fclose(fp);
  98. // Done!
  99. if (deleteFile)
  100. {
  101. unlink(argv[1]);
  102. }
  103. }
粤ICP备19079148号