Shinichiro Hamaji | 2f6078b | 2015-07-31 14:59:46 +0900 | [diff] [blame] | 1 | // 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 | |
| 21 | void DumpInt(FILE* fp, int v) { |
| 22 | size_t r = fwrite(&v, sizeof(v), 1, fp); |
| 23 | CHECK(r == 1); |
| 24 | } |
| 25 | |
| 26 | void 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 | |
| 32 | int LoadInt(FILE* fp) { |
| 33 | int v; |
| 34 | size_t r = fread(&v, sizeof(v), 1, fp); |
Shinichiro Hamaji | ecababb | 2015-08-29 11:38:11 +0900 | [diff] [blame] | 35 | if (r != 1) |
| 36 | return -1; |
Shinichiro Hamaji | 2f6078b | 2015-07-31 14:59:46 +0900 | [diff] [blame] | 37 | return v; |
| 38 | } |
| 39 | |
Shinichiro Hamaji | ecababb | 2015-08-29 11:38:11 +0900 | [diff] [blame] | 40 | bool LoadString(FILE* fp, string* s) { |
| 41 | int len = LoadInt(fp); |
| 42 | if (len < 0) |
| 43 | return false; |
| 44 | s->resize(len); |
Shinichiro Hamaji | 2f6078b | 2015-07-31 14:59:46 +0900 | [diff] [blame] | 45 | size_t r = fread(&(*s)[0], 1, s->size(), fp); |
Shinichiro Hamaji | ecababb | 2015-08-29 11:38:11 +0900 | [diff] [blame] | 46 | if (r != s->size()) |
| 47 | return false; |
| 48 | return true; |
Shinichiro Hamaji | 2f6078b | 2015-07-31 14:59:46 +0900 | [diff] [blame] | 49 | } |