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 
WriteVersionFile(const std::string & verison,std::string fileName)73 std::string IcsFileWriter::WriteVersionFile(const std::string& verison, std::string fileName)
74 {
75     std::string filePath = "/data/log/" + fileName + ".txt";
76     std::ofstream fstream(filePath);
77     if (!fstream.is_open()) {
78         printf("file can't access.\r\n");
79         return filePath;
80     }
81     std::string content = "version=" + verison;
82     fstream << content << std::endl;
83     fstream.close();
84     return filePath;
85 }
86 
WriteManifest(const std::vector<std::string> & list,std::string & fileName)87 std::string IcsFileWriter::WriteManifest(const std::vector<std::string>& list, std::string& fileName)
88 {
89     std::string filePath = "/data/log/" + fileName;
90     std::ofstream fstream(filePath);
91     if (!fstream.is_open()) {
92         printf("file can't access.\r\n");
93         return filePath;
94     }
95     for (size_t i = 0; i < list.size(); i++) {
96         fstream << list[i] << std::endl;
97     }
98     fstream.close();
99     return filePath;
100 }
101 
WriteBinaryFile(std::string & fileName)102 std::string IcsFileWriter::WriteBinaryFile(std::string& fileName)
103 {
104     std::string filePath = "/data/log/" + fileName;
105     std::ofstream fstream(filePath, std::ios_base::out | std::ios_base::binary);
106     if (!fstream.is_open()) {
107         printf("file can't access.\r\n");
108         return filePath;
109     }
110     if (fstream.is_open()) {
111         const char* data = "01010101000101001010";
112         const size_t len = 20;
113         fstream.write(data, len);
114     }
115     fstream.close();
116     return filePath;
117 }
118 } // namespace I18n
119 } // namespace Global
120 } // namespace OHOS