tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / DEPRACATED / Memory / Manager.cpp
1 /*
2  * Copyright (c) 2011 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 #include <fstream>
17 #include <cassert>
18 #include <pcrecpp.h>
19 #include <commons/Exception.h>
20 #include "Manager.h"
21
22 namespace {
23 const char* MEMINFO_FILE = "/proc/meminfo";
24 const char* MEMORY_TOTAL_PATTERN = "^MemTotal:\\s+(\\d+)\\s*kB$";
25 const char* MEMORY_FREE_PATTERN = "^MemFree:\\s+(\\d+)\\s*kB$";
26 }
27
28 namespace WrtPlugins {
29 namespace Platform {
30 namespace Memory {
31 Manager& Manager::getInstance()
32 {
33     static Manager instance;
34     return instance;
35 }
36
37 unsigned long Manager::getTotal() const
38 {
39     return getMeminfoValue(MEMORY_TOTAL_PATTERN);
40 }
41
42 unsigned long Manager::getFree() const
43 {
44     return getMeminfoValue(MEMORY_FREE_PATTERN);
45 }
46
47 Manager::Manager()
48 {
49 }
50
51 unsigned long Manager::getMeminfoValue(const char* pattern) const
52 {
53     assert(pattern && "Pattern not set.");
54
55     std::ifstream file(MEMINFO_FILE);
56     if (!file) {
57         ThrowMsg(Commons::PlatformException, "Memory info file not found.");
58     }
59
60     unsigned long result = 0;
61     bool matched = false;
62     std::string line;
63     while ((std::getline(file, line).rdstate() &
64             (std::ifstream::failbit | std::ifstream::eofbit)) == 0) {
65         if (pcrecpp::RE(pattern).FullMatch(line, &result)) {
66             matched = true;
67             break;
68         }
69     }
70     file.close();
71
72     if (!matched) {
73         ThrowMsg(Commons::UnsupportedException, "Total memory data not found.");
74     }
75
76     return result;
77 }
78 } // Memory
79 } // Platform
80 } // WrtPlugins