Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #define NUM_PER_LINE 40
  13. extern char* szHeaderFile;
  14. void printUsage()
  15. {
  16. printf("res2c <res file name> <c file name> <object name>\n");
  17. }
  18. char* id4filename(const char* filename)
  19. {
  20. static char id[500];
  21. const char* read=filename;
  22. char *write=id;
  23. for (read;*read;read++)
  24. {
  25. //if first char
  26. if (read==filename)
  27. {
  28. if (isalpha(*read) || *read=='_')
  29. *write++=*read;
  30. }
  31. else if (isalnum(*read))
  32. *write++=*read;
  33. }
  34. *write++='s';
  35. *write++='r';
  36. *write++='c';
  37. *write++='\0';
  38. return id;
  39. }
  40. void main(int argc, const char* argv[])
  41. {
  42. if (argc < 4)
  43. {
  44. printUsage();
  45. return;
  46. }
  47. char cppname[200];
  48. sprintf(cppname,"%s.cpp",argv[2]);
  49. char hname[200];
  50. sprintf(hname,"%s.h",argv[2]);
  51. FILE* f=fopen(argv[1],"rb");
  52. FILE* cppout=fopen(cppname,"at");
  53. FILE* hout=fopen(hname,"at");
  54. FILE* brheader=fopen("BinaryResource.h","wt");
  55. if (!brheader){printf("couldn't open %s to write\n","BinaryResource.h");exit(-1);}
  56. if (!f){printf("couldn't read %s\n",argv[1]);exit(-1);}
  57. if (!cppout){printf("couldn't open %s to write\n",argv[2]);exit(-1);}
  58. if (!hout){printf("couldn't open %s to write\n",argv[2]);exit(-1);}
  59. fprintf(brheader,szHeaderFile);
  60. fclose(brheader);
  61. fprintf(cppout,"\nunsigned char %s[]={\n",id4filename(argv[1]));
  62. int numLeft4Line=NUM_PER_LINE;
  63. unsigned char c;
  64. int result=fread(&c,sizeof(unsigned char),1,f);
  65. int numbytes=0;
  66. while (result)
  67. {
  68. //int longc=(*c)&0x000000ff;
  69. fprintf(cppout,"0x%02.2x,",c);
  70. numbytes++;
  71. if(--numLeft4Line==0)
  72. {
  73. numLeft4Line=NUM_PER_LINE;
  74. fprintf(cppout,"\n");
  75. }
  76. result=fread(&c,sizeof(unsigned char),1,f);
  77. }
  78. fprintf(cppout,"\n};\n\n");
  79. char* coloncolon=strstr(argv[3],"::");
  80. if (coloncolon!=NULL)
  81. {
  82. coloncolon+=2;
  83. fprintf(hout,"static CBinaryResource %s;\n",coloncolon);
  84. fprintf(cppout,"CBinaryResource %s(\"%s\",%li,%s);\n\n\n",argv[3],argv[1],numbytes,id4filename(argv[1]));
  85. }
  86. else
  87. {
  88. fprintf(hout,"//extern CBinaryResource g_%s;\n",argv[3]);
  89. fprintf(cppout,"CBinaryResource g_%s;\n",argv[3]);
  90. }
  91. fclose(cppout);
  92. fclose(hout);
  93. fclose(f);
  94. }