Class CSVFile
- Namespace
- NominalSystems.Core
- Assembly
- NominalSystems.Core.dll
This class is a helper class for managing CSV files, both reading and writing to them. This allows easier reading of data and is also able to interpolate a value between a field. This class is also useful for Nominal Editor and Nominal Studio to read and write data to the file without too much hassle.
public class CSVFile
- Inheritance
-
CSVFile
Constructors
CSVFile()
Default constructor performs any initial actions
public CSVFile()
CSVFile(string, char, bool)
This constructor tanks in a full path to a file and will attempt to load it into the variable.
public CSVFile(string file, char separator = ',', bool header = true)
Parameters
file
stringThe full file path to the file that is loaded
separator
charThe character separator for the file
header
boolA flag indicating whether a header row should exist
Properties
Columns
Defines the number of columns that exist within the CSV file. When reading the data, this is defined as the number of non-empty strings within the first row.
public int Columns { get; }
Property Value
FilePath
The full file path of the file that has been opened.
public string FilePath { get; }
Property Value
IsLoaded
A flag that indicates if the file has been loaded correctly yet or not.
public bool IsLoaded { get; }
Property Value
RawText
The raw text of the file that has been loaded when it is called load. This is also the buffer to write into.
public string RawText { get; }
Property Value
Rows
Defines the number of valid rows that exist within the CSV file.
public int Rows { get; }
Property Value
Separator
The separator between text in the CSV file, both for reading and for writing to the file.
public char Separator { get; }
Property Value
Methods
AddColumn(string)
Adds a column to the end of the list of data and updates the column number. This column will be empty with some default values as provided.
public bool AddColumn(string defaultValue = "")
Parameters
defaultValue
stringThe default empty value that is inserted into every row
Returns
- bool
A success flag
AddColumn(string, string)
Adds a new column to the end of the list of data with a particular name. This column will be empty with some default values as provided.
public bool AddColumn(string column, string defaultValue = "")
Parameters
column
stringThe name of the column to insert
defaultValue
stringThe default empty value that is inserted into every row
Returns
- bool
A success flag
AddRow(params string[])
Adds a new row into the CSV data at the end of the table. If string data is entered into a list containing numeric data then a 0.0 will be inserted instead.
public bool AddRow(params string[] data)
Parameters
data
string[]The array of strings containing the row data to add
Returns
- bool
A successful add
Average(int)
Calculates the average value in the table at some particular column by column index. The column must be a numeric column.
public double Average(int column)
Parameters
column
intThe column index to perform the calculation
Returns
- double
The average value of the column
Average(string)
Calculates the average value in the table at some particular column by column name. The column must be a numeric column.
public double Average(string column)
Parameters
column
stringThe column name to perform the calculation
Returns
- double
The average value of the column
ColumnIndex(string)
Returns the column index of a particular column name within the CSV table. If it doesn't exist, then it will return -1.
public int ColumnIndex(string column)
Parameters
column
stringThe name of the column to get the index of
Returns
- int
The index of the column. If failed, it will be -1
ColumnName(int)
Returns the name of the column at a particular column index.
public string ColumnName(int column)
Parameters
column
intThe column index to get the name of
Returns
- string
The name of the column if it exists
ContainsColumn(string)
Returns whether a particular column exists within the CSV table. This is not case sensitive.
public bool ContainsColumn(string column)
Parameters
column
stringThe name of the column to check
Returns
- bool
A flag indicating whether the column exists
ContainsColumns(params string[])
Returns whether a particular set of columns exist within the CSV table, based on the list of column parameters.
public bool ContainsColumns(params string[] columns)
Parameters
columns
string[]The column list (not case sensitive) to check
Returns
- bool
A flag indicating whether all of the passed columns exist
Create(string, char, params string[])
Attempts to create a new CSV file with the appropriate parameters. This will set up the file for writing data to it. The header column names can be passed in which will define the number of columns. If the file already exists, it will be overwritten.
public bool Create(string file, char separator = ',', params string[] columns)
Parameters
file
stringThe full absolute file path to the CSV file
separator
charThe separator character to use
columns
string[]The array of column names to set in the file
Returns
- bool
A success flag
DeleteAll()
Deletes all rows from the CSV table and keeps only the header column names stored.
public void DeleteAll()
DeleteColumn(int)
Deletes a particular column by index from the table.
public bool DeleteColumn(int column)
Parameters
column
intThe column index to delete
Returns
- bool
A successful delete flag
DeleteColumn(string)
Deletes a particular column by name from the table.
public bool DeleteColumn(string column)
Parameters
column
stringThe column name to delete
Returns
- bool
A successful delete flag
DeleteRow(int)
Deletes a particular row by index from the table.
public bool DeleteRow(int row)
Parameters
row
intThe row index to delete
Returns
- bool
A successful delete flag
DeleteRows(int, int)
Deletes a number of rows based on a starting row in the table and a number of rows to delete.
public void DeleteRows(int index, int count)
Parameters
index
intThe row index to start deleting from
count
intThe number of rows to delete including the first row at the index
GetCellText(int, int)
Attempts to fetch some text contained within a particular row and a particular column based on an index. The first row is NOT the header and the first real row is the data file.
public string GetCellText(int row, int column)
Parameters
row
intThe index (excluding the header) of the CSV table row
column
intThe index of the CSV table column
Returns
- string
The string contained within the cell, if it exists
GetCellText(int, string)
Attempts to fetch some text contained within a particular row and a particular column based on the header name. The first row is NOT the header and the first real row is the data file.
public string GetCellText(int row, string column)
Parameters
Returns
- string
The raw text found within the cell
GetCell<T>(int, int)
Attempts to fetch some text contained within a particular row and a particular column based on an index. The first row is NOT the header and the first real row is the data file. This casts the cell to a particular type.
public T? GetCell<T>(int row, int column)
Parameters
row
intThe index (excluding the header) of the CSV table row
column
intThe index of the CSV table column
Returns
- T
The string contained within the cell, if it exists
Type Parameters
T
The type to convert to, provided it is a valid type
GetCell<T>(int, string)
Attempts to fetch some text contained within a particular row and a particular column based on the header name. The first row is NOT the header and the first real row is the data file. This casts the cell to a particular type.
public T? GetCell<T>(int row, string column)
Parameters
Returns
- T
The raw text found within the cell
Type Parameters
T
The type to convert to, provided it is a valid type
GetColumn(int)
Attempts to fetch a particular column from the data based on the index in the table.
public string[] GetColumn(int column)
Parameters
column
intThe index of the CSV table column
Returns
- string[]
An array of strings of the column
GetColumn(string)
Attempts to fetch a particular column from the data based on the header of the column.
public string[] GetColumn(string column)
Parameters
column
stringThe table column name
Returns
- string[]
An array of strings of the column
GetData<T>()
Attempts to get the entire data system in a particular formatted list of some defined data type, assuming that the data type can be formatted in the correct way.
public List<List<T>> GetData<T>()
Returns
Type Parameters
T
The data type that the list should be formatted as.
GetHeaders()
Returns a string array of the headers of the CSV file if they exist.
public string[] GetHeaders()
Returns
- string[]
The list of headers if they exist
GetRow(int)
Attempts to fetch a particular row from the data based on the index in the table. In this case, the first row is NOT the header and the first real row is the data file.
public string[] GetRow(int row)
Parameters
row
intThe index (excluding the header) of the CSV table row
Returns
- string[]
An array of strings of the row
InsertRow(int, params string[])
Inserts a new row into the CSV data at a specific index. This will add the row in before the index of the current data and increase the list. If string data is entered into a list containing numeric data then a 0.0 will be inserted instead.
public bool InsertRow(int index, params string[] data)
Parameters
index
intThe index to which to add the new row to
data
string[]The array of strings containing the row data to add
Returns
- bool
A successful insertion
Load(string, char, bool)
Attempts to load the file into the data. If the file cannot be opened or the file does not exist, then the method will return a failure flag.
public bool Load(string file, char separator = ',', bool header = true)
Parameters
file
stringThe full absolute path to the file
separator
charThe character separator for the file
header
boolA flag indicating whether a header row should exist
Returns
- bool
A successful load flag
Lookup(int, int, double, bool)
Attempts to lookup a particular double value from within the CSV file, matching the value passed in to the lookup column assuming that the CSV is ordered in ascending order by that column. The output column is the value that will returned based on the input (or lookup) value. The lerp flag allows for a linear interpolation between the two closest results (if they exist), allowing for a seamless lookup.
public double Lookup(int lookupColumn, int outputColumn, double value, bool lerp = true)
Parameters
lookupColumn
intThe column index to lookup with the independent variable
outputColumn
intThe value column index with the dependent variable to Lerp
value
doubleThe independent value for which to perform the lookup on
lerp
boolA flag for performing a Linear Interpolate between the closest values
Returns
- double
The closest value from the table that is looked up
Lookup(string, string)
Performs a lookup on a particular column and returns the first row where a particular value matches the column exactly. If the value is not exactly the same or no row could be found, then an empty string array will be returned.
public string[] Lookup(string lookupColumn, string value)
Parameters
lookupColumn
stringThe column name to lookup in the table
value
stringThe value within the lookup column to match
Returns
- string[]
The row at which the lookup matches, if it exists
Lookup(string, string, double, bool)
Attempts to lookup a particular double value from within the CSV file, matching the value passed in to the lookup column assuming that the CSV is ordered in ascending order by that column. The output column is the value that will returned based on the input (or lookup) value. The lerp flag allows for a linear interpolation between the two closest results (if they exist), allowing for a seamless lookup.
public double Lookup(string lookupColumn, string outputColumn, double value, bool lerp = true)
Parameters
lookupColumn
stringThe column name to lookup with the independent variable
outputColumn
stringThe value column name with the dependent variable to Lerp
value
doubleThe independent value for which to perform the lookup on
lerp
boolA flag for performing a Linear Interpolate between the closest values
Returns
- double
The closest value from the table that is looked up
LookupRowIndexAboveLimit(string, double, int, int)
Finds the row index of which a particular row is above a limit defined. The lookup column to check is also defined and if the value is greater than or equal to the limit, then the row index will be returned. If no row reaches the limit, a -1 is returned.
public int LookupRowIndexAboveLimit(string lookupColumn, double limit, int decimals = 6, int start = 0)
Parameters
lookupColumn
stringThe column name to lookup
limit
doubleThe limited value to check is reached
decimals
intThe number of decimal places to round to
start
intA lookup index to start at if known
Returns
- int
The row index of the first row that reaches the limit
LookupRowIndexBelowLimit(string, double, int)
Finds the row index of which a particular row is below a limit defined. The lookup column to check is also defined and if the value is less than or equal to the limit, then the row index will be returned. If no row reaches the limit, a -1 is returned.
public int LookupRowIndexBelowLimit(string lookupColumn, double limit, int decimals = 6)
Parameters
lookupColumn
stringThe column name to lookup
limit
doubleThe limited value to check is reached
decimals
intThe number of decimal places to round to
Returns
- int
The row index of the first row that reaches below the limit
Maximum(int)
Calculates the maximum value in the table at some particular column by column index. The column must be a numeric column.
public double Maximum(int column)
Parameters
column
intThe column index to perform the calculation
Returns
- double
The maximum value of the column
Maximum(string)
Calculates the maximum value in the table at some particular column by column name. The column must be a numeric column.
public double Maximum(string column)
Parameters
column
stringThe column name to perform the calculation
Returns
- double
The maximum value of the column
Minimum(int)
Calculates the minimum value in the table at some particular column by column index. The column must be a numeric column.
public double Minimum(int column)
Parameters
column
intThe column index to perform the calculation
Returns
- double
The minimum value of the column
Minimum(string)
Calculates the minimum value in the table at some particular column by column name. The column must be a numeric column.
public double Minimum(string column)
Parameters
column
stringThe column name to perform the calculation
Returns
- double
The minimum value of the column
Save()
Saves the data to the current stored file. This will ensure that the data is saved correct and will update the file with any changes. This is saved to the file that is set when the CSV reader is loaded.
public bool Save()
Returns
- bool
A successful save flag
SaveAs(string)
Attempts to save the file to the CSV file with a particular filename. This will overwrite a file if it already exists or will create a new one if required and will save the file based on the current state of the data.
public bool SaveAs(string filename)
Parameters
filename
stringThe full path to the file that will be saved
Returns
- bool
A successful save flag
SetCellText(int, int, string)
Updates the text within a particular cell based on the row index and the column index.
public bool SetCellText(int row, int column, string text)
Parameters
row
intThe row index of the data to update
column
intThe column index of the data to update
text
stringThe text to update in the cell
Returns
- bool
A success flag
SetCellText(int, string, string)
Updates the text within a particular cell based on the row index and the column name.
public bool SetCellText(int row, string column, string text)
Parameters
row
intThe row index of the data to update
column
stringThe column name of the data to update
text
stringThe text to update in the cell
Returns
- bool
A success flag
SetCell<T>(int, int, T)
Updates the text within a particular cell based on the row index and the column index to a particular text of some type.
public bool SetCell<T>(int row, int column, T value)
Parameters
row
intThe row index of the data to update
column
intThe column index of the data to update
value
TThe text to update in the cell
Returns
- bool
A success flag
Type Parameters
T
The type of object to convert to a string
SetCell<T>(int, string, T)
Updates the text within a particular cell based on the row index and the column name to a particular text of some type.
public bool SetCell<T>(int row, string column, T value)
Parameters
row
intThe row index of the data to update
column
stringThe column name of the data to update
value
TThe text to update in the cell
Returns
- bool
A success flag
Type Parameters
T
The type of object to convert to a string
Sort(int, bool)
Attempts to sort the table by a particular column in either an ascending or descending value. This will sort all of the rows and all columns by this value.
public bool Sort(int column, bool ascending = true)
Parameters
column
intThe column index to sort by
ascending
boolA flag indicating whether to sort ascending or descending
Returns
- bool
A successful sorting flag
Sort(string, bool)
Attempts to sort the table by a particular column in either an ascending or descending value. This will sort all of the rows and all columns by this value.
public bool Sort(string column, bool ascending = true)
Parameters
column
stringThe column name to sort by
ascending
boolA flag indicating whether to sort ascending or descending
Returns
- bool
A successful sorting flag
Sum(int)
Calculates the sum value in the table at some particular column by column index. The column must be a numeric column.
public double Sum(int column)
Parameters
column
intThe column index to perform the calculation
Returns
- double
The sum value of the column
Sum(string)
Calculates the sum value in the table at some particular column by column name. The column must be a numeric column.
public double Sum(string column)
Parameters
column
stringThe column name to perform the calculation
Returns
- double
The sum value of the column
ToString()
public override string ToString()