buildVersionUpdate.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FILE: buildVersionUpdate.cpp //////////////////////////////////////////////////////
  19. // Generals version number class updater
  20. // Author: Matthew D. Campbell, November 2001
  21. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  22. #define WIN32_LEAN_AND_MEAN // only bare bones windows stuff wanted
  23. #include <windows.h>
  24. #include <lmcons.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. // Local defines
  29. #define VERSION_MAJOR "VERSION_MAJOR"
  30. #define VERSION_MINOR "VERSION_MINOR"
  31. #define VERSION_BUILDNUM "VERSION_BUILDNUM"
  32. #define VERSION_STRING "VERSION_STRING"
  33. #define FORMAT "#define " VERSION_STRING " \"%d.%d.%d\"\n"
  34. #define COMMENTS "// Do not modify this file by hand. Auto-created and\n// Updated by buildVersionUpdate.\n"
  35. #define NUMFMT "#define %s %d\n"
  36. #define NUMFMT_MINOR "#define %s %d ///< This effects the replay version number.\n"
  37. static void writeVersion(char *file, int major, int minor, int build)
  38. {
  39. FILE *filePtr = fopen(file, "w");
  40. // Clobber the file. Hey, this is a simple program.
  41. if (file)
  42. {
  43. if (filePtr)
  44. {
  45. fprintf(filePtr, COMMENTS);
  46. fprintf(filePtr, FORMAT, major, minor, build);
  47. fprintf(filePtr, NUMFMT, VERSION_MAJOR, major);
  48. fprintf(filePtr, NUMFMT_MINOR, VERSION_MINOR, minor);
  49. fprintf(filePtr, NUMFMT, VERSION_BUILDNUM, build);
  50. fclose(filePtr);
  51. }
  52. else
  53. {
  54. printf("Cannot write file\n");
  55. }
  56. }
  57. else
  58. {
  59. printf("No file to write\n");
  60. }
  61. }
  62. static void usage(char *progname)
  63. {
  64. if (progname)
  65. {
  66. printf ("Usage: %s versionfile.h", progname);
  67. }
  68. }
  69. // strtrim ====================================================================
  70. /** Trim leading and trailing whitespace from a character string (in place). */
  71. //=============================================================================
  72. static char* strtrim(char* buffer)
  73. {
  74. if (buffer != NULL) {
  75. // Strip leading white space from the string.
  76. char * source = buffer;
  77. while ((*source != 0) && ((unsigned char)*source <= 32))
  78. {
  79. source++;
  80. }
  81. if (source != buffer)
  82. {
  83. strcpy(buffer, source);
  84. }
  85. // Clip trailing white space from the string.
  86. for (int index = strlen(buffer)-1; index >= 0; index--)
  87. {
  88. if ((*source != 0) && ((unsigned char)buffer[index] <= 32))
  89. {
  90. buffer[index] = '\0';
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. }
  98. return buffer;
  99. }
  100. int APIENTRY WinMain(HINSTANCE hInstance,
  101. HINSTANCE hPrevInstance,
  102. LPSTR lpCmdLine,
  103. int nCmdShow)
  104. {
  105. /*
  106. ** Convert WinMain arguments to simple main argc and argv
  107. */
  108. int argc = 1;
  109. char * argv[20];
  110. argv[0] = NULL;
  111. char * token = strtok(lpCmdLine, " ");
  112. while (argc < 20 && token != NULL)
  113. {
  114. argv[argc++] = strtrim(token);
  115. token = strtok(NULL, " ");
  116. }
  117. int major = 1;
  118. int minor = 0;
  119. int build = 0;
  120. if (argc != 2)
  121. {
  122. usage(argv[0]);
  123. }
  124. else
  125. {
  126. char *target = argv[argc-1];
  127. FILE *filePtr;
  128. if (target) {
  129. filePtr = fopen(target, "r+");
  130. if (filePtr)
  131. {
  132. char buffer[256];
  133. char *stringPtr = NULL;
  134. while (!feof(filePtr))
  135. {
  136. fread(buffer, 256, 1, filePtr);
  137. if ((stringPtr = strstr(buffer, VERSION_STRING)) != NULL)
  138. {
  139. char *ptr;
  140. // Looking for '#define VERSION "x.y.z"'
  141. ptr = strtok(stringPtr, " "); // The VERSION
  142. ptr = strtok(NULL, "\n"); // The remainder
  143. if (*ptr == '\"')
  144. {
  145. ptr++; // Inc past the first "
  146. ptr = strtok(ptr, "."); // The first number
  147. major = atoi(ptr);
  148. ptr = strtok(NULL, "."); // The second number
  149. minor = atoi(ptr);
  150. ptr = strtok(NULL, "\""); // The final number
  151. build = atoi(ptr);
  152. fclose(filePtr);
  153. writeVersion(target, major, minor, ++build);
  154. printf ("Build %d Version %d.%d.%d\n", build, major, minor, build);
  155. break;
  156. } else
  157. {
  158. printf ("Build 0. Oops, didn't find a string of the format: '#define VERSION \"x.y.z\"'");
  159. }
  160. } // End if if (strstr
  161. } // End of while
  162. } // End of if filePtr
  163. else
  164. {
  165. // Didn't find the file, write a new one
  166. writeVersion(target, major, minor, build);
  167. }
  168. }
  169. }
  170. return 0;
  171. }