• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <algorithm>
17 #include <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <fstream>
21 #include <iostream>
22 #include <iterator>
23 #include <securec.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include "directory_ex.h"
27 #include "file_ex.h"
28 using namespace std;
29 
30 const int MAX_FILE_LENGTH = 32 * 1024 * 1024;
31 
32 namespace OHOS {
LoadStringFromFile(const string & filePath,string & content)33 bool LoadStringFromFile(const string& filePath, string& content)
34 {
35     ifstream file(filePath.c_str());
36     if (!file.is_open()) {
37         return false;
38     }
39 
40     file.seekg(0, ios::end);
41     const long fileLength = file.tellg();
42     if (fileLength > MAX_FILE_LENGTH) {
43         return false;
44     }
45 
46     content.clear();
47     file.seekg(0, ios::beg);
48     copy(istreambuf_iterator<char>(file), istreambuf_iterator<char>(), back_inserter(content));
49     return true;
50 }
51 } // OHOS
52