guess_extension.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "guess_extension.h"
  2. #include <string.h>
  3. #include "is_stl.h"
  4. IGL_INLINE void igl::guess_extension(FILE * fp, std::string & guess)
  5. {
  6. const auto is_off = [](FILE * fp)-> bool
  7. {
  8. char header[1000];
  9. const std::string OFF("OFF");
  10. const std::string NOFF("NOFF");
  11. const std::string COFF("COFF");
  12. bool f = (fscanf(fp,"%s\n",header)==1 && (
  13. std::string(header).compare(0, OFF.length(), OFF)==0 ||
  14. std::string(header).compare(0, COFF.length(), COFF)==0 ||
  15. std::string(header).compare(0,NOFF.length(),NOFF)==0));
  16. rewind(fp);
  17. return f;
  18. };
  19. const auto is_ply = [](FILE * fp) -> bool
  20. {
  21. char header[1000];
  22. const std::string PLY("ply");
  23. bool f = (fscanf(fp,"%s\n",header)==1 && (std::string(header).compare(0, PLY.length(), PLY)==0 ));
  24. rewind(fp);
  25. return f;
  26. };
  27. const auto is_wrl = [](FILE * wrl_file)->bool
  28. {
  29. bool still_comments = true;
  30. char line[1000];
  31. std::string needle("point [");
  32. std::string haystack;
  33. while(still_comments)
  34. {
  35. if(fgets(line,1000,wrl_file) == NULL)
  36. {
  37. rewind(wrl_file);
  38. return false;
  39. }
  40. haystack = std::string(line);
  41. still_comments = std::string::npos == haystack.find(needle);
  42. }
  43. rewind(wrl_file);
  44. return true;
  45. };
  46. const auto is_mesh = [](FILE * mesh_file )->bool
  47. {
  48. char line[2048];
  49. // eat comments at beginning of file
  50. bool still_comments= true;
  51. while(still_comments)
  52. {
  53. if(fgets(line,2048,mesh_file) == NULL)
  54. {
  55. rewind(mesh_file);
  56. return false;
  57. }
  58. still_comments = (line[0] == '#' || line[0] == '\n');
  59. }
  60. char str[2048];
  61. sscanf(line," %s",str);
  62. // check that first word is MeshVersionFormatted
  63. if(0!=strcmp(str,"MeshVersionFormatted"))
  64. {
  65. rewind(mesh_file);
  66. return false;
  67. }
  68. rewind(mesh_file);
  69. return true;
  70. };
  71. guess = "obj";
  72. if(is_mesh(fp))
  73. {
  74. guess = "mesh";
  75. }else if(is_off(fp))
  76. {
  77. guess = "off";
  78. }else if(is_ply(fp))
  79. {
  80. guess = "ply";
  81. }else if(igl::is_stl(fp))
  82. {
  83. guess = "stl";
  84. }else if(is_wrl(fp))
  85. {
  86. guess = "wrl";
  87. }
  88. // else obj
  89. rewind(fp);
  90. }
  91. IGL_INLINE std::string igl::guess_extension(FILE * fp)
  92. {
  93. std::string guess;
  94. guess_extension(fp,guess);
  95. return guess;
  96. }