Agrarsense
CSVFile.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "CSVFile.h"
7
10
11#include "Misc/Paths.h"
12#include "HAL/PlatformFileManager.h"
13
14UCSVFile* UCSVFile::CreateCSVFile(const FString& FileNameWithoutExtension, const FCSVFileSettings& Settings)
15{
16 UCSVFile* CSVFile = NewObject<UCSVFile>(UCSVFile::StaticClass());
17 CSVFile->AddToRoot();
18 if (CSVFile)
19 {
20 CSVFile->Create(FileNameWithoutExtension, Settings);
21 }
22 return CSVFile;
23}
24
25void UCSVFile::Create(const FString& FileNameWithoutExtension, const FCSVFileSettings& Settings)
26{
27 CurrentSettings = Settings;
28
29 FString BasePath = Settings.CustomPath.IsEmpty() ? UAgrarsensePaths::GetDataFolder() + "/CSV/" : Settings.CustomPath;
30 IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
31 if (!PlatformFile.DirectoryExists(*BasePath))
32 {
33 PlatformFile.CreateDirectoryTree(*BasePath);
34 }
35
36 FString FullPath = FPaths::Combine(*BasePath, FileNameWithoutExtension + TEXT(".csv"));
37 UE_LOG(LogTemp, Warning, TEXT("FullPath: %s"), *FullPath);
38
39 if (Settings.CreateUnique)
40 {
41 int32 Counter = 0;
42 while (FPaths::FileExists(FullPath))
43 {
44 FullPath = FPaths::Combine(*BasePath, FileNameWithoutExtension + FString::Printf(TEXT("_%d.csv"), Counter++));
45 }
46 }
47
48 FilePath = FullPath;
49
50 OpenStream();
51}
52
54{
55 if (CSVStream.is_open())
56 {
57 CSVStream.close();
58 }
59
60 std::ios_base::openmode Mode = std::ios::out;
62 {
63 Mode |= std::ios::app;
64 }
65
66 CSVStream.open(TCHAR_TO_UTF8(*FilePath), Mode);
67}
68
69void UCSVFile::WriteRow(const TArray<FString>& Cells)
70{
71 if (!CSVStream.is_open()) OpenStream();
72 if (!CSVStream.is_open()) return;
73
74 // Determine delimiter
75 FString Delimiter = TEXT(",");
77 {
78 Delimiter = TEXT(";");
79 }
81 {
82 Delimiter = TEXT("\t");
83 }
84
85 // Join values using chosen delimiter
86 FString Line = FString::Join(Cells, *Delimiter);
87
89 {
90 WriteRaw(Line); // Write the line immediately
91 }
93 {
94 // Queue the line for later
95 PendingRows.Add(Line);
96 }
97}
98
99void UCSVFile::WriteRaw(const FString& Line)
100{
101 if (!CSVStream.is_open()) OpenStream();
102 if (!CSVStream.is_open()) return;
103
104 CSVStream << TCHAR_TO_UTF8(*Line) << std::endl;
105}
106
108{
109 if (CSVStream.is_open())
110 {
111 // If we have queued rows, write them out
113 {
114 for (const FString& Row : PendingRows)
115 {
116 WriteRaw(Row);
117 }
118 PendingRows.Empty();
119 }
120
121 CSVStream.close();
122 }
123}
124
126{
127 Close();
128 ConditionalBeginDestroy();
129}
130
132{
133 if (!FilePath.IsEmpty())
134 {
136 }
137}
static FString GetDataFolder()
static UCSVFile * CreateCSVFile(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
Definition: CSVFile.cpp:14
void WriteRaw(const FString &Line)
Definition: CSVFile.cpp:99
std::ofstream CSVStream
Definition: CSVFile.h:91
void OpenFilePath()
Definition: CSVFile.cpp:131
void Create(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
Definition: CSVFile.cpp:25
FString FilePath
Definition: CSVFile.h:87
TArray< FString > PendingRows
Definition: CSVFile.h:85
void Close()
Definition: CSVFile.cpp:107
void OpenStream()
Definition: CSVFile.cpp:53
FCSVFileSettings CurrentSettings
Definition: CSVFile.h:89
void WriteRow(const TArray< FString > &Cells)
Definition: CSVFile.cpp:69
void Destroy()
Definition: CSVFile.cpp:125
static void OpenFileExplorer(FString Path)
bool CreateUnique
Definition: CSVFile.h:42
ECSVDelimiter Delimiter
Definition: CSVFile.h:36
FString CustomPath
Definition: CSVFile.h:48
FCSVFileWriteOptions FileWriteOption
Definition: CSVFile.h:45