Use stdbool instead of gboolean
[platform/core/multimedia/libmm-utility.git] / gif / unittest / FileInterface.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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 "FileInterface.h"
18
19 FileInterface::FileInterface(const char *path)
20 {
21         fp = NULL;
22         fileSize = 0;
23
24         FileInterface::Open(path);
25 }
26
27 FileInterface::~FileInterface(void)
28 {
29         if (fp)
30                 fclose(fp);
31         fp = NULL;
32
33         if (readData)
34                 free(readData);
35         readData = NULL;
36 }
37
38 void FileInterface::Open(const char *path)
39 {
40         fp = fopen(path, "r");
41         if (!fp)
42                 return ;
43
44         long size = 0;
45
46         if (fseek(fp, 0, SEEK_END) < 0)
47                 return ;
48
49         size = (size_t)ftell(fp);
50         rewind(fp);
51
52         if (size < 0)
53                 return ;
54         fileSize = (size_t)size;
55 }
56
57 bool FileInterface::ReadData()
58 {
59         if (!fp || fileSize == 0)
60                 return false;
61
62         readData = (unsigned char *)calloc(1, fileSize);
63         if (fread(readData, 1, fileSize, fp) != fileSize) {
64                 return false;
65         }
66         readDataSize = fileSize;
67         return true;
68 }