1 /*
2  * Copyright 2012, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 
19 #include "bcinfo/Wrap/file_wrapper_output.h"
20 
FileWrapperOutput(const char * name)21 FileWrapperOutput::FileWrapperOutput(const char* name)
22     : _name(name) {
23   _file = fopen(name, "wb");
24   if (nullptr == _file) {
25     fprintf(stderr, "Unable to open: %s\n", name);
26     exit(1);
27   }
28 }
29 
~FileWrapperOutput()30 FileWrapperOutput::~FileWrapperOutput() {
31   fclose(_file);
32 }
33 
Write(uint8_t byte)34 bool FileWrapperOutput::Write(uint8_t byte) {
35   return EOF != fputc(byte, _file);
36 }
37 
Write(const uint8_t * buffer,size_t buffer_size)38 bool FileWrapperOutput::Write(const uint8_t* buffer, size_t buffer_size) {
39   if (!buffer) {
40     return false;
41   }
42 
43   if (buffer_size > 0) {
44     return buffer_size == fwrite(buffer, 1, buffer_size, _file);
45   } else {
46     return true;
47   }
48 }
49