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