//-------------------------------------- //--- 010 Editor v6.0.1 Script File // File: ParseCSV.1sc // Author: Artur Babecki , artur.babecki@gmail.com // Revision: 13.03.2015 // Purpose: the set of functions to parse CSV with an example. // the other separator than "," can be specified. // // int FindFields(char line[],int idx[],char separator) // line - line to parse,idx- array of the positions of the separators // returns number of fields // char[] GetField (char line[],int idx[], int n,int trim) // line - line to parse, idx -array of positions from FindFields // n - requested field number, trim - false: do not trim whitespaces at the separators, true: trim whitespaces // returns requested field // char [] TrimWhiteSpaces(char line[]) // Remove whitespaces from the head and the tail of the string ( " " and "\t" ) //-------------------------------------- // begin of the example int idx[64]; //array for keeping field indexes char line[1024]; string field; int line_number; int linesize; int number_of_fields, field_number; RequiresVersion( 6,0,0 ); // regular expressions are in use if(GetFileInterface()!="Text") {Printf("\n This is not text file!!!\n");return;} for (line_number=0;line_number0) // parse only not empty line { // parse current line number_of_fields=FindFields(line,idx,','); // Printf("number of fields: %d\n",number_of_fields); // get and print fields found , trim whitespaces for(field_number=0;field_number=0){ result = RegExSearch(line,sep,size,start ); idx[i]=result; start=result+1; i++; } idx[i-1]=Strlen(line); i--; return i; } //------------------------------------------ char[] GetField (char line[],int idx[], int n,int trim) { // get field n from line using the idx array initialzed by FindFields function . First field is n=0. // trim - false: get field as is, true: remove white spaces from the head and the tail. // if (n==0) { if (trim==0) return SubStr(line,idx[n],idx[n+1]-idx[n]); else return TrimWhiteSpaces(SubStr(line,idx[n],idx[n+1]-idx[n])); } else { if (trim==0) return SubStr(line,idx[n]+1,idx[n+1]-idx[n]-1); else return TrimWhiteSpaces(SubStr(line,idx[n]+1,idx[n+1]-idx[n]-1)); } } //-------------------------------------------