Agrarsense
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
UCSVFile Class Reference

#include <CSVFile.h>

Inheritance diagram for UCSVFile:
Inheritance graph
[legend]
Collaboration diagram for UCSVFile:
Collaboration graph
[legend]

Public Member Functions

void WriteRow (const TArray< FString > &Cells)
 
void OpenFilePath ()
 
void Close ()
 
void Destroy ()
 

Static Public Member Functions

static UCSVFileCreateCSVFile (const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
 

Private Member Functions

void Create (const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
 
void OpenStream ()
 
void WriteRaw (const FString &Line)
 

Private Attributes

TArray< FString > PendingRows
 
FString FilePath
 
FCSVFileSettings CurrentSettings
 
std::ofstream CSVStream
 

Detailed Description

CSV file writer utility Usage example:

FCSVFileSettings Settings; UCSVFile* CSV = UCSVFile::CreateCSVFile("MyCoolCSVFile", Settings); CSV->WriteRow({ TEXT("Time"), TEXT("X"), TEXT("Y"), TEXT("Z") }); CSV->WriteRow({ TEXT("12.3"), TEXT("100"), TEXT("200"), TEXT("300") }); CSV->Destroy();

Definition at line 62 of file CSVFile.h.

Member Function Documentation

◆ Close()

void UCSVFile::Close ( )

Definition at line 107 of file CSVFile.cpp.

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}
void WriteRaw(const FString &Line)
Definition: CSVFile.cpp:99
std::ofstream CSVStream
Definition: CSVFile.h:91
TArray< FString > PendingRows
Definition: CSVFile.h:85
FCSVFileSettings CurrentSettings
Definition: CSVFile.h:89
FCSVFileWriteOptions FileWriteOption
Definition: CSVFile.h:45

References CSVStream, CurrentSettings, FCSVFileSettings::FileWriteOption, PendingRows, Queue, and WriteRaw().

Referenced by Destroy().

◆ Create()

void UCSVFile::Create ( const FString &  FileNameWithoutExtension,
const FCSVFileSettings Settings 
)
private

Definition at line 25 of file CSVFile.cpp.

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}
static FString GetDataFolder()
FString FilePath
Definition: CSVFile.h:87
void OpenStream()
Definition: CSVFile.cpp:53
bool CreateUnique
Definition: CSVFile.h:42
FString CustomPath
Definition: CSVFile.h:48

References FCSVFileSettings::CreateUnique, CurrentSettings, FCSVFileSettings::CustomPath, FilePath, UAgrarsensePaths::GetDataFolder(), and OpenStream().

Referenced by CreateCSVFile().

◆ CreateCSVFile()

UCSVFile * UCSVFile::CreateCSVFile ( const FString &  FileNameWithoutExtension,
const FCSVFileSettings Settings 
)
static

Definition at line 14 of file CSVFile.cpp.

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}
void Create(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
Definition: CSVFile.cpp:25

References Create().

Referenced by AInstancedRendererManager::ExportTreesAsCSV().

◆ Destroy()

void UCSVFile::Destroy ( )

Definition at line 125 of file CSVFile.cpp.

126{
127 Close();
128 ConditionalBeginDestroy();
129}
void Close()
Definition: CSVFile.cpp:107

References Close().

Referenced by AInstancedRendererManager::ExportTreesAsCSV().

◆ OpenFilePath()

void UCSVFile::OpenFilePath ( )

Definition at line 131 of file CSVFile.cpp.

132{
133 if (!FilePath.IsEmpty())
134 {
136 }
137}
static void OpenFileExplorer(FString Path)

References FilePath, and UFileUtilities::OpenFileExplorer().

◆ OpenStream()

void UCSVFile::OpenStream ( )
private

Definition at line 53 of file CSVFile.cpp.

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}

References FCSVFileSettings::Append, CSVStream, CurrentSettings, and FilePath.

Referenced by Create(), WriteRaw(), and WriteRow().

◆ WriteRaw()

void UCSVFile::WriteRaw ( const FString &  Line)
private

Definition at line 99 of file CSVFile.cpp.

100{
101 if (!CSVStream.is_open()) OpenStream();
102 if (!CSVStream.is_open()) return;
103
104 CSVStream << TCHAR_TO_UTF8(*Line) << std::endl;
105}

References CSVStream, and OpenStream().

Referenced by Close(), and WriteRow().

◆ WriteRow()

void UCSVFile::WriteRow ( const TArray< FString > &  Cells)

Definition at line 69 of file CSVFile.cpp.

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}
ECSVDelimiter Delimiter
Definition: CSVFile.h:36

References CSVStream, CurrentSettings, FCSVFileSettings::Delimiter, FCSVFileSettings::FileWriteOption, Immediate, OpenStream(), PendingRows, Queue, Semicolon, Tab, and WriteRaw().

Referenced by AInstancedRendererManager::ExportTreesAsCSV().

Member Data Documentation

◆ CSVStream

std::ofstream UCSVFile::CSVStream
private

Definition at line 91 of file CSVFile.h.

Referenced by Close(), OpenStream(), WriteRaw(), and WriteRow().

◆ CurrentSettings

FCSVFileSettings UCSVFile::CurrentSettings
private

Definition at line 89 of file CSVFile.h.

Referenced by Close(), Create(), OpenStream(), and WriteRow().

◆ FilePath

FString UCSVFile::FilePath
private

Definition at line 87 of file CSVFile.h.

Referenced by Create(), OpenFilePath(), and OpenStream().

◆ PendingRows

TArray<FString> UCSVFile::PendingRows
private

Definition at line 85 of file CSVFile.h.

Referenced by Close(), and WriteRow().


The documentation for this class was generated from the following files: