[3.0] Fix build error
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / portable / file-closer.h
1 #ifndef _DALI_INTERNAL_PLATFORM_FILECLOSER_H__
2 #define _DALI_INTERNAL_PLATFORM_FILECLOSER_H__
3 /*
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21
22 // EXTERNAL INCLUDES
23 #include <cstdio>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Platform
30 {
31
32 /**
33  * Opens files and closes them later even if an exception is thrown.
34  */
35 class FileCloser
36 {
37 public:
38
39   /**
40    * @brief Construct a FileCloser guarding a new FILE* for accessing the path passed in.
41    */
42   FileCloser( const char * const filename, const char * const mode ) :
43     mFile(fopen(filename, mode))
44   {
45     DALI_ASSERT_DEBUG( filename != 0 && "Cant open a null filename." );
46     DALI_ASSERT_DEBUG( mode != 0 && "Null mode is undefined behaviour in spec." );
47
48     if( mFile == 0 )
49     {
50       DALI_LOG_WARNING( "File open failed for: \"%s\" in mode: \"%s\".\n", filename, mode );
51     }
52   }
53
54   /**
55    * @brief Construct a FileCloser guarding a FILE* for reading out of the memory buffer passed in.
56    */
57   FileCloser( void * const buffer, const size_t bufferSize, const char * const mode ) :
58     mFile( fmemopen( buffer, bufferSize, mode ) )
59   {
60     DALI_ASSERT_DEBUG( buffer != 0 && "Cant open file on null buffer." );
61     DALI_ASSERT_DEBUG( bufferSize > 0 && "Pointless to open file on empty buffer." );
62     DALI_ASSERT_DEBUG( mode != 0 && "Null mode is undefined behaviour in spec." );
63
64     if( mFile == 0 )
65     {
66       DALI_LOG_WARNING( "File open failed for memory buffer at location: \"%p\", of size: \"%u\", in mode: \"%s\".\n", static_cast<void*>(buffer), static_cast<unsigned>(bufferSize), mode );
67     }
68   }
69
70    /**
71     * @brief Destroy the FileCloser and clean up its FILE*.
72     */
73   ~FileCloser()
74   {
75     if( mFile != 0 )
76     {
77       const int closeFailed = fclose( mFile );
78
79       if ( closeFailed )
80       {
81         DALI_LOG_WARNING( "File close failed for FILE: \"%p\".\n", static_cast<void*>(mFile) );
82       }
83       mFile = 0;
84     }
85   }
86
87   /**
88    * @return The FILE* guarded by this object.
89    */
90   FILE* GetFile()
91   {
92     return mFile;
93   }
94
95 private:
96
97   // Undefined
98   FileCloser( const FileCloser& fileCloser );
99
100   // Undefined
101   FileCloser& operator=( const FileCloser& fileCloser );
102
103 private:
104   FILE* mFile;
105 };
106
107 } /* namespace Platform */
108 } /* namespace Internal */
109 } /* namespace Dali */
110
111 #endif /* _DALI_INTERNAL_PLATFORM_FILECLOSER_H__ */