blob: 9ae1c5eb29278868d35b0096ec6b6aa1d3e8a63c [file] [log] [blame]
Shinichiro Hamaji2f6078b2015-07-31 14:59:46 +09001// Copyright 2015 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17#include "io.h"
18
19#include "log.h"
20
21void DumpInt(FILE* fp, int v) {
22 size_t r = fwrite(&v, sizeof(v), 1, fp);
23 CHECK(r == 1);
24}
25
26void DumpString(FILE* fp, StringPiece s) {
27 DumpInt(fp, s.size());
28 size_t r = fwrite(s.data(), 1, s.size(), fp);
29 CHECK(r == s.size());
30}
31
32int LoadInt(FILE* fp) {
33 int v;
34 size_t r = fread(&v, sizeof(v), 1, fp);
Shinichiro Hamajiecababb2015-08-29 11:38:11 +090035 if (r != 1)
36 return -1;
Shinichiro Hamaji2f6078b2015-07-31 14:59:46 +090037 return v;
38}
39
Shinichiro Hamajiecababb2015-08-29 11:38:11 +090040bool LoadString(FILE* fp, string* s) {
41 int len = LoadInt(fp);
42 if (len < 0)
43 return false;
44 s->resize(len);
Shinichiro Hamaji2f6078b2015-07-31 14:59:46 +090045 size_t r = fread(&(*s)[0], 1, s->size(), fp);
Shinichiro Hamajiecababb2015-08-29 11:38:11 +090046 if (r != s->size())
47 return false;
48 return true;
Shinichiro Hamaji2f6078b2015-07-31 14:59:46 +090049}