-
Notifications
You must be signed in to change notification settings - Fork 16
/
FileUtilities.h
107 lines (94 loc) · 2.37 KB
/
FileUtilities.h
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
107
////////////////////////////////////////////////////////////////////////////////////
// Copyright © Charalambos "Charis" Poullis, [email protected] //
// This work can only be used under an exclusive license of the author. //
////////////////////////////////////////////////////////////////////////////////////
#ifndef __FILE_UTILITIES_H__
#define __FILE_UTILITIES_H__
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
///Enumarated type for file types
typedef enum {
UNSUPPORTED_FORMAT = -1,
JPG,
PNG,
TGA,
BMP,
HDR,
PFM,
XYZ,
XYZI,
XYZRGB,
OBJ,
LAS,
TIF
} FILE_TYPE;
///Separates the file name from the extension
static bool separate(std::string const &file_name, std::string &name, FILE_TYPE &type) {
///The position of the "."
int pos = file_name.rfind('.');
///The name of the file
name = file_name.substr(0,pos-1);
///The extension of the file
std::string file_extension = &file_name.c_str()[pos+1];
if (strcasecmp(file_extension.c_str(),"jpg")==0 || strcasecmp(file_extension.c_str(),"jpeg")==0) {
type = JPG;
return true;
}
if (strcasecmp(file_extension.c_str(),"png")==0) {
type = PNG;
return true;
}
if (strcasecmp(file_extension.c_str(),"tga")==0) {
type = TGA;
return true;
}
if (strcasecmp(file_extension.c_str(),"hdr")==0) {
type = HDR;
return true;
}
if (strcasecmp(file_extension.c_str(),"pfm")==0) {
type = PFM;
return true;
}
if (strcasecmp(file_extension.c_str(),"xyz")==0) {
type = XYZ;
return true;
}
if (strcasecmp(file_extension.c_str(),"xyzi")==0) {
type = XYZI;
return true;
}
if (strcasecmp(file_extension.c_str(),"xyzrgb")==0) {
type = XYZRGB;
return true;
}
if (strcasecmp(file_extension.c_str(),"obj")==0) {
type = OBJ;
return true;
}
if (strcasecmp(file_extension.c_str(),"tiff")==0) {
type = TIF;
return true;
}
type = UNSUPPORTED_FORMAT;
return false;
}
static int getDirectoryContents(std::string const &dir, std::vector<std::string> &file_names) {
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
std::cout << "Error(" << errno << ") opening " << dir << std::endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
file_names.push_back(std::string(dirp->d_name));
}
closedir(dp);
return 0;
}
#endif