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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/**
* ======================== legal notice ======================
*
* File: FileSystem.cc
* Created: 21. Juli 2012, 12
* Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a>
* Project: libfsScan: mediatypes and filesystem scanning
*
* CMP - compound media player
*
* is a client/server mediaplayer intended to play any media from any workstation
* without the need to export or mount shares. cmps is an easy to use backend
* with a (ready to use) HTML-interface. Additionally the backend supports
* authentication via HTTP-digest authorization.
* cmpc is a client with vdr-like osd-menues.
*
* Copyright (c) 2012 Reinhard Mantey, some rights reserved!
* published under Creative Commons by-sa
* For details see http://creativecommons.org/licenses/by-sa/3.0/
*
* The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
*
* --------------------------------------------------------------
*/
#include <FileSystem.h>
#include <FileRepresentation.h>
#include <File.h>
#include <StringBuilder.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
char cFileSystem::PathSeparator = '/';
char cFileSystem::RootPath[4] = { PathSeparator, 0, 0, 0 };
cFileSystem::cFileSystem()
{
}
cFileSystem::~cFileSystem()
{
std::tr1::unordered_map<std::string, cFileRepresentation *>::iterator it = fileCache.begin();
while (it != fileCache.end()) {
if (it->second) delete it->second;
++it;
}
}
cFileRepresentation *cFileSystem::cacheEntry(const char* Path)
{
if (fileCache.empty()) return NULL;
std::tr1::unordered_map<std::string, cFileRepresentation *>::iterator it = fileCache.find(Path);
if (it != fileCache.end()) return it->second;
return NULL;
}
cFileRepresentation *cFileSystem::representationOfFile(const cFile &Parent, const char* Path)
{
cFileRepresentation *parentRep = (cFileRepresentation *) Parent.rep;
cFileRepresentation *tmp = NULL;
cStringBuilder *sb = new cStringBuilder(Parent.AbsolutePath());
char *scratch = strdup(Path);
char *last = scratch + strlen(Path);
char *start = *scratch == PathSeparator ? scratch + 1 : scratch;
char *end = strchr(scratch, PathSeparator);
char *path;
while (start < last) {
end = strchr(start, PathSeparator);
if (!end) end = start + strlen(start);
*end = 0;
sb->Append(PathSeparator).Append(start);
path = sb->toString();
tmp = cacheEntry(path);
if (!tmp) {
tmp = new cFileRepresentation(parentRep, start);
fileCache[path] = tmp;
}
parentRep = tmp;
free(path);
start = end + 1;
}
free(scratch);
delete sb;
if (tmp) return tmp;
return NULL;
}
cFileRepresentation *cFileSystem::representationOfFile(const char* Path)
{
cFileRepresentation *rv = cacheEntry(Path);
if (!rv) {
cFileRepresentation *tmp = NULL;
char *scratch = strdup(Path);
char *p;
for (p = strrchr(scratch, PathSeparator); !rv && p; p = strrchr(scratch, PathSeparator)) {
if (p > scratch) *p = 0;
else {
if (!(rv = cacheEntry(RootPath))) {
*p = 0;
tmp = new cFileRepresentation(RootPath);
fileCache[RootPath] = tmp;
break;
}
}
if ((tmp = cacheEntry(scratch))) break;
}
if (tmp) {
cFileRepresentation *parent = tmp;
size_t poolSize = fileCache.size();
const char *last = scratch + strlen(Path);
for (p = scratch + strlen(scratch) + 1; p < last; p += strlen(p) + 1) {
if (!*(p - 1)) *(p - 1) = PathSeparator;
tmp = new cFileRepresentation(parent, p);
fileCache[scratch] = tmp;
poolSize = fileCache.size();
parent = tmp;
}
rv = parent;
}
free(scratch);
}
return rv;
}
|