Use DarkWyrm's TextFile from Paladin

This commit is contained in:
Jaidyn Ann 2022-06-04 11:33:30 -05:00
parent c91b665253
commit d249b25df1
4 changed files with 153 additions and 14 deletions

View File

@ -37,7 +37,8 @@ SRCS = src/App.cpp \
src/MainWindow.cpp \ src/MainWindow.cpp \
src/MediaPlayer.cpp \ src/MediaPlayer.cpp \
src/ReplicantView.cpp \ src/ReplicantView.cpp \
src/Song.cpp src/Song.cpp \
src/TextFile.cpp
# Specify the resource definition files to use. Full or relative paths can be # Specify the resource definition files to use. Full or relative paths can be
# used. # used.

View File

@ -5,13 +5,13 @@
#include "Song.h" #include "Song.h"
#include <iostream>
#include <Directory.h> #include <Directory.h>
#include <File.h> #include <File.h>
#include <String.h> #include <String.h>
#include <TranslationUtils.h> #include <TranslationUtils.h>
#include "TextFile.h"
Song::Song() Song::Song()
{ {
@ -35,18 +35,17 @@ Song::InitCheck()
status_t status_t
Song::Lyrics(BString* buffer) Song::Lyrics(BString* buffer)
{ {
BFile file(_LyricsPath().Path(), B_READ_ONLY); TextFile file(_LyricsPath().Path(), B_READ_ONLY);
off_t size = 0; if (file.InitCheck() == B_OK) {
char* buf = NULL; const char* line = file.ReadLine();
if (file.InitCheck() == B_OK && file.GetSize(&size) == B_OK) { while (line != NULL) {
buf = (char*)malloc((size_t)size); *buffer << line << '\n';
file.Read(buf, size); line = file.ReadLine();
*buffer << buf; }
free(buf); }
} else
*buffer = "No lyrics available.\n"; return file.InitCheck();
return B_OK;
} }

109
src/TextFile.cpp Normal file
View File

@ -0,0 +1,109 @@
/*
TextFile.cpp: Buffered line-based access to text files
Written by DarkWyrm, Copyright 2007
Released under the MIT license.
*/
#include "TextFile.h"
#include <stdio.h>
#include <OS.h>
#include <string.h>
TextFile::TextFile(void)
: BFile()
{
InitObject();
}
TextFile::TextFile(const char *path, const uint32 &openmode)
: BFile(path,openmode)
{
InitObject();
}
TextFile::TextFile(const entry_ref &ref, const uint32 &openmode)
: BFile(&ref,openmode)
{
InitObject();
}
TextFile::~TextFile(void)
{
delete [] fBuffer;
delete [] fReadBuffer;
}
void
TextFile::InitObject(void)
{
fReadBuffer = new char[4096];
fReadBufferSize = 4096;
if (InitCheck() == B_OK) {
GetSize(&fBufferSize);
fBuffer = new char[fBufferSize+1];
Read(fBuffer,fBufferSize);
Seek(0,SEEK_SET);
fBuffer[fBufferSize]=0;
} else {
fBuffer = NULL;
fBufferSize = 0;
}
}
const char *
TextFile::ReadLine(void)
{
off_t pos = Position();
off_t size = -1;
if (GetSize(&size) != B_OK || pos >= size)
return NULL;
char *c = fBuffer;
c += sizeof(char) * pos;
char *eol = strchr(c,'\n');
if (!eol) {
// This means that there are no more linefeeds before the the
// end of the file
eol = strchr(c,'\0');
if (!eol)
return NULL;
}
int32 length = eol-c;
if (length > fReadBufferSize) {
fReadBufferSize = length;
delete fReadBuffer;
fReadBuffer = new char[fReadBufferSize];
}
strncpy(fReadBuffer,c,length);
fReadBuffer[length] = 0;
Seek(length + 1, SEEK_CUR);
// do we need to add a \0?
return fReadBuffer;
}
ssize_t
TextFile::WriteString(const char *string)
{
if (!string)
return B_ERROR;
size_t size = strlen(string);
return Write(string, size);
}

30
src/TextFile.h Normal file
View File

@ -0,0 +1,30 @@
/*
TextFile.h: Buffered line-based access to text files
Written by DarkWyrm, Copyright 2007
Released under the MIT license.
*/
#ifndef TEXTFILE_H
#define TEXTFILE_H
#include <File.h>
class TextFile : public BFile
{
public:
TextFile(void);
TextFile(const char *path, const uint32 &openmode);
TextFile(const entry_ref &ref, const uint32 &openmode);
~TextFile(void);
const char * ReadLine(void);
ssize_t WriteString(const char *string);
private:
void InitObject(void);
char *fBuffer;
off_t fBufferSize;
char *fReadBuffer;
int32 fReadBufferSize;
};
#endif