1/* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6#include "DataSource.h" 7 8#include <new> 9 10#include <String.h> 11 12 13// #pragma mark - DataSource 14 15 16DataSource::DataSource() 17{ 18} 19 20 21DataSource::~DataSource() 22{ 23} 24 25 26status_t 27DataSource::GetName(BString& name) 28{ 29 return B_UNSUPPORTED; 30} 31 32 33// #pragma mark - FileDataSource 34 35 36 37status_t 38FileDataSource::CreateDataIO(BDataIO** _io) 39{ 40 BFile* file = new(std::nothrow) BFile; 41 if (file == NULL) 42 return B_NO_MEMORY; 43 44 status_t error = OpenFile(*file); 45 if (error != B_OK) { 46 delete file; 47 return error; 48 } 49 50 *_io = file; 51 return B_OK; 52} 53 54 55// #pragma mark - PathDataSource 56 57 58status_t 59PathDataSource::Init(const char* path) 60{ 61 return fPath.SetTo(path); 62} 63 64 65status_t 66PathDataSource::GetName(BString& name) 67{ 68 if (fPath.Path() == NULL) 69 return B_NO_INIT; 70 71 name = fPath.Path(); 72 return B_OK; 73} 74 75 76status_t 77PathDataSource::OpenFile(BFile& file) 78{ 79 return file.SetTo(fPath.Path(), B_READ_ONLY); 80} 81 82 83// #pragma mark - EntryRefDataSource 84 85 86status_t 87EntryRefDataSource::Init(const entry_ref* ref) 88{ 89 if (ref->name == NULL) 90 return B_BAD_VALUE; 91 92 fRef = *ref; 93 if (fRef.name == NULL) 94 return B_NO_MEMORY; 95 96 return B_OK; 97} 98 99 100status_t 101EntryRefDataSource::GetName(BString& name) 102{ 103 BEntry entry; 104 status_t error = entry.SetTo(&fRef); 105 if (error != B_OK) 106 return error; 107 108 BPath path; 109 error = entry.GetPath(&path); 110 if (error != B_OK) 111 return error; 112 113 name = path.Path(); 114 return B_OK; 115} 116 117 118status_t 119EntryRefDataSource::OpenFile(BFile& file) 120{ 121 return file.SetTo(&fRef, B_READ_ONLY); 122} 123