@@ -1108,28 +1108,37 @@ const char* UpgradeV1LayerType(const V1LayerParameter_LayerType type) {
1108
1108
1109
1109
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
1110
1110
1111
+ bool ReadProtoFromBinary (ZeroCopyInputStream* input, Message *proto) {
1112
+ CodedInputStream coded_input (input);
1113
+ coded_input.SetTotalBytesLimit (kProtoReadBytesLimit , 536870912 );
1114
+
1115
+ return proto->ParseFromCodedStream (&coded_input);
1116
+ }
1117
+
1111
1118
bool ReadProtoFromTextFile (const char * filename, Message* proto) {
1112
1119
std::ifstream fs (filename, std::ifstream::in);
1113
1120
CHECK (fs.is_open ()) << " Can't open \" " << filename << " \" " ;
1114
1121
IstreamInputStream input (&fs);
1115
- bool success = google::protobuf::TextFormat::Parse (&input, proto);
1116
- fs.close ();
1117
- return success;
1122
+ return google::protobuf::TextFormat::Parse (&input, proto);
1118
1123
}
1119
1124
1120
1125
bool ReadProtoFromBinaryFile (const char * filename, Message* proto) {
1121
1126
std::ifstream fs (filename, std::ifstream::in | std::ifstream::binary);
1122
1127
CHECK (fs.is_open ()) << " Can't open \" " << filename << " \" " ;
1123
- ZeroCopyInputStream* raw_input = new IstreamInputStream (&fs);
1124
- CodedInputStream* coded_input = new CodedInputStream (raw_input);
1125
- coded_input->SetTotalBytesLimit (kProtoReadBytesLimit , 536870912 );
1128
+ IstreamInputStream raw_input (&fs);
1129
+
1130
+ return ReadProtoFromBinary (&raw_input, proto);
1131
+ }
1132
+
1133
+ bool ReadProtoFromTextBuffer (const char * data, size_t len, Message* proto) {
1134
+ ArrayInputStream input (data, len);
1135
+ return google::protobuf::TextFormat::Parse (&input, proto);
1136
+ }
1126
1137
1127
- bool success = proto->ParseFromCodedStream (coded_input);
1128
1138
1129
- delete coded_input;
1130
- delete raw_input;
1131
- fs.close ();
1132
- return success;
1139
+ bool ReadProtoFromBinaryBuffer (const char * data, size_t len, Message* proto) {
1140
+ ArrayInputStream raw_input (data, len);
1141
+ return ReadProtoFromBinary (&raw_input, proto);
1133
1142
}
1134
1143
1135
1144
void ReadNetParamsFromTextFileOrDie (const char * param_file,
@@ -1139,13 +1148,27 @@ void ReadNetParamsFromTextFileOrDie(const char* param_file,
1139
1148
UpgradeNetAsNeeded (param_file, param);
1140
1149
}
1141
1150
1151
+ void ReadNetParamsFromTextBufferOrDie (const char * data, size_t len,
1152
+ NetParameter* param) {
1153
+ CHECK (ReadProtoFromTextBuffer (data, len, param))
1154
+ << " Failed to parse NetParameter buffer" ;
1155
+ UpgradeNetAsNeeded (" memory buffer" , param);
1156
+ }
1157
+
1142
1158
void ReadNetParamsFromBinaryFileOrDie (const char * param_file,
1143
1159
NetParameter* param) {
1144
1160
CHECK (ReadProtoFromBinaryFile (param_file, param))
1145
1161
<< " Failed to parse NetParameter file: " << param_file;
1146
1162
UpgradeNetAsNeeded (param_file, param);
1147
1163
}
1148
1164
1165
+ void ReadNetParamsFromBinaryBufferOrDie (const char * data, size_t len,
1166
+ NetParameter* param) {
1167
+ CHECK (ReadProtoFromBinaryBuffer (data, len, param))
1168
+ << " Failed to parse NetParameter buffer" ;
1169
+ UpgradeNetAsNeeded (" memory buffer" , param);
1170
+ }
1171
+
1149
1172
}
1150
1173
}
1151
1174
#endif
0 commit comments