split FSSpecManager out so RM can use it (HOpenResFile)

This commit is contained in:
Kelvin Sherlock 2014-12-16 13:07:37 -05:00
parent 0b76b9266e
commit 85e515bdeb
4 changed files with 163 additions and 100 deletions

View File

@ -21,6 +21,7 @@ set(TOOLBOX_SRC
saneparser.cpp
pathnames.cpp
utility.cpp
fs_spec.cpp
)

113
toolbox/fs_spec.cpp Normal file
View File

@ -0,0 +1,113 @@
#include "fs_spec.h"
#include <algorithm>
#include <cassert>
namespace OS {
std::deque<FSSpecManager::Entry> FSSpecManager::_pathQueue;
void FSSpecManager::Init()
{
static bool initialized = false;
if (!initialized)
{
// "/" is item #1
//IDForPath("/", true);
static std::string RootPath("/");
std::hash<std::string> hasher;
size_t hash = hasher(RootPath);
_pathQueue.emplace_back(FSSpecManager::Entry(RootPath, hash));
assert(_pathQueue.size() == 1);
initialized = true;
}
}
int32_t FSSpecManager::IDForPath(const std::string &path, bool insert)
{
/*
char buffer[PATH_MAX + 1];
char *cp = realpath(path.c_str(), buffer);
if (!cp) return -1;
std::string s(cp);
*/
std::hash<std::string> hasher;
size_t hash = hasher(path);
Init();
int i = 1;
for (const auto &e : _pathQueue)
{
if (e.hash == hash && e.path == path) return i;
++i;
}
if (!insert) return -1;
_pathQueue.emplace_back(FSSpecManager::Entry(path, hash));
return _pathQueue.size();
}
int32_t FSSpecManager::IDForPath(std::string &&path, bool insert)
{
/*
char buffer[PATH_MAX + 1];
char *cp = realpath(path.c_str(), buffer);
if (!cp) return -1;
std::string s(cp);
*/
std::hash<std::string> hasher;
size_t hash = hasher(path);
Init();
int i = 1;
for (const auto &e : _pathQueue)
{
if (e.hash == hash && e.path == path) return i;
++i;
}
if (!insert) return -1;
_pathQueue.emplace_back(FSSpecManager::Entry(std::move(path), hash));
return _pathQueue.size();
}
const std::string &FSSpecManager::PathForID(int32_t id)
{
static std::string NullString;
Init();
if (id < 1) return NullString;
if (id > _pathQueue.size()) return NullString;
return _pathQueue[id - 1].path;
}
std::string FSSpecManager::ExpandPath(const std::string &path, int32_t id)
{
if (path.length() && path[0] == '/') return path;
if (id == 0) return path;
const std::string &dir = PathForID(id);
if (dir.empty()) return dir;
return dir + path;
}
}

42
toolbox/fs_spec.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __fs_spec_h__
#define __fs_spec_h__
#include <string>
#include <stdint.h>
#include <deque>
namespace OS {
class FSSpecManager
{
public:
static std::string ExpandPath(const std::string &path, int32_t id);
static const std::string &PathForID(int32_t id);
static int32_t IDForPath(const std::string &path, bool insert = true);
static int32_t IDForPath(std::string &&path, bool insert = true);
static void Init();
private:
struct Entry
{
Entry(const std::string &p, size_t h) :
path(p), hash(h)
{}
Entry(std::string &&p, size_t h) :
path(p), hash(h)
{}
std::string path;
size_t hash = 0;
};
static std::deque<Entry> _pathQueue;
};
}
#endif

View File

@ -51,102 +51,11 @@
#include "os_internal.h"
#include "toolbox.h"
#include "stackframe.h"
#include "fs_spec.h"
using ToolBox::Log;
using OS::Internal::errno_to_oserr;
namespace {
// FSSpec garbage
class FSSpecManager
{
public:
static const std::string &pathForID(int32_t id);
static int32_t idForPath(const std::string &path, bool insert = true);
static void Init();
private:
struct Entry
{
#if 0
Entry(std::string &&p) : path(p), hash(std::hash(path))
{}
Entry(const std::string &p) : path(p), hash(std::hash(path))
{}
#endif
Entry(const std::string &p, size_t h) :
path(p), hash(h)
{}
Entry(std::string &&p, size_t h) :
path(p), hash(h)
{}
std::string path;
size_t hash;
};
static std::deque<Entry> _pathQueue;
};
std::deque<FSSpecManager::Entry> FSSpecManager::_pathQueue;
void FSSpecManager::Init()
{
static bool initialized = false;
if (!initialized)
{
// "/" is item #1
idForPath("/", true);
initialized = true;
}
}
int32_t FSSpecManager::idForPath(const std::string &path, bool insert)
{
/*
char buffer[PATH_MAX + 1];
char *cp = realpath(path.c_str(), buffer);
if (!cp) return -1;
std::string s(cp);
*/
std::hash<std::string> hasher;
size_t hash = hasher(path);
int i = 1;
for (const auto &e : _pathQueue)
{
if (e.hash == hash && e.path == path) return i;
++i;
}
if (!insert) return -1;
_pathQueue.emplace_back(FSSpecManager::Entry(path, hash));
return _pathQueue.size();
}
const std::string &FSSpecManager::pathForID(int32_t id)
{
static std::string NullString;
if (id < 1) return NullString;
if (id > _pathQueue.size()) return NullString;
return _pathQueue[id - 1].path;
}
}
namespace OS {
@ -190,7 +99,7 @@ namespace OS {
{
// SC uses dirID + relative path.
std::string root = FSSpecManager::pathForID(dirID);
std::string root = FSSpecManager::PathForID(dirID);
if (root.empty())
{
std::memset(memoryPointer(spec), 0, 8);
@ -239,7 +148,7 @@ namespace OS {
path = path.substr(0, pos + 1); // include the /
}
int parentID = FSSpecManager::idForPath(path, true);
int parentID = FSSpecManager::IDForPath(path, true);
memoryWriteWord(vRefNum, spec + 0);
memoryWriteLong(parentID, spec + 2);
@ -274,7 +183,7 @@ namespace OS {
int parentID = memoryReadLong(spec + 2);
std::string leaf = ToolBox::ReadPString(spec + 6, false);
std::string path = FSSpecManager::pathForID(parentID);
std::string path = FSSpecManager::PathForID(parentID);
path += leaf;
@ -299,7 +208,7 @@ namespace OS {
int parentID = memoryReadLong(spec + 2);
std::string leaf = ToolBox::ReadPString(spec + 6, false);
std::string path = FSSpecManager::pathForID(parentID);
std::string path = FSSpecManager::PathForID(parentID);
path += leaf;
@ -325,7 +234,7 @@ namespace OS {
int parentID = memoryReadLong(spec + 2);
std::string leaf = ToolBox::ReadPString(spec + 6, false);
std::string path = FSSpecManager::pathForID(parentID);
std::string path = FSSpecManager::PathForID(parentID);
path += leaf;
@ -355,8 +264,6 @@ namespace OS {
uint16_t selector;
FSSpecManager::Init();
selector = cpuGetDReg(0) & 0xffff;
Log("%04x HighLevelHFSDispatch(%04x)\n", trap, selector);
@ -436,7 +343,7 @@ namespace OS {
if (ioDirID && !absolute)
{
std::string dir = FSSpecManager::pathForID(ioDirID);
std::string dir = FSSpecManager::PathForID(ioDirID);
if (dir.empty())
{
d0 = MacOS::dirNFErr;