1c397aae3SStephan Aßmus /*
2c397aae3SStephan Aßmus * Copyright 2008, Haiku.
3c397aae3SStephan Aßmus * Distributed under the terms of the MIT License.
4c397aae3SStephan Aßmus *
5c397aae3SStephan Aßmus * Authors:
6c397aae3SStephan Aßmus * Michael Pfeiffer <laplace@users.sourceforge.net>
7c397aae3SStephan Aßmus * Fredrik Modéen <fredrik@modeen.se>
8c397aae3SStephan Aßmus */
9c397aae3SStephan Aßmus #include "FileReadWrite.h"
10c397aae3SStephan Aßmus
11c397aae3SStephan Aßmus #include <UTF8.h>
12c397aae3SStephan Aßmus #include <Path.h>
13c397aae3SStephan Aßmus
1426e9cdb7SStephan Aßmus
FileReadWrite(BFile * file,int32 sourceEncoding)15c397aae3SStephan Aßmus FileReadWrite::FileReadWrite(BFile* file, int32 sourceEncoding)
16c397aae3SStephan Aßmus : fFile(file),
1726e9cdb7SStephan Aßmus fSourceEncoding(sourceEncoding),
1826e9cdb7SStephan Aßmus fPositionInBuffer(0),
1926e9cdb7SStephan Aßmus fAmtRead(0)
20c397aae3SStephan Aßmus {}
21c397aae3SStephan Aßmus
22c397aae3SStephan Aßmus
23c397aae3SStephan Aßmus void
SetEncoding(int32 sourceEncoding)24c397aae3SStephan Aßmus FileReadWrite::SetEncoding(int32 sourceEncoding)
25c397aae3SStephan Aßmus {
26c397aae3SStephan Aßmus fSourceEncoding = sourceEncoding;
27c397aae3SStephan Aßmus }
28c397aae3SStephan Aßmus
29c397aae3SStephan Aßmus
30c397aae3SStephan Aßmus uint32
GetEncoding() const3126e9cdb7SStephan Aßmus FileReadWrite::GetEncoding() const
32c397aae3SStephan Aßmus {
33c397aae3SStephan Aßmus return fSourceEncoding;
34c397aae3SStephan Aßmus }
35c397aae3SStephan Aßmus
36c397aae3SStephan Aßmus
37c397aae3SStephan Aßmus status_t
Write(const BString & contents) const38c397aae3SStephan Aßmus FileReadWrite::Write(const BString& contents) const
39c397aae3SStephan Aßmus {
4026e9cdb7SStephan Aßmus ssize_t written = fFile->Write(contents.String(), contents.Length());
4126e9cdb7SStephan Aßmus if (written != contents.Length())
4226e9cdb7SStephan Aßmus return written < 0 ? (status_t)written : B_IO_ERROR;
4326e9cdb7SStephan Aßmus
44c397aae3SStephan Aßmus return B_OK;
45c397aae3SStephan Aßmus }
46c397aae3SStephan Aßmus
47c397aae3SStephan Aßmus
48c397aae3SStephan Aßmus bool
Next(BString & string)49c397aae3SStephan Aßmus FileReadWrite::Next(BString& string)
50c397aae3SStephan Aßmus {
5126e9cdb7SStephan Aßmus // Fill up the buffer with the first chunk of data
52c397aae3SStephan Aßmus if (fPositionInBuffer == 0)
53c397aae3SStephan Aßmus fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer));
54c397aae3SStephan Aßmus while (fAmtRead > 0) {
55c397aae3SStephan Aßmus while (fPositionInBuffer < fAmtRead) {
56c397aae3SStephan Aßmus // Return true if we hit a newline or the end of the file
57*c8ccdf52SStephan Aßmus // TODO: If the source file is expected to have different encoding,
58*c8ccdf52SStephan Aßmus // don't we need to check for the line endings in that encoding?!
59c397aae3SStephan Aßmus if (fBuffer[fPositionInBuffer] == '\n') {
60c397aae3SStephan Aßmus fPositionInBuffer++;
61*c8ccdf52SStephan Aßmus if (fSourceEncoding != -1) {
62c397aae3SStephan Aßmus int32 state = 0;
63c397aae3SStephan Aßmus int32 bufferLen = string.Length();
64c397aae3SStephan Aßmus int32 destBufferLen = bufferLen;
65c397aae3SStephan Aßmus char destination[destBufferLen];
6626e9cdb7SStephan Aßmus convert_to_utf8(fSourceEncoding, string.String(),
6726e9cdb7SStephan Aßmus &bufferLen, destination, &destBufferLen, &state);
68c397aae3SStephan Aßmus string = destination;
69*c8ccdf52SStephan Aßmus }
70c397aae3SStephan Aßmus return true;
71c397aae3SStephan Aßmus }
72*c8ccdf52SStephan Aßmus // TODO: Adding one char at a time is very inefficient!
73c397aae3SStephan Aßmus string += fBuffer[fPositionInBuffer];
74c397aae3SStephan Aßmus fPositionInBuffer++;
75c397aae3SStephan Aßmus }
76c397aae3SStephan Aßmus
77c397aae3SStephan Aßmus // Once the buffer runs out, grab some more and start again
78c397aae3SStephan Aßmus fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer));
79c397aae3SStephan Aßmus fPositionInBuffer = 0;
80c397aae3SStephan Aßmus }
81c397aae3SStephan Aßmus return false;
82c397aae3SStephan Aßmus }
83