input/TextInputStream: return char*
Revert to the old API before commit e9e55b08
, removing unnecessary
bloat.
This commit is contained in:
parent
08fee9a284
commit
69ae879c58
|
@ -27,9 +27,10 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
bool TextInputStream::ReadLine(std::string &line)
|
char *
|
||||||
|
TextInputStream::ReadLine()
|
||||||
{
|
{
|
||||||
const char *src, *p;
|
char *src, *p;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
size_t nbytes;
|
size_t nbytes;
|
||||||
|
@ -46,33 +47,33 @@ bool TextInputStream::ReadLine(std::string &line)
|
||||||
buffer.Append(nbytes);
|
buffer.Append(nbytes);
|
||||||
else if (error.IsDefined()) {
|
else if (error.IsDefined()) {
|
||||||
LogError(error);
|
LogError(error);
|
||||||
return false;
|
return nullptr;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
nbytes = 0;
|
nbytes = 0;
|
||||||
|
|
||||||
auto src_p = buffer.Read();
|
auto src_p = buffer.Read();
|
||||||
if (src_p.IsEmpty())
|
if (src_p.IsEmpty())
|
||||||
return false;
|
return nullptr;
|
||||||
|
|
||||||
src = src_p.data;
|
src = src_p.data;
|
||||||
|
|
||||||
p = reinterpret_cast<const char*>(memchr(src, '\n', src_p.size));
|
p = reinterpret_cast<char*>(memchr(src, '\n', src_p.size));
|
||||||
if (p == nullptr && nbytes == 0) {
|
if (p == nullptr && nbytes == 0) {
|
||||||
/* end of file (or line too long): terminate
|
/* end of file (or line too long): terminate
|
||||||
the current line */
|
the current line */
|
||||||
dest = buffer.Write();
|
dest = buffer.Write();
|
||||||
assert(!dest.IsEmpty());
|
assert(!dest.IsEmpty());
|
||||||
dest[0] = '\n';
|
dest[0] = 0;
|
||||||
buffer.Append(1);
|
buffer.Clear();
|
||||||
|
return src;
|
||||||
}
|
}
|
||||||
} while (p == nullptr);
|
} while (p == nullptr);
|
||||||
|
|
||||||
size_t length = p - src + 1;
|
buffer.Consume(p - src + 1);
|
||||||
|
|
||||||
while (p > src && IsWhitespaceOrNull(p[-1]))
|
while (p > src && IsWhitespaceOrNull(p[-1]))
|
||||||
--p;
|
--p;
|
||||||
|
*p = 0;
|
||||||
line = std::string(src, p - src);
|
return src;
|
||||||
buffer.Consume(length);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,6 @@
|
||||||
|
|
||||||
#include "util/StaticFifoBuffer.hxx"
|
#include "util/StaticFifoBuffer.hxx"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class InputStream;
|
class InputStream;
|
||||||
|
|
||||||
class TextInputStream {
|
class TextInputStream {
|
||||||
|
@ -46,11 +44,9 @@ public:
|
||||||
/**
|
/**
|
||||||
* Reads the next line from the stream with newline character stripped.
|
* Reads the next line from the stream with newline character stripped.
|
||||||
*
|
*
|
||||||
* @param line a string to put result to
|
* @return a pointer to the line, or nullptr on end-of-file or error
|
||||||
* @return true if line is read successfully, false on end of file
|
|
||||||
* or error
|
|
||||||
*/
|
*/
|
||||||
bool ReadLine(std::string &line);
|
char *ReadLine();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -52,9 +52,9 @@ CuePlaylist::NextSong()
|
||||||
if (song != nullptr)
|
if (song != nullptr)
|
||||||
return song;
|
return song;
|
||||||
|
|
||||||
std::string line;
|
const char *line;
|
||||||
while (tis.ReadLine(line)) {
|
while ((line = tis.ReadLine()) != nullptr) {
|
||||||
parser.Feed(line.c_str());
|
parser.Feed(line);
|
||||||
song = parser.Get();
|
song = parser.Get();
|
||||||
if (song != nullptr)
|
if (song != nullptr)
|
||||||
return song;
|
return song;
|
||||||
|
|
|
@ -39,9 +39,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckFirstLine() {
|
bool CheckFirstLine() {
|
||||||
std::string line;
|
const char *line = tis.ReadLine();
|
||||||
return tis.ReadLine(line) &&
|
return line != nullptr && strcmp(line, "#EXTM3U") == 0;
|
||||||
strcmp(line.c_str(), "#EXTM3U") == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual DetachedSong *NextSong() override;
|
virtual DetachedSong *NextSong() override;
|
||||||
|
@ -105,15 +104,13 @@ DetachedSong *
|
||||||
ExtM3uPlaylist::NextSong()
|
ExtM3uPlaylist::NextSong()
|
||||||
{
|
{
|
||||||
Tag tag;
|
Tag tag;
|
||||||
std::string line;
|
|
||||||
const char *line_s;
|
const char *line_s;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!tis.ReadLine(line))
|
line_s = tis.ReadLine();
|
||||||
|
if (line_s == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
line_s = line.c_str();
|
|
||||||
|
|
||||||
if (StringStartsWith(line_s, "#EXTINF:")) {
|
if (StringStartsWith(line_s, "#EXTINF:")) {
|
||||||
tag = extm3u_parse_tag(line_s + 8);
|
tag = extm3u_parse_tag(line_s + 8);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -45,14 +45,13 @@ m3u_open_stream(InputStream &is)
|
||||||
DetachedSong *
|
DetachedSong *
|
||||||
M3uPlaylist::NextSong()
|
M3uPlaylist::NextSong()
|
||||||
{
|
{
|
||||||
std::string line;
|
|
||||||
const char *line_s;
|
const char *line_s;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (!tis.ReadLine(line))
|
line_s = tis.ReadLine();
|
||||||
|
if (line_s == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
line_s = line.c_str();
|
|
||||||
line_s = strchug_fast(line_s);
|
line_s = strchug_fast(line_s);
|
||||||
} while (line_s[0] == '#' || *line_s == 0);
|
} while (line_s[0] == '#' || *line_s == 0);
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,9 @@
|
||||||
static void
|
static void
|
||||||
dump_text_file(TextInputStream &is)
|
dump_text_file(TextInputStream &is)
|
||||||
{
|
{
|
||||||
std::string line;
|
const char *line;
|
||||||
while (is.ReadLine(line))
|
while ((line = is.ReadLine()) != nullptr)
|
||||||
printf("'%s'\n", line.c_str());
|
printf("'%s'\n", line);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
Loading…
Reference in New Issue