* Copyright 2010, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ithamar R. Adema <ithamar.adema@team-embedded.nl>
*/
#include "PPDParser.h"
#include <File.h>
#include <Path.h>
PPDParser::PPDParser(BFile& file)
{
InitData(file);
}
PPDParser::PPDParser(const BDirectory& dir, const char* fname)
{
BFile file(&dir, fname, B_READ_ONLY);
InitData(file);
}
PPDParser::PPDParser(const BPath& path)
{
BFile file(path.Path(), B_READ_ONLY);
InitData(file);
}
status_t
PPDParser::InitData(BFile& file)
{
if ((fInitErr = file.InitCheck()) != B_OK)
return fInitErr;
ssize_t len;
char buffer[1025];
while ((len = file.Read(buffer, sizeof(buffer)-1)) > 0) {
buffer[len] = '\0';
fContent << buffer;
}
fContent.ReplaceAll("\r\n", "\n");
fContent.ReplaceAll("&&\n", "");
fContent << '\n';
return B_OK;
}
BString
PPDParser::GetParameter(const BString& param)
{
BString result, line;
int32 pos = 0, next;
BString pattern;
pattern << "*" << param << ":";
while ((next = fContent.FindFirst('\n', pos)) > 0) {
fContent.CopyInto(line, pos, next - pos);
if (line.Compare(pattern, pattern.Length()) == 0) {
line.CopyInto(result, pattern.Length(),
line.Length() - pattern.Length()).Trim();
if (result[0] == '"') {
result.Truncate(result.Length() -1);
result.Remove(0, 1);
}
break;
}
pos = next +1;
}
return result;
}
PPDParser::~PPDParser()
{
}