Adding initial structure for unittest
[platform/core/multimedia/libmm-utility.git] / jpeg / 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         fseek(fp, 0, SEEK_END);
47         size = (size_t)ftell(fp);
48         rewind(fp);
49
50         if (size < 0)
51                 return ;
52         fileSize = (size_t)size;
53 }
54
55 gboolean FileInterface::ReadData()
56 {
57         if (!fp || fileSize == 0)
58                 return FALSE;
59
60         readData = (unsigned char *)calloc(1, fileSize);
61         if (fread(readData, 1, fileSize, fp) != fileSize) {
62                 return FALSE;
63         }
64         readDataSize = fileSize;
65         return TRUE;
66 }