* Again, I forgot to svn add some files. Sorry. Build should be fixed.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24719 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2008-04-01 13:49:39 +00:00
parent c41a74e8f3
commit c397aae3cc
4 changed files with 316 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* Copyright 2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Michael Pfeiffer <laplace@users.sourceforge.net>
* Fredrik Modéen <fredrik@modeen.se>
*/
#include "FileReadWrite.h"
#include <UTF8.h>
#include <Path.h>
FileReadWrite::FileReadWrite(BFile *file, int32 sourceEncoding)
: fFile(file),
fSourceEncoding(sourceEncoding)
{}
void
FileReadWrite::SetEncoding(int32 sourceEncoding)
{
fSourceEncoding = sourceEncoding;
}
uint32
FileReadWrite::GetEncoding()
{
return fSourceEncoding;
}
status_t
FileReadWrite::Write(const BString& contents)const
{
ssize_t sz = fFile->Write(contents.String(), contents.Length());
if (sz != contents.Length())
return sz < 0 ? sz : B_IO_ERROR;
else
return B_OK;
}
bool
FileReadWrite::Next(BString& string)
{
// Fill up the buffer with the first chunk of code
if (fPositionInBuffer == 0)
fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer));
while (fAmtRead > 0) {
while (fPositionInBuffer < fAmtRead) {
// Return true if we hit a newline or the end of the file
if (fBuffer[fPositionInBuffer] == '\n') {
fPositionInBuffer++;
//Convert begin
int32 state = 0;
int32 bufferLen = string.Length();
int32 destBufferLen = bufferLen;
char destination[destBufferLen];
if (fSourceEncoding)
convert_to_utf8(fSourceEncoding, string.String(), &bufferLen, destination, &destBufferLen, &state);
string = destination;
return true;
}
string += fBuffer[fPositionInBuffer];
fPositionInBuffer++;
}
// Once the buffer runs out, grab some more and start again
fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer));
fPositionInBuffer = 0;
}
return false;
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Michael Pfeiffer <laplace@users.sourceforge.net>
* Fredrik Modéen <fredrik@modeen.se>
*/
#ifndef _FILEREADRWITE_
#define _FILEREADRWITE_
#include <stdio.h>
#include <File.h>
#include <String.h>
class FileReadWrite {
public:
// -1 defult encoding
FileReadWrite(BFile *file, int32 sourceEncoding = -1);
bool Next(BString& string);
status_t Write(const BString& contents)const;
void SetEncoding(int32 sourceEncoding);
uint32 GetEncoding();
private:
BFile* fFile;
int32 fSourceEncoding;
char fBuffer[4096];
off_t fPositionInBuffer;
ssize_t fAmtRead;
};
#endif //_FILEREADRWITE_

View File

@ -0,0 +1,138 @@
//Volume II, Issue 35; September 2, 1998 (Eric Shepherd)
#include "TPreferences.h"
#include <FindDirectory.h>
#include <File.h>
TPreferences::TPreferences(directory_which directory, const char *filename)
:BMessage('pref')
{
BFile file;
fStatus = find_directory(directory, &fPath);
if (fStatus != B_OK)
return;
fPath.Append(filename);
fStatus = file.SetTo(fPath.Path(), B_READ_ONLY);
if (fStatus == B_OK)
fStatus = Unflatten(&file);
}
TPreferences::~TPreferences()
{
BFile file;
if (file.SetTo(fPath.Path(), B_WRITE_ONLY | B_CREATE_FILE) == B_OK)
Flatten(&file);
}
status_t
TPreferences::SetBool(const char *name, bool b)
{
if (HasBool(name))
return ReplaceBool(name, 0, b);
return AddBool(name, b);
}
status_t
TPreferences::SetInt8(const char *name, int8 i)
{
if (HasInt8(name))
return ReplaceInt8(name, 0, i);
return AddInt8(name, i);
}
status_t
TPreferences::SetInt16(const char *name, int16 i)
{
if (HasInt16(name))
return ReplaceInt16(name, 0, i);
return AddInt16(name, i);
}
status_t
TPreferences::SetInt32(const char *name, int32 i)
{
if (HasInt32(name))
return ReplaceInt32(name, 0, i);
return AddInt32(name, i);
}
status_t
TPreferences::SetInt64(const char *name, int64 i)
{
if (HasInt64(name))
return ReplaceInt64(name, 0, i);
return AddInt64(name, i);
}
status_t
TPreferences::SetFloat(const char *name, float f)
{
if (HasFloat(name))
return ReplaceFloat(name, 0, f);
return AddFloat(name, f);
}
status_t
TPreferences::SetDouble(const char *name, double f)
{
if (HasDouble(name))
return ReplaceDouble(name, 0, f);
return AddDouble(name, f);
}
status_t
TPreferences::SetString(const char *name, const char *s)
{
if (HasString(name))
return ReplaceString(name, 0, s);
return AddString(name, s);
}
status_t
TPreferences::SetPoint(const char *name, BPoint p)
{
if (HasPoint(name))
return ReplacePoint(name, 0, p);
return AddPoint(name, p);
}
status_t
TPreferences::SetRect(const char *name, BRect r)
{
if (HasRect(name))
return ReplaceRect(name, 0, r);
return AddRect(name, r);
}
status_t
TPreferences::SetMessage(const char *name, const BMessage *message)
{
if (HasMessage(name))
return ReplaceMessage(name, 0, message);
return AddMessage(name, message);
}
status_t
TPreferences::SetFlat(const char *name, const BFlattenable *obj)
{
if (HasFlat(name, obj))
return ReplaceFlat(name, 0, (BFlattenable *) obj);
return AddFlat(name, (BFlattenable *) obj);
}

View File

@ -0,0 +1,68 @@
#ifndef __TPREFS_H__
#define __TPREFS_H__
//Volume II, Issue 35; September 2, 1998 (Eric Shepherd)
/*
Usage
TPreferences prefs("PrefsSample_prefs"); // Preferences
if (prefs.InitCheck() != B_OK) {
prefs.SetInt64("last_used", real_time_clock());
prefs.SetInt32("use_count", 0);
}
Then we call PrintToStream() to print out the contents of the TPreferences object:
prefs.PrintToStream();
Finally, we update the preferences:
int32 count;
if (prefs.FindInt32("use_count", &count) != B_OK) {
count = 0;
}
prefs.SetInt64("last_used", real_time_clock());
prefs.SetInt32("use_count", ++count);
*/
#include <Message.h>
#include <Path.h>
#include <FindDirectory.h>
class TPreferences : public BMessage {
public:
TPreferences(directory_which directory, const char* filename);
virtual ~TPreferences();
status_t InitCheck(void);
status_t SetBool(const char *name, bool b);
status_t SetInt8(const char *name, int8 i);
status_t SetInt16(const char *name, int16 i);
status_t SetInt32(const char *name, int32 i);
status_t SetInt64(const char *name, int64 i);
status_t SetFloat(const char *name, float f);
status_t SetDouble(const char *name, double d);
status_t SetString(const char *name,
const char *string);
status_t SetPoint(const char *name, BPoint p);
status_t SetRect(const char *name, BRect r);
status_t SetMessage(const char *name,
const BMessage *message);
status_t SetFlat(const char *name,
const BFlattenable *obj);
private:
BPath fPath;
status_t fStatus;
};
inline
status_t TPreferences::InitCheck(void)
{
return fStatus;
}
#if __POWERPC__
#pragma export off
#endif
#endif // __TPREFS_H__