1
0

DrawUI_FixMenu.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // This is free and unencumbered software released into the public domain.
  2. // For more information, please refer to https://github.com/hongwenjun
  3. // 兰雅VBA代码分享 https://lyvba.com/
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <string>
  7. using std::string;
  8. bool readfile_DrawUI(string &str, const char *filename);
  9. bool savefile_DrawUI(string &str, const char *filename);
  10. int main() {
  11. const char *filename = "DrawUI.xml";
  12. FILE *fp = fopen(filename, "r");
  13. if (fp == NULL) {
  14. printf("无法打开文件 DrawUI.xml\n");
  15. return 1;
  16. }
  17. string drawui_text;
  18. bool flag = readfile_DrawUI(drawui_text, filename);
  19. bool saveflag = false;
  20. printf("%d\n", drawui_text.size());
  21. char guid[] = "f3016f3c-2847-4557-b61a-a2d05319cf18";
  22. char line[2048];
  23. string str;
  24. string menu = "type=\"menu\"";
  25. string toolbar = "type=\"toolbar\"";
  26. while (fgets(line, sizeof(line), fp) != NULL) {
  27. line[strlen(line) - 1] = '\0';
  28. if (strstr(line, guid) != NULL) {
  29. str = string(line);
  30. // 使用 C++ 标准库的 replace() 方法替换单词
  31. size_t pos = str.find(menu);
  32. if (pos != string::npos) {
  33. str.replace(pos, menu.length(), toolbar);
  34. printf("%s", str.c_str());
  35. pos = drawui_text.find(string(line));
  36. if (pos != string::npos) {
  37. saveflag = savefile_DrawUI(drawui_text, "DrawUI.xml_BAK");
  38. drawui_text.replace(pos, string(line).length(), str);
  39. }
  40. }
  41. }
  42. }
  43. fclose(fp);
  44. if (saveflag)
  45. savefile_DrawUI(drawui_text, filename);
  46. return 0;
  47. }
  48. bool readfile_DrawUI(string &str, const char *filename) {
  49. FILE *fp;
  50. long size;
  51. char *buffer;
  52. // 打开文件
  53. fp = fopen(filename, "rb");
  54. if (fp == NULL) {
  55. printf("打开文件失败\n");
  56. return false;
  57. }
  58. // 获取文件大小
  59. fseek(fp, 0, SEEK_END);
  60. size = ftell(fp);
  61. rewind(fp);
  62. // 分配内存空间
  63. buffer = (char *)malloc(size + 1);
  64. if (buffer == NULL) {
  65. printf("内存分配失败\n");
  66. fclose(fp);
  67. return false;
  68. }
  69. // 读取文件内容到内存
  70. if (fread(buffer, 1, size, fp) != size) {
  71. printf("读取文件失败\n");
  72. free(buffer);
  73. fclose(fp);
  74. return false;
  75. }
  76. // 添加字符串结尾标志
  77. buffer[size] = '\0';
  78. str = string(buffer);
  79. // 释放资源
  80. free(buffer);
  81. fclose(fp);
  82. return true;
  83. }
  84. bool savefile_DrawUI(string &str, const char *filename) {
  85. FILE *fp;
  86. const char *content = str.c_str();
  87. // 打开文件,以写入模式
  88. fp = fopen(filename, "wb");
  89. if (fp == NULL) {
  90. printf("打开文件失败\n");
  91. return false;
  92. }
  93. // 写入字符串到文件
  94. fputs(content, fp);
  95. // 关闭文件
  96. fclose(fp);
  97. return true;
  98. }