File: Remove puts(), add write_line(), cascade changes in interface.

This commit is contained in:
Jared Boone 2016-07-24 20:39:21 -07:00
parent c2d971d59e
commit ffa797b926
5 changed files with 26 additions and 21 deletions

View File

@ -101,13 +101,18 @@ File::Result<uint64_t> File::seek(const uint64_t new_position) {
return { static_cast<uint64_t>(old_position) };
}
File::Result<size_t> File::puts(const std::string& string) {
const auto result = f_puts(string.c_str(), &f);
if( result >= 0 ) {
return { static_cast<size_t>(result) };
} else {
return { static_cast<Error>(FR_EOF) };
Optional<File::Error> File::write_line(const std::string& s) {
const auto result_s = write(s.c_str(), s.size());
if( result_s.is_error() ) {
return { result_s.error() };
}
const auto result_crlf = write("\r\n", 2);
if( result_crlf.is_error() ) {
return { result_crlf.error() };
}
return { };
}
Optional<File::Error> File::sync() {

View File

@ -207,7 +207,7 @@ public:
return write(data.data(), N);
}
Result<size_t> puts(const std::string& string);
Optional<Error> write_line(const std::string& s);
// TODO: Return Result<>.
Optional<Error> sync();

View File

@ -23,15 +23,15 @@
#include "string_format.hpp"
File::Result<size_t> LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
Optional<File::Error> LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
std::string timestamp = to_string_timestamp(datetime);
return write(timestamp + " " + entry + "\r\n");
return write_line(timestamp + " " + entry);
}
File::Result<size_t> LogFile::write(const std::string& message) {
auto puts_result = file.puts(message);
if( puts_result.is_ok() ) {
Optional<File::Error> LogFile::write_line(const std::string& message) {
auto error = file.write_line(message);
if( !error.is_valid() ) {
file.sync();
}
return puts_result;
return error;
}

View File

@ -35,12 +35,12 @@ public:
return file.append(filename);
}
File::Result<size_t> write_entry(const rtc::RTC& datetime, const std::string& entry);
Optional<File::Error> write_entry(const rtc::RTC& datetime, const std::string& entry);
private:
File file;
File::Result<size_t> write(const std::string& message);
Optional<File::Error> write_line(const std::string& message);
};
#endif/*__LOG_FILE_H__*/

View File

@ -320,13 +320,13 @@ Optional<File::Error> RecordView::write_metadata_file(const std::string& filenam
if( create_error.is_valid() ) {
return create_error;
} else {
const auto puts_result1 = file.puts("sample_rate=" + to_string_dec_uint(sampling_rate) + "\n");
if( puts_result1.is_error() ) {
return { puts_result1.error() };
const auto error_line1 = file.write_line("sample_rate=" + to_string_dec_uint(sampling_rate));
if( error_line1.is_valid() ) {
return error_line1;
}
const auto puts_result2 = file.puts("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()) + "\n");
if( puts_result2.is_error() ) {
return { puts_result2.error() };
const auto error_line2 = file.write_line("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()));
if( error_line2.is_valid() ) {
return error_line2;
}
return { };
}