1/* 2* Copyright 2010, Haiku. All rights reserved. 3* Distributed under the terms of the MIT License. 4* 5* Authors: 6* Ithamar R. Adema <ithamar.adema@team-embedded.nl> 7*/ 8 9 10#include "PPDParser.h" 11 12#include <File.h> 13#include <Path.h> 14 15 16PPDParser::PPDParser(BFile& file) 17{ 18 InitData(file); 19} 20 21 22PPDParser::PPDParser(const BDirectory& dir, const char* fname) 23{ 24 BFile file(&dir, fname, B_READ_ONLY); 25 InitData(file); 26} 27 28 29PPDParser::PPDParser(const BPath& path) 30{ 31 BFile file(path.Path(), B_READ_ONLY); 32 InitData(file); 33} 34 35 36status_t 37PPDParser::InitData(BFile& file) 38{ 39 // Check if file exists... 40 if ((fInitErr = file.InitCheck()) != B_OK) 41 return fInitErr; 42 43 // Read entire file into BString 44 ssize_t len; 45 char buffer[1025]; 46 while ((len = file.Read(buffer, sizeof(buffer)-1)) > 0) { 47 buffer[len] = '\0'; 48 fContent << buffer; 49 } 50 51 // Convert DOS/Windows newlines to UNIX ones 52 fContent.ReplaceAll("\r\n", "\n"); 53 // Handle line continuation 54 fContent.ReplaceAll("&&\n", ""); 55 // Make sure file ends with newline 56 fContent << '\n'; 57 58 return B_OK; 59} 60 61 62BString 63PPDParser::GetParameter(const BString& param) 64{ 65 BString result, line; 66 int32 pos = 0, next; 67 BString pattern; 68 69 pattern << "*" << param << ":"; 70 71 while ((next = fContent.FindFirst('\n', pos)) > 0) { 72 // Grab line (without newline) 73 fContent.CopyInto(line, pos, next - pos); 74 // Found our parameter? 75 if (line.Compare(pattern, pattern.Length()) == 0) { 76 // Copy result 77 line.CopyInto(result, pattern.Length(), 78 line.Length() - pattern.Length()).Trim(); 79 // If result is quoted, remove quotes 80 if (result[0] == '"') { 81 result.Truncate(result.Length() -1); 82 result.Remove(0, 1); 83 } 84 break; 85 } 86 pos = next +1; 87 } 88 89 return result; 90} 91 92 93PPDParser::~PPDParser() 94{ 95} 96