1e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk/*
2e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * Copyright 2014 The Android Open Source Project
3e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk *
4e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * Licensed under the Apache License, Version 2.0 (the "License");
5e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * you may not use this file except in compliance with the License.
6e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * You may obtain a copy of the License at
7e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk *
8e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk *      http://www.apache.org/licenses/LICENSE-2.0
9e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk *
10e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * Unless required by applicable law or agreed to in writing, software
11e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * distributed under the License is distributed on an "AS IS" BASIS,
12e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * See the License for the specific language governing permissions and
14e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk * limitations under the License.
15e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk */
16e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
17e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk#include <img_utils/FileOutput.h>
18e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
19e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk#include <utils/Log.h>
20e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
21e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunknamespace android {
22e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunknamespace img_utils {
23e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
24e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben BrunkFileOutput::FileOutput(String8 path) : mFp(NULL), mPath(path), mOpen(false) {}
25e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
26e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben BrunkFileOutput::~FileOutput() {
27e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if (mOpen) {
28e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGW("%s: Destructor called with %s still open.", __FUNCTION__, mPath.string());
29e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        close();
30e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
31e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk}
32e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
33e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunkstatus_t FileOutput::open() {
34e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if (mOpen) {
35e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGW("%s: Open called when file %s already open.", __FUNCTION__, mPath.string());
36e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        return OK;
37e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
38e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    mFp = ::fopen(mPath, "wb");
39e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if (!mFp) {
40e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGE("%s: Could not open file %s", __FUNCTION__, mPath.string());
41e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        return BAD_VALUE;
42e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
43e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    mOpen = true;
44e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    return OK;
45e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk}
46e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
47e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunkstatus_t FileOutput::write(const uint8_t* buf, size_t offset, size_t count) {
48e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if (!mOpen) {
49e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGE("%s: Could not write file %s, file not open.", __FUNCTION__, mPath.string());
50e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        return BAD_VALUE;
51e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
52e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
53e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    ::fwrite(buf + offset, sizeof(uint8_t), count, mFp);
54e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
55e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    int error = ::ferror(mFp);
56e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if (error != 0) {
57e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGE("%s: Error %d occurred while writing file %s.", __FUNCTION__, error, mPath.string());
58e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        return BAD_VALUE;
59e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
60e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    return OK;
61e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk}
62e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
63e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunkstatus_t FileOutput::close() {
64e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if(!mOpen) {
65e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGW("%s: Close called when file %s already close.", __FUNCTION__, mPath.string());
66e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        return OK;
67e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
68e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
69e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    status_t ret = OK;
70e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    if(::fclose(mFp) != 0) {
71e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ALOGE("%s: Failed to close file %s.", __FUNCTION__, mPath.string());
72e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk        ret = BAD_VALUE;
73e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    }
74e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk    mOpen = false;
75191367e5d2b301f692c4561f252ee462635613a1Andreas Gampe    return ret;
76e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk}
77e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk
78e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk} /*namespace img_utils*/
79e507721000647a7d8afe44c63ef7fd04ef8971b1Ruben Brunk} /*namespace android*/
80