Last active
November 24, 2025 14:36
-
-
Save Lokno/d5aaea0ab7e7f7a706bb9d609ac79123 to your computer and use it in GitHub Desktop.
Streamer.bot C# Script that parses a CSV line in a global into 3 values and stores them in 3 seperate globals. Handles double quotes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| public class CPHInline | |
| { | |
| // parses a CSV line where elements may be wrapped in double quotes | |
| public string[] parse_csv( string line ) | |
| { | |
| string comma = ","; | |
| string quote = "\""; | |
| List<string> elements = []; | |
| line = line.Replace("\\\"","'"); | |
| int max_iterations = 100; | |
| while( line != "" && max_iterations != 0 ) | |
| { | |
| max_iterations--; | |
| int next_comma = line.IndexOf(comma); | |
| int next_quote = line.IndexOf(quote); | |
| if( next_quote != -1 && (next_comma == -1 || ( next_comma > next_quote) ) ) | |
| { | |
| int end_quote = line.IndexOf(quote,next_quote+1); | |
| if( end_quote != -1 ) | |
| { | |
| bool whitespace_only = true; | |
| for( int i = 0; i < next_quote; i++ ) | |
| { | |
| if( line[i] != ' ') | |
| { | |
| whitespace_only = false; | |
| break; | |
| } | |
| } | |
| if( whitespace_only ) | |
| { | |
| elements.Add(line.Substring(next_quote+1,end_quote-next_quote-1).Trim()); | |
| line = (line.Length > (end_quote+1)) ? line.Substring( end_quote+1 ) : ""; | |
| } | |
| else | |
| { | |
| // invalid | |
| line = ""; | |
| break; | |
| } | |
| } | |
| } | |
| else if( next_comma != -1 ) | |
| { | |
| string temp = line.Substring(0,next_comma).Trim(); | |
| if( temp != "" ) elements.Add(line.Substring(0,next_comma).Trim()); | |
| line = line.Substring(next_comma+1); | |
| } | |
| else | |
| { | |
| elements.Add(line.Trim()); | |
| line = ""; | |
| break; | |
| } | |
| } | |
| return elements.ToArray(); | |
| } | |
| public bool Execute() | |
| { | |
| var success = false; | |
| string randomLine = CPH.GetGlobalVar<string>("randomLine", true); | |
| // for simple CSV files where commas only exists as delimiters, you only need these lines | |
| //char[] delimiters = { ',' }; | |
| //string[] words = randomLine.Split(delimiters); | |
| // but to handle double quoted strings with possible commas betwixt we use a custom parsing method | |
| string[] words = parse_csv(randomLine); | |
| if( words.Length == 3 ) | |
| { | |
| CPH.SetGlobalVar("value1", words[0], true); | |
| CPH.SetGlobalVar("value2", words[1], true); | |
| CPH.SetGlobalVar("value3", words[2], true); | |
| success = true; | |
| } | |
| return success; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment