137ea7254SIngo Weinhold /* 237ea7254SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 337ea7254SIngo Weinhold * Distributed under the terms of the MIT License. 437ea7254SIngo Weinhold */ 537ea7254SIngo Weinhold 637ea7254SIngo Weinhold #include "DataSource.h" 737ea7254SIngo Weinhold 837ea7254SIngo Weinhold #include <new> 937ea7254SIngo Weinhold 10*602a9fe5SIngo Weinhold #include <String.h> 11*602a9fe5SIngo Weinhold 1237ea7254SIngo Weinhold 1337ea7254SIngo Weinhold // #pragma mark - DataSource 1437ea7254SIngo Weinhold 1537ea7254SIngo Weinhold DataSource()1637ea7254SIngo WeinholdDataSource::DataSource() 1737ea7254SIngo Weinhold { 1837ea7254SIngo Weinhold } 1937ea7254SIngo Weinhold 2037ea7254SIngo Weinhold ~DataSource()2137ea7254SIngo WeinholdDataSource::~DataSource() 2237ea7254SIngo Weinhold { 2337ea7254SIngo Weinhold } 2437ea7254SIngo Weinhold 2537ea7254SIngo Weinhold 26*602a9fe5SIngo Weinhold status_t GetName(BString & name)27*602a9fe5SIngo WeinholdDataSource::GetName(BString& name) 28*602a9fe5SIngo Weinhold { 29*602a9fe5SIngo Weinhold return B_UNSUPPORTED; 30*602a9fe5SIngo Weinhold } 31*602a9fe5SIngo Weinhold 32*602a9fe5SIngo Weinhold 3337ea7254SIngo Weinhold // #pragma mark - FileDataSource 3437ea7254SIngo Weinhold 3537ea7254SIngo Weinhold 3637ea7254SIngo Weinhold 3737ea7254SIngo Weinhold status_t CreateDataIO(BDataIO ** _io)3837ea7254SIngo WeinholdFileDataSource::CreateDataIO(BDataIO** _io) 3937ea7254SIngo Weinhold { 4037ea7254SIngo Weinhold BFile* file = new(std::nothrow) BFile; 4137ea7254SIngo Weinhold if (file == NULL) 4237ea7254SIngo Weinhold return B_NO_MEMORY; 4337ea7254SIngo Weinhold 4437ea7254SIngo Weinhold status_t error = OpenFile(*file); 4537ea7254SIngo Weinhold if (error != B_OK) { 4637ea7254SIngo Weinhold delete file; 4737ea7254SIngo Weinhold return error; 4837ea7254SIngo Weinhold } 4937ea7254SIngo Weinhold 5037ea7254SIngo Weinhold *_io = file; 5137ea7254SIngo Weinhold return B_OK; 5237ea7254SIngo Weinhold } 5337ea7254SIngo Weinhold 5437ea7254SIngo Weinhold 5537ea7254SIngo Weinhold // #pragma mark - PathDataSource 5637ea7254SIngo Weinhold 5737ea7254SIngo Weinhold 5837ea7254SIngo Weinhold status_t Init(const char * path)5937ea7254SIngo WeinholdPathDataSource::Init(const char* path) 6037ea7254SIngo Weinhold { 6137ea7254SIngo Weinhold return fPath.SetTo(path); 6237ea7254SIngo Weinhold } 6337ea7254SIngo Weinhold 6437ea7254SIngo Weinhold 6537ea7254SIngo Weinhold status_t GetName(BString & name)66*602a9fe5SIngo WeinholdPathDataSource::GetName(BString& name) 67*602a9fe5SIngo Weinhold { 68*602a9fe5SIngo Weinhold if (fPath.Path() == NULL) 69*602a9fe5SIngo Weinhold return B_NO_INIT; 70*602a9fe5SIngo Weinhold 71*602a9fe5SIngo Weinhold name = fPath.Path(); 72*602a9fe5SIngo Weinhold return B_OK; 73*602a9fe5SIngo Weinhold } 74*602a9fe5SIngo Weinhold 75*602a9fe5SIngo Weinhold 76*602a9fe5SIngo Weinhold status_t OpenFile(BFile & file)7737ea7254SIngo WeinholdPathDataSource::OpenFile(BFile& file) 7837ea7254SIngo Weinhold { 7937ea7254SIngo Weinhold return file.SetTo(fPath.Path(), B_READ_ONLY); 8037ea7254SIngo Weinhold } 8137ea7254SIngo Weinhold 8237ea7254SIngo Weinhold 8337ea7254SIngo Weinhold // #pragma mark - EntryRefDataSource 8437ea7254SIngo Weinhold 8537ea7254SIngo Weinhold 8637ea7254SIngo Weinhold status_t Init(const entry_ref * ref)8737ea7254SIngo WeinholdEntryRefDataSource::Init(const entry_ref* ref) 8837ea7254SIngo Weinhold { 8937ea7254SIngo Weinhold if (ref->name == NULL) 9037ea7254SIngo Weinhold return B_BAD_VALUE; 9137ea7254SIngo Weinhold 9237ea7254SIngo Weinhold fRef = *ref; 9337ea7254SIngo Weinhold if (fRef.name == NULL) 9437ea7254SIngo Weinhold return B_NO_MEMORY; 9537ea7254SIngo Weinhold 9637ea7254SIngo Weinhold return B_OK; 9737ea7254SIngo Weinhold } 9837ea7254SIngo Weinhold 9937ea7254SIngo Weinhold 10037ea7254SIngo Weinhold status_t GetName(BString & name)101*602a9fe5SIngo WeinholdEntryRefDataSource::GetName(BString& name) 102*602a9fe5SIngo Weinhold { 103*602a9fe5SIngo Weinhold BEntry entry; 104*602a9fe5SIngo Weinhold status_t error = entry.SetTo(&fRef); 105*602a9fe5SIngo Weinhold if (error != B_OK) 106*602a9fe5SIngo Weinhold return error; 107*602a9fe5SIngo Weinhold 108*602a9fe5SIngo Weinhold BPath path; 109*602a9fe5SIngo Weinhold error = entry.GetPath(&path); 110*602a9fe5SIngo Weinhold if (error != B_OK) 111*602a9fe5SIngo Weinhold return error; 112*602a9fe5SIngo Weinhold 113*602a9fe5SIngo Weinhold name = path.Path(); 114*602a9fe5SIngo Weinhold return B_OK; 115*602a9fe5SIngo Weinhold } 116*602a9fe5SIngo Weinhold 117*602a9fe5SIngo Weinhold 118*602a9fe5SIngo Weinhold status_t OpenFile(BFile & file)11937ea7254SIngo WeinholdEntryRefDataSource::OpenFile(BFile& file) 12037ea7254SIngo Weinhold { 12137ea7254SIngo Weinhold return file.SetTo(&fRef, B_READ_ONLY); 12237ea7254SIngo Weinhold } 123