main.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "yariv.h"
  4. void *spirv;
  5. size_t spirv_size;
  6. static void replacestr(char *line, const char *search, const char *replace)
  7. {
  8. char *sp;
  9. if ((sp = strstr(line, search)) == NULL) {
  10. return;
  11. }
  12. int search_len = strlen(search);
  13. int replace_len = strlen(replace);
  14. int tail_len = strlen(sp+search_len);
  15. memmove(sp+replace_len,sp+search_len,tail_len+1);
  16. memcpy(sp, replace, replace_len);
  17. }
  18. int main(int argc,char* argv[]) {
  19. if(argc > 2) {
  20. printf("Too many arguments supplied. One argument expected.\n");
  21. return 0;
  22. }
  23. if(argc == 1) {
  24. printf("One argument expected.\n");
  25. return 0;
  26. }
  27. if(strstr(argv[1], ".spv") == NULL){
  28. printf("error in file name, need file.spv \n");
  29. return 0;
  30. }
  31. FILE *f = fopen(argv[1], "rb");
  32. if (f==NULL){
  33. printf("File not found.\n");
  34. return 0;
  35. }
  36. fseek(f, 0, SEEK_END);
  37. spirv_size = ftell(f);
  38. fseek(f, 0, SEEK_SET);
  39. spirv = malloc(spirv_size);
  40. fread(spirv, 1, spirv_size, f);
  41. fclose(f);
  42. void *out_yariv;
  43. size_t out_yariv_size;
  44. out_yariv_size = yariv_encode_size(yariv_encode_flags_strip, spirv, spirv_size);
  45. out_yariv = malloc(out_yariv_size);
  46. yariv_encode(yariv_encode_flags_strip, out_yariv, out_yariv_size, spirv, spirv_size);
  47. char str[200];
  48. strcpy(str,argv[1]);
  49. replacestr(str,".spv",".yariv");
  50. FILE *fp;
  51. fp = fopen(str, "w");
  52. fwrite(out_yariv,out_yariv_size,1,fp);
  53. fclose(fp);
  54. free(out_yariv);
  55. printf("converted %s to %s yariv_size %ld \n",argv[1],str,out_yariv_size);
  56. }