[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3Common / b3FileUtils.h
1 #ifndef B3_FILE_UTILS_H
2 #define B3_FILE_UTILS_H
3
4 #include <stdio.h>
5 #include "b3Scalar.h"
6 #include <stddef.h>  //ptrdiff_h
7 #include <string.h>
8
9 struct b3FileUtils
10 {
11         b3FileUtils()
12         {
13         }
14         virtual ~b3FileUtils()
15         {
16         }
17
18         static bool findFile(const char* orgFileName, char* relativeFileName, int maxRelativeFileNameMaxLen)
19         {
20                 FILE* f = 0;
21                 f = fopen(orgFileName, "rb");
22                 if (f)
23                 {
24                         //printf("original file found: [%s]\n", orgFileName);
25                         sprintf(relativeFileName, "%s", orgFileName);
26                         fclose(f);
27                         return true;
28                 }
29
30                 //printf("Trying various directories, relative to current working directory\n");
31                 const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
32                 int numPrefixes = sizeof(prefix) / sizeof(const char*);
33
34                 f = 0;
35                 bool fileFound = false;
36
37                 for (int i = 0; !f && i < numPrefixes; i++)
38                 {
39 #ifdef _MSC_VER
40                         sprintf_s(relativeFileName, maxRelativeFileNameMaxLen, "%s%s", prefix[i], orgFileName);
41 #else
42                         sprintf(relativeFileName, "%s%s", prefix[i], orgFileName);
43 #endif
44                         f = fopen(relativeFileName, "rb");
45                         if (f)
46                         {
47                                 fileFound = true;
48                                 break;
49                         }
50                 }
51                 if (f)
52                 {
53                         fclose(f);
54                 }
55
56                 return fileFound;
57         }
58
59         static const char* strip2(const char* name, const char* pattern)
60         {
61                 size_t const patlen = strlen(pattern);
62                 size_t patcnt = 0;
63                 const char* oriptr;
64                 const char* patloc;
65                 // find how many times the pattern occurs in the original string
66                 for (oriptr = name; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
67                 {
68                         patcnt++;
69                 }
70                 return oriptr;
71         }
72
73         static int extractPath(const char* fileName, char* path, int maxPathLength)
74         {
75                 const char* stripped = strip2(fileName, "/");
76                 stripped = strip2(stripped, "\\");
77
78                 ptrdiff_t len = stripped - fileName;
79                 b3Assert((len + 1) < maxPathLength);
80
81                 if (len && ((len + 1) < maxPathLength))
82                 {
83                         for (int i = 0; i < len; i++)
84                         {
85                                 path[i] = fileName[i];
86                         }
87                         path[len] = 0;
88                 }
89                 else
90                 {
91                         len = 0;
92                         b3Assert(maxPathLength > 0);
93                         if (maxPathLength > 0)
94                         {
95                                 path[len] = 0;
96                         }
97                 }
98                 return len;
99         }
100
101         static char toLowerChar(const char t)
102         {
103                 if (t >= (char)'A' && t <= (char)'Z')
104                         return t + ((char)'a' - (char)'A');
105                 else
106                         return t;
107         }
108
109         static void toLower(char* str)
110         {
111                 int len = strlen(str);
112                 for (int i = 0; i < len; i++)
113                 {
114                         str[i] = toLowerChar(str[i]);
115                 }
116         }
117
118         /*static const char* strip2(const char* name, const char* pattern)
119         {
120                 size_t const patlen = strlen(pattern);
121                 size_t patcnt = 0;
122                 const char * oriptr;
123                 const char * patloc;
124                 // find how many times the pattern occurs in the original string
125                 for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
126                 {
127                         patcnt++;
128                 }
129                 return oriptr;
130         }
131         */
132 };
133 #endif  //B3_FILE_UTILS_H