Procházet zdrojové kódy

Added text.csv to sidebar.

Brucey před 2 roky
rodič
revize
583c076508

+ 31 - 0
docs/api/text/text.csv/scsvcolumn.md

@@ -0,0 +1,31 @@
+---
+id: scsvcolumn
+title: SCsvColumn
+sidebar_label: SCsvColumn
+---
+
+A csv column.
+
+
+## Fields
+
+### `Field length:Size_T`
+
+The length of the column.
+
+<br/>
+
+### `Field quoted:Byte`
+
+A non-zero value indicates that the column is quoted.
+
+<br/>
+
+## Methods
+
+### `Method GetValue:String()`
+
+Returns the value of the column, or [Null](../../../brl/brl.blitz/#null) if the column is empty.
+
+<br/>
+

+ 32 - 0
docs/api/text/text.csv/tcsvheader.md

@@ -0,0 +1,32 @@
+---
+id: tcsvheader
+title: TCsvHeader
+sidebar_label: TCsvHeader
+---
+
+A csv header.
+
+
+A header consists of 1 or more rows.
+
+
+## Methods
+
+### `Method IndexForName:Int(name:String)`
+
+Returns the column index for the column header <b>name</b>.
+
+<br/>
+
+### `Method ColumnCount:Size_T()`
+
+Returns the number of header columns.
+
+<br/>
+
+### `Method GetHeader:String(index:Size_T)`
+
+Returns the header for the given <b>index</b>.
+
+<br/>
+

+ 65 - 0
docs/api/text/text.csv/tcsvoptions.md

@@ -0,0 +1,65 @@
+---
+id: tcsvoptions
+title: TCsvOptions
+sidebar_label: TCsvOptions
+---
+
+Options for customising the parser.
+
+
+## Fields
+
+### `Field maxColumns:UInt = 1024`
+
+Maximum number of columns to parse.
+
+Defaults to 1024.
+
+
+<br/>
+
+### `Field delimiter:String = ","`
+
+The delimiter.
+
+Typically a comma or tab. Can be any char other than newline, form feed or quote. Defaults to comma.
+
+
+<br/>
+
+### `Field noQuotes:Int`
+
+When enabled, indicates that the parser should treat double-quotes just like any ordinary character.
+
+<br/>
+
+### `Field insertHeaderRow:String`
+
+If the actual data does not have a header row with column names, the caller should provide one (in CSV format) which will be treated as if it was the first row of data.
+
+<br/>
+
+### `Field headerSpan:UInt = 1`
+
+The number of rows that the header row spans.
+
+If 0 or 1, header is assumed to span 1 row otherwise, set to number > 1 to span multiple rows.
+
+
+<br/>
+
+### `Field rowsToIgnore:UInt`
+
+The number of rows to ignore before the initial row is processed.
+
+<br/>
+
+### `Field keepEmptyHeaderRows:UInt`
+
+By default, ignores empty header rows.
+
+To disable this behaviour, set to [True](../../../brl/brl.blitz/#true).
+
+
+<br/>
+

+ 54 - 0
docs/api/text/text.csv/tcsvparser.md

@@ -0,0 +1,54 @@
+---
+id: tcsvparser
+title: TCsvParser
+sidebar_label: TCsvParser
+---
+
+Csv Parser
+
+
+The parser expects at least 1 header row. If a file does not have a header row, one can be
+provided by setting TCsvOptions.insertHeaderRow with an appropriate value.
+
+
+## Methods
+
+### `Method NextRow:ECsvStatus()`
+
+Fetches the next row.
+
+#### Returns
+Returns ECsvStatus.row when a row is retrieved.
+
+
+<br/>
+
+### `Method GetRow:TCsvRow()`
+
+Returns the current row.
+
+The cell values are only valid until the next call to [NextRow](../../../text/text.csv/tcsvparser/#method-nextrowecsvstatus).
+
+
+<br/>
+
+### `Method Free()`
+
+Frees the parser and any associated data.
+
+<br/>
+
+## Functions
+
+### `Function Parse:TCsvParser(path:String, opts:TCsvOptions)`
+
+Creates a new [TCsvParser](../../../text/text.csv/tcsvparser) instance, using the given path.
+
+<br/>
+
+### `Function Parse:TCsvParser(stream:TStream, options:TCsvOptions = Null)`
+
+Creates a new [TCsvParser](../../../text/text.csv/tcsvparser) instance, using the provided [TStream](../../../brl/brl.stream/tstream).
+
+<br/>
+

+ 53 - 0
docs/api/text/text.csv/tcsvrow.md

@@ -0,0 +1,53 @@
+---
+id: tcsvrow
+title: TCsvRow
+sidebar_label: TCsvRow
+---
+
+A Csv Row.
+
+
+## Methods
+
+### `Method ColumnCount:Size_T()`
+
+Returns the number of columns in the row.
+
+<br/>
+
+### `Method GetColumn:SCsvColumn(index:Size_T)`
+
+Returns the column at the given index.
+
+<br/>
+
+### `Method GetColumn:SCsvColumn(index:Int)`
+
+Returns the column at the given index.
+
+<br/>
+
+### `Method GetColumn:SCsvColumn(name:String)`
+
+Returns the column with the given column <b>name</b>.
+
+<br/>
+
+### `Method GetValue:String(index:Size_T)`
+
+Returns the value of the column at the given <b>index</b>.
+
+<br/>
+
+### `Method GetValue:String(name:String)`
+
+Returns the value of the given column <b>name</b>.
+
+<br/>
+
+### `Method GetHeader:TCsvHeader()`
+
+Returns the header.
+
+<br/>
+

+ 85 - 0
docs/api/text/text_csv.md

@@ -0,0 +1,85 @@
+---
+id: text.csv
+title: Text.CSV
+sidebar_label: Introduction
+---
+
+
+Text.CSV is a CSV (Comma Separated Values) file parser. CSV files are a popular format for exchanging data between
+applications, as they are simple, flexible, and can be easily generated or consumed by various software programs.
+
+We will explore the CSV file format in depth, and learn how to use the Text.CSV to load and manipulate CSV files in
+BlitzMax applications.
+
+## CSV File Format
+
+CSV files are plain text files that store tabular data, making them suitable for representing spreadsheet-like
+structures. Each row in a CSV file corresponds to a single line of text, and columns within a row are separated by
+a delimiter, which is typically a comma (`,`) or a tab (`\t`). CSV files can store various types of data, such as
+numbers, text, and dates, and they can accommodate both simple and complex data structures.
+
+There are several reasons why one might choose to use the CSV file format over other formats:
+
+1. **Simplicity** : CSV files are easy to create and understand, as they have a straightforward structure and use plain
+text encoding.
+2. **Portability** : CSV files can be read and written by a wide variety of software applications, including
+spreadsheet programs, databases, and data analysis tools.
+3. **Size** : Because CSV files are plain text, they can be compressed efficiently, reducing storage and
+transmission costs.
+4. **Human Readable** : Unlike binary formats, CSV files can be opened and examined with a simple text editor,
+making it easier to identify and troubleshoot issues.
+
+To create a valid CSV file that can be loaded into Text.CSV, you should follow these basic rules:
+
+1. **Consistent Delimiters** : Use the same delimiter character (e.g., a comma or a tab) to separate columns
+throughout the entire file. Mixing delimiters within the same file can lead to parsing errors.
+2. **Quoting** : If a field value contains the delimiter character, newlines, or double quotes, enclose the value
+in double quotes. For example, `"John Doe", 25, "123 Main St, Apt 4B"` has the address field quoted because it contains
+a comma. To represent a double quote within a quoted field, use two double quotes together, like `""`. For example,
+`"He said, ""Hello"""`
+3. **Header Row** : It's a good practice to include a header row at the beginning of the file that lists the column
+names. This makes it easier to understand the structure and content of the file.
+4. **Consistent Rows** : Ensure that each row has the same number of columns. This helps prevent parsing errors and
+ensures that the data can be processed correctly.
+5. **UTF-8 Encoding** : Save the file using UTF-8 encoding to ensure that it can be read correctly by the parser.
+
+## Parsing CSV Files
+
+You'll need to create an instance of the [TCsvParser](../../text/text.csv/tcsvparser) type, and then use its various methods to interact with the
+CSV data. The parser accepts input streams in UTF-8 format.
+
+Here is a basic example of how to use the module:
+
+```blitzmax
+SuperStrict
+
+Framework BRL.StandardIO
+Import Text.CSV
+
+Local options:TCsvOptions = New TCsvOptions
+options.delimiter = ","
+
+Local parser:TCsvParser = TCsvParser.Parse("path/to/your/csvfile.csv", options)
+
+While parser.NextRow() = ECsvStatus.row
+	Local row:TCsvRow = parser.GetRow()
+	Print row.GetValue("column_name")
+Wend
+
+parser.Free()
+```
+
+
+## Types
+| Type | Description |
+|---|---|
+| [TCsvParser](../../text/text.csv/tcsvparser) | Csv Parser |
+| [TCsvOptions](../../text/text.csv/tcsvoptions) | Options for customising the parser. |
+| [TCsvRow](../../text/text.csv/tcsvrow) | A Csv Row. |
+| [TCsvHeader](../../text/text.csv/tcsvheader) | A csv header. |
+
+## Structs
+| Struct | Description |
+|---|---|
+| [SCsvColumn](../../text/text.csv/scsvcolumn) | A csv column. |
+

+ 12 - 0
website/sidebars.json

@@ -392,6 +392,18 @@
       "api/brl/brl.wavloader"
     ],
     "Text": [
+      {
+        "type": "subcategory",
+        "label": "CSV",
+        "ids": [
+          "api/text/text.csv",
+          "api/text/text.csv/tcsvparser",
+          "api/text/text.csv/tcsvheader",
+          "api/text/text.csv/tcsvrow",
+          "api/text/text.csv/tcsvoptions",
+          "api/text/text.csv/scsvcolumn"
+        ]
+      },
       {
         "type": "subcategory",
         "label": "Format",