1 /*
2 * Copyright (c) 2023-2024 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 #include <iostream>
16 #include <fstream>
17 #include <vector>
18 #include "generate_ics_file.h"
19
20 namespace OHOS {
21 namespace Global {
22 namespace I18n {
23
IcsFileWriter()24 IcsFileWriter::IcsFileWriter()
25 {
26 }
27
~IcsFileWriter()28 IcsFileWriter::~IcsFileWriter()
29 {
30 }
31
GenerateFile()32 std::string IcsFileWriter::GenerateFile()
33 {
34 std::string filePath = "/data/log/TR.ics";
35 std::ofstream fstream(filePath);
36 if (!fstream.is_open()) {
37 printf("file can't access.\r\n");
38 return filePath;
39 }
40 std::vector<std::string> list = {
41 "BEGIN:VCALENDAR\r\n",
42 "METHOD:PUBLISH\r\n",
43 "BEGIN:VEVENT\r\n",
44 "UID:1\r\n",
45 "DTSTART;VALUE=DATE:20220625\r\n",
46 "DTEND;VALUE=DATE:20220625\r\n",
47 "SUMMARY:Sacrifice Feast Holiday\r\n",
48 "RESOURCES;LANGUAGE=TR:Kurban Bayrami Tatili\r\n"
49 "END:VEVENT\r\n",
50 "BEGIN:VEVENT\r\n",
51 "UID:2\r\n",
52 "DTSTART;VALUE=DATE:20220626\r\n",
53 "DTEND;VALUE=DATE:20220626\r\n",
54 "SUMMARY:The Second Day of Sacrifice Feast\r\n",
55 "RESOURCES;LANGUAGE=TR:Kurban Bayrami 2. Günü\r\n"
56 "END:VEVENT\r\n",
57 "BEGIN:VEVENT\r\n",
58 "UID:3\r\n",
59 "DTSTART;VALUE=DATE:20220625\r\n",
60 "DTEND;VALUE=DATE:20220625\r\n",
61 "SUMMARY:Test Holiday\r\n",
62 "RESOURCES;LANGUAGE=TR:test holiday\r\n"
63 "END:VEVENT\r\n",
64 "END:VCALENDAR\r\n"
65 };
66 for (size_t i = 0; i < list.size(); i++) {
67 fstream << list[i] << std::endl;
68 }
69 fstream.close();
70 return filePath;
71 }
72
WriteFile(const std::string & content,const std::string & filePath) const73 bool IcsFileWriter::WriteFile(const std::string& content, const std::string& filePath) const
74 {
75 std::ofstream fstream(filePath);
76 if (!fstream.is_open()) {
77 printf("file can't access.\r\n");
78 return false;
79 }
80 fstream << content << std::endl;
81 fstream.close();
82 return true;
83 }
84
WriteManifest(const std::vector<std::string> & list,std::string & fileName)85 std::string IcsFileWriter::WriteManifest(const std::vector<std::string>& list, std::string& fileName)
86 {
87 std::string filePath = "/data/log/" + fileName;
88 std::ofstream fstream(filePath);
89 if (!fstream.is_open()) {
90 printf("file can't access.\r\n");
91 return filePath;
92 }
93 for (size_t i = 0; i < list.size(); i++) {
94 fstream << list[i] << std::endl;
95 }
96 fstream.close();
97 return filePath;
98 }
99
WriteBinaryFile(std::string & fileName)100 std::string IcsFileWriter::WriteBinaryFile(std::string& fileName)
101 {
102 std::string filePath = "/data/log/" + fileName;
103 std::ofstream fstream(filePath, std::ios_base::out | std::ios_base::binary);
104 if (!fstream.is_open()) {
105 printf("file can't access.\r\n");
106 return filePath;
107 }
108 if (fstream.is_open()) {
109 const char* data = "01010101000101001010";
110 const size_t len = 20;
111 fstream.write(data, len);
112 }
113 fstream.close();
114 return filePath;
115 }
116 } // namespace I18n
117 } // namespace Global
118 } // namespace OHOS