Welcome to mirror list, hosted at ThFree Co, Russian Federation.

guess_extension.cpp « igl « libigl « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e8e449a1c383948d6f0a21c4e80cd511e41c358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "guess_extension.h"

#include <string.h>

#include "is_stl.h"
#include "ply.h"

IGL_INLINE void igl::guess_extension(FILE * fp, std::string & guess)
{
  const auto is_off = [](FILE * fp)-> bool
  {
    char header[1000];
    const std::string OFF("OFF");
    const std::string NOFF("NOFF");
    const std::string COFF("COFF");
    bool f = (fscanf(fp,"%s\n",header)==1 && (
        std::string(header).compare(0, OFF.length(), OFF)==0 ||
        std::string(header).compare(0, COFF.length(), COFF)==0 ||
        std::string(header).compare(0,NOFF.length(),NOFF)==0));
    rewind(fp);
    return f;
  };
  const auto is_ply = [](FILE * ply_file) -> bool
  {
    int nelems;
    char ** elem_names;
    igl::ply::PlyFile * in_ply = igl::ply::ply_read(ply_file,&nelems,&elem_names);
    if(in_ply==NULL)
    {
      return false;
    }
    free(in_ply);
    rewind(ply_file);
    return true;
  };
  const auto is_wrl = [](FILE * wrl_file)->bool
  {
    bool still_comments = true;
    char line[1000];
    std::string needle("point [");
    std::string haystack;
    while(still_comments)
    {
      if(fgets(line,1000,wrl_file) == NULL)
      {
        rewind(wrl_file);
        return false;
      }
      haystack = std::string(line);
      still_comments = std::string::npos == haystack.find(needle);
    }
    rewind(wrl_file);
    return true;
  };
  const auto is_mesh = [](FILE * mesh_file )->bool
  {
    char line[2048];
    // eat comments at beginning of file
    bool still_comments= true;
    while(still_comments)
    {
      if(fgets(line,2048,mesh_file) == NULL)
      {
        rewind(mesh_file);
        return false;
      }
      still_comments = (line[0] == '#' || line[0] == '\n');
    }
    char str[2048];
    sscanf(line," %s",str);
    // check that first word is MeshVersionFormatted
    if(0!=strcmp(str,"MeshVersionFormatted"))
    {
      rewind(mesh_file);
      return false;
    }
    rewind(mesh_file);
    return true;
  };
  guess = "obj";
  if(is_mesh(fp))
  {
    guess = "mesh";
  }else if(is_off(fp))
  {
    guess = "off";
  }else if(is_ply(fp))
  {
    guess = "ply";
  }else if(igl::is_stl(fp))
  {
    guess = "stl";
  }else if(is_wrl(fp))
  {
    guess = "wrl";
  }
  // else obj
  rewind(fp);
}

IGL_INLINE std::string igl::guess_extension(FILE * fp)
{
  std::string guess;
  guess_extension(fp,guess);
  return guess;
}