stockpules: fix protobuf-lite support

Looks like we lost more than just TextFormat when we lost full protobuf
support. Copied these two serialization functions over.
develop
Casey Link 2014-11-27 16:36:42 +01:00
parent 1c496fb255
commit cb6d4da2f2
2 changed files with 33 additions and 9 deletions

@ -1,6 +1,6 @@
package dfstockpiles; package dfstockpiles;
option optimize_for = CODE_SIZE; option optimize_for = LITE_RUNTIME;
message StockpileSettings { message StockpileSettings {
@ -156,4 +156,4 @@ message StockpileSettings {
optional int32 max_bins = 21; optional int32 max_bins = 21;
optional int32 max_wheelbarrows = 22; optional int32 max_wheelbarrows = 22;
optional bool use_links_only = 23; optional bool use_links_only = 23;
} }

@ -41,6 +41,7 @@
// protobuf // protobuf
#include "proto/stockpiles.pb.h" #include "proto/stockpiles.pb.h"
#include <google/protobuf/io/zero_copy_stream_impl.h>
// os // os
#include <sys/stat.h> #include <sys/stat.h>
@ -463,28 +464,51 @@ public:
mOut = &out; mOut = &out;
} }
/**
* Since we depend on protobuf-lite, not the full lib, we copy this function from
* protobuf message.cc
*/
bool serialize_to_ostream(ostream* output)
{
mBuffer.Clear();
write();
{
io::OstreamOutputStream zero_copy_output(output);
if (!mBuffer.SerializeToZeroCopyStream(&zero_copy_output)) return false;
}
return output->good();
}
/** /**
* Will serialize stockpile settings to a file (overwrites existing files) * Will serialize stockpile settings to a file (overwrites existing files)
* @return success/failure * @return success/failure
*/ */
bool serialize_to_file ( const std::string & file ) bool serialize_to_file ( const std::string & file )
{ {
mBuffer.Clear();
write();
std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc ); std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc );
return mBuffer.SerializeToOstream ( &output ); return serialize_to_ostream(&output);
} }
/**
* Again, copied from message.cc
*/
bool parse_from_istream(istream* input)
{
mBuffer.Clear();
io::IstreamInputStream zero_copy_input(input);
const bool res = mBuffer.ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
if( res ) read();
return res;
}
/** /**
* Read stockpile settings from file * Read stockpile settings from file
*/ */
bool unserialize_from_file ( const std::string & file ) bool unserialize_from_file ( const std::string & file )
{ {
mBuffer.Clear();
std::fstream input ( file, std::ios::in | std::ios::binary ); std::fstream input ( file, std::ios::in | std::ios::binary );
const bool res = mBuffer.ParseFromIstream ( &input ); return parse_from_istream(&input);
read();
return res;
} }
private: private: