File: rtfile.epoc.cpp

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (210 lines) | stat: -rwxr-xr-x 5,410 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * rtfile.epoc.cpp
 * 
 * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
 * and Martin Trautmann (martintrautmann@gmx.de) 
 * 
 * This file may be distributed and/or modified under the terms of the 
 * GNU General Public License version 2 as published by the Free Software 
 * Foundation. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 */

#ifndef __LRT_FILE_EPOC__
#define __LRT_FILE_EPOC__

#include "rtfile.h"

#include <f32file.h>

#include "rtetools.h"

#include "rtcollect.h"
#include "rtstring.h"
#include "rtchar.h"

namespace lrt {

const int File::minNameCount = 1;
const char File::separatorChar = '\\';
const String File::separator = "\\";
const bool File::caseSensitive = false;

const TTime TIMEepoch(TDateTime(1970, EJanuary, 0, 0, 0, 0, 0));

//////// Private Helper Functions /////////////
// checks if the drive specified in the given file name exists
bool driveExists(const String& fname, RFs& session)
{
	if(fname.length() == 0) return false;
	int driveNum = Char::lowerCase(fname[0]) - 'a';
	if((driveNum < 0) || (driveNum >= 26)) 
		return false;
	TDriveList drives;
	if(session.DriveList(drives) < 0)
		return false;
	return drives[driveNum];
}

// copies the file attributes from the entry to the file
void File::setAttribs(TEntry& entry)
{
	attribs->exists = ATTRIB_TRUE;
	attribs->canRead = ATTRIB_TRUE;
	attribs->canWrite = (entry.IsReadOnly() ? ATTRIB_FALSE : ATTRIB_TRUE);
	attribs->isFolder = (entry.IsDir() ? ATTRIB_TRUE : ATTRIB_FALSE);
	attribs->size = entry.iSize;
	TTimeIntervalSeconds ti;
	entry.iModified.SecondsFrom(TIMEepoch, ti);
	attribs->lastModified.sec = ti.Int();
}

/////// Real functions /////////////////

void File::initialize(const String &executableFileName)
{
	currentFolder = new File("C:\\");
	currentFolder->attribs->isFolder = ATTRIB_TRUE;
	executableFile = new File(executableFileName);
	File execFolder = executableFile->getParentFile();
	homeFolder = new File(execFolder);
	settingsFolder = new File(execFolder);
	delete currentFolder;
	currentFolder = new File(execFolder);
}

// Same as in Windows
bool File::resolveExtra(File *file, const String &fname) const
{
	if(fname.startsWith("\\"))
	{
		Array<String> *nNames = new Array<String>(file->names->length() + 1);
		(*nNames)[0] = (*names)[0];
		for(int i = 0; i < file->names->length(); i++)
			(*nNames)[i+1] = (*(file->names))[i];
		file->set(nNames);
		return true;
	}
	return false;
}

bool File::createSingleFolder(const File& folder)
{
	String fname = folder.getName();
	RFs session;
	if(session.Connect() < 0) return false;
	bool ret;
	ret = (session.MkDir(ESTRING(fname)) >= 0);
	session.Close();
	return ret;
}

// How?
void File::fetchAttributes()
{
	RFs session;
	if(session.Connect() < 0) return;
	unsigned int attr = 0;
	String fname = getName();

	if(names->length() == 1) // volumes must be handled separately, Att() fails for them
	{
		attribs->exists = (driveExists(fname, session) ? ATTRIB_TRUE : ATTRIB_FALSE);
		attribs->canRead = ATTRIB_FALSE;
		attribs->canWrite = ATTRIB_FALSE;
		return;
	}

	int ret = session.Att(ESTRING(fname), attr);
	if(ret == KErrNotFound) // file doesn't exist
	{
		attribs->exists = ATTRIB_FALSE;
		attribs->canRead = ATTRIB_FALSE;
		attribs->canWrite = ATTRIB_TRUE;
	}
	else if(ret < 0) { // Att() fails for volume identifiers (ex: "C:\"), try to catch that
		System::println("Att() failed for " + fname); 
		return; 
	}

	else { // file exists
		TEntry entry;
		if(!session.Entry(ESTRING(fname), entry)) // success
		{
			setAttribs(entry);
		}
		else 
		{
			attribs->exists = ATTRIB_TRUE;
			attribs->canRead = ATTRIB_TRUE;
			attribs->canWrite = ((attr & KEntryAttReadOnly) ? ATTRIB_FALSE : ATTRIB_TRUE);
			attribs->isFolder = ((attr & KEntryAttDir) ? ATTRIB_TRUE : ATTRIB_FALSE);
		}
	}
	session.Close();
}

// Same as in Windows
bool File::isAbsolute(const String &fname)
{
	if(fname.length() < 2) return false;
	if((fname[1] == ':') && Char::isLetter(fname[0]))
		return true;
	return false;

}

String File::getAbsolutePrefix()
{
  return String();
}

// How?
Array<File> File::listImpl(const IFilenameFilter *filter)
{
	bool acceptAll = false;
	if(filter == 0) 
		acceptAll = true;
	
	File parentFile = (isFolder() ? (*this) : getParentFile());
	String search = (isFolder() ? getName() : parentFile.getName());
	search += "*";

	Vector<File> ret(0);

	RFs session;
	if(session.Connect() < 0) { System::println("Can't connect!"); return ret; }
	CDir *dirList;
	
    if(session.GetDir(ESTRING(search),
		KEntryAttMaskSupported,ESortByName,dirList) < 0) {
		System::println("Can't GetDir!"); delete[] dirList; return ret; }
    for(int i = 0; i < dirList->Count(); i++)
	{
		TEntry curEntry = (*dirList)[i];
		String file = MYSTRING_C(curEntry.iName);
		if((file == ".") || (file == "..")) // don't include them
			continue;
		if(curEntry.IsDir()) file += separatorChar;
		if(acceptAll || filter->accept(parentFile, file))
		{
			File r = File(parentFile, file);
			r.setAttribs(curEntry);
		    ret += r;
		}
	}
	
	delete dirList;
	session.Close();

	return ret;

}

} // namespace


#endif