public static string[] SplitValuesDelimitedByTabOrMultipleSpaces(string sVal) { string[] sarrReturn=null; ArrayList alNow=null; if (sVal!=null) { alNow=new ArrayList(); bool bFoundMoreThanOneValueLeftInDestructableString_Now=true;//MUST start true so first run of loop happens int[] iarrBreak=new int[sarrTabSeparatedSeparationsStartWith.Length]; int iBreakAt=-1;//the current STRING index in the Haystack string int iBreakType=0;//the current separator string ARRAY index in the for loop int iBreakTypeFirst=-1;//the string ARRAY index first separator (in the separator array) that occurs in the Haystack string while (bFoundMoreThanOneValueLeftInDestructableString_Now) { bFoundMoreThanOneValueLeftInDestructableString_Now=false; for (int iFlag=0; iFlag<sarrTabSeparatedLineHasMoreThanOneChunkIfContains.Length; iFlag++) { if ( RString.Contains(sVal,sarrTabSeparatedLineHasMoreThanOneChunkIfContains[iFlag]) ) { //This is a valid way of knowing if has inline column names, since removed whitespace from ends of destructable string already after each substring operation above bFoundMoreThanOneValueLeftInDestructableString_Now=true; //Do NOT break here, as code below must run. } } if (bFoundMoreThanOneValueLeftInDestructableString_Now) { for (iBreakType=0; iBreakType<iarrBreak.Length; iBreakType++) { iarrBreak[iBreakType]=sVal.IndexOf(sarrTabSeparatedSeparationsStartWith[iBreakType]); } iBreakAt=-1; iBreakTypeFirst=-1; for (iBreakType=0; iBreakType<iarrBreak.Length; iBreakType++) { if ( (iarrBreak[iBreakType]>=0) && (iBreakAt<0||iarrBreak[iBreakType]<iBreakAt) ) { iBreakAt=iarrBreak[iBreakType]; iBreakTypeFirst=iBreakType; } } if (iBreakAt>=0) { string sPart0=RString.SafeSubstring(sVal,0,iBreakAt); sVal=RString.SafeSubstring(sVal,iBreakAt+sarrTabSeparatedSeparationsStartWith[iBreakTypeFirst].Length); RString.RemoveEndsWhiteSpace(ref sPart0); RString.RemoveEndsWhiteSpace(ref sVal); alNow.Add(sPart0); } } else {//else only one left alNow.Add(sVal);//get leftover chunk that does not contain the tabs nor multiple spaces break; } }//end while if ( alNow!=null && alNow.Count>0 ) {//is an inline table sarrReturn=new string[alNow.Count]; for (int iChunk=0; iChunk<alNow.Count; iChunk++) { sarrReturn[iChunk]=(string)alNow[iChunk]; } } } //else got a null string so leave return as null array return sarrReturn; }//end SplitValuesDelimitedByTabOrMultipleSpaces
}//end LiteralFieldToCSVField public static string CSVFieldToLiteralField(string sCSVField, char cFieldDelimX, char cTextDelimX) {//aka CSVFieldToString if (RString.Contains(sCSVField,cTextDelimX)) { if (sCSVField.Length>=2&&sCSVField[0]==cTextDelimX&&sCSVField[sCSVField.Length-1]==cTextDelimX) { sCSVField=sCSVField.Substring(1,sCSVField.Length-2);//debug performance (recreating string) } sCSVField=RString.Replace(sCSVField,char.ToString(cTextDelimX)+char.ToString(cTextDelimX),char.ToString(cTextDelimX));//debug performance (recreating string, type conversion from char to string) } for (int iNow=0; iNow<sarrEscapeLiteral.Length; iNow++) { sCSVField=RString.Replace(sCSVField,sarrEscapeSymbolic[iNow],sarrEscapeLiteral[iNow]);// (old,new) } return sCSVField;//now a raw field }
private static IsBlockable(string sWord) //TODO: remove periods first before separating words //TODO: account for per-word bits and FilterText option bits { bool bBlock = false; int iNow; int iChar; try { //Change numbers and symbols to letters first: //TODO: do fuzzy compare using Alternates[][] char instead ([][0] is primary) //for (iNow=0; iNow<carrFixSymSrc.Length; iNow++) { // for (iChar=0; iNow<sWord.Length; iNow++) { // if (sWord[iChar]==carrFixSymSrc[iChar]) sWord[iChar]=carrFixSymSrc[iChar]; // } //} for (iNow = 0; iNow < sarrUnSparseSrc.Length; iNow++) { sWord = sWord.Replace(sarrUnSparseSrc[iNow], sarrUnSparseDest[iNow]); } for (iNow = 0; iNow < sarrIfWhole.Length; iNow++) { if (sWord == sarrIfWhole[iNow]) { bBlock = true; break; } } if (!bBlock) { //TODO: first remove spaces to check sarrIfPartial for (iNow = 0; iNow < sarrIfPartial.Length; iNow++) { if (RString.Contains(sWord, sarrIfPartial[iNow])) { bBlock = true; break; } } } } catch (Exception exn) { RReporting.ShowExn(exn, "FilterText IsBlockable") } return(bBlock); }
public static bool IsReservedUserName(ref string sTest) { //TODO: also check FilterText.IsBlockable(sTest) if (sTest==null) return true; string sTestToLower=sTest.ToLower(); if (sTestToLower==TheFakeUserNameDenotingAnExeption) return true; else if (RString.Contains(sTestToLower,"admin")) return true; else if (sTestToLower=="administrator") return true; else if (sTestToLower=="all") return true; //so "/to all Hello Everyone!" works else if (sTestToLower=="noone") return true; //so TheFakeUserNameDenotingAnExeption works //debug NYI check if CONTAINS: else if (RString.Contains(sTestToLower,"neoarmor")) return true; else if (RString.Contains(sTestToLower,"neoarmor")) return true; else if (RString.Contains(sTestToLower,"expert multimedia")) return true; else if (RString.Contains(sTestToLower,"expertmultimedia")) return true; else if (RString.Contains(sTestToLower,"jakegustafson")) return true; else if (RString.Contains(sTestToLower,"jake gustafson")) return true; else return false; }
}//end GetMissingColumns public static string LiteralFieldToCSVField(string sLiteralField, char cFieldDelimX, char cTextDelimX, bool bReplaceNewLineWithTabInsteadOfHTMLBrWithMarkerProperty) { //aka ToCSVField if (bReplaceNewLineWithTabInsteadOfHTMLBrWithMarkerProperty) { sLiteralField=RString.Replace(sLiteralField,"\r\n","\t");// (old,new) sLiteralField=RString.Replace(sLiteralField,"\r","\t");// (old,new) sLiteralField=RString.Replace(sLiteralField,"\n","\t");// (old,new) } for (int iNow=0; iNow<sarrEscapeLiteral.Length; iNow++) { sLiteralField=RString.Replace(sLiteralField,sarrEscapeLiteral[iNow],sarrEscapeSymbolic[iNow]);// (old,new) } //debug performance: conversions and additions for double delimiters if (RString.Contains(sLiteralField,cTextDelimX)) { sLiteralField=RString.Replace(sLiteralField,char.ToString(cTextDelimX),char.ToString(cTextDelimX)+char.ToString(cTextDelimX));//debug performance sLiteralField=char.ToString(cTextDelimX)+sLiteralField+char.ToString(cTextDelimX);//debug performance } else if ( RString.Contains(sLiteralField,cFieldDelimX) || RString.Contains(sLiteralField,'\n') || RString.Contains(sLiteralField,'\r') ) { sLiteralField=char.ToString(cTextDelimX)+sLiteralField+char.ToString(cTextDelimX);//debug performance } return sLiteralField;//now a CSV field }//end LiteralFieldToCSVField
public bool Load(string sFile, bool FirstRowHasTitles) { sLastFile=sFile; if (sLastFile==null) sLastFile=""; bool bGood=false; bFirstRowLoadAndSaveAsTitles=FirstRowHasTitles; StreamReader fsSource=null; string sLine=null; try { fsSource=new StreamReader(sFile); if (bFirstRowLoadAndSaveAsTitles) { sLine=fsSource.ReadLine(); if ( bShowNewlineWarning && (RString.Contains(sLine,'\n')||RString.Contains(sLine,'\r')) ) { MessageBox.Show("Warning: newline character found in field. File may have been saved in a different operating system and need line breaks converted."); bShowNewlineWarning=false; } teTitles=new TableEntry(RTable.SplitCSV(sLine,cFieldDelimiter,cTextDelimiter)); //Parse TYPE NAME{METANAME:METAVALUE;...} title row notation: if (teTitles.Columns>0) { sarrFieldMetaData=new string[teTitles.Columns]; iarrFieldType=new int[teTitles.Columns]; for (int iColumn=0; iColumn<teTitles.Columns; iColumn++) { string FieldDataNow=teTitles.Field(iColumn); if (FieldDataNow==null) { RReporting.ShowErr("Field is not accessible","loading csv file","Load("+RReporting.StringMessage(sFile,true)+",...){Row 0:Titles; Column:"+iColumn+"}"); } int iType=StartsWithType(FieldDataNow); int iStartName=0; if (iType>-1) { iarrFieldType[iColumn]=iType; iStartName=sarrType[iType].Length+1; //teTitles.SetField(iColumn,RString.SafeSubstring(teTitles.Field(iColumn),sarrType[iType].Length+1)); } else { RReporting.Debug("Unknown type in column#"+iColumn.ToString()+"("+RReporting.StringMessage(FieldDataNow,true)+")"); } int iMetaData=-1; //if (FieldDataNow!=null) { iMetaData=FieldDataNow.IndexOf("{"); //} if (iMetaData>-1) { //string FieldDataNow=teTitles.Field(iColumn); if (FieldDataNow==null) { RReporting.ShowErr("Can't access field","loading csv file","rtable Load("+RReporting.StringMessage(sFile,true)+"){Row:titles; Column:"+iColumn+"}"); } this.sarrFieldMetaData[iColumn]=FieldDataNow.Substring(iMetaData); while (iMetaData>=0 && (FieldDataNow[iMetaData]=='{'||FieldDataNow[iMetaData]==' ')) iMetaData--; teTitles.SetField(iColumn,RString.SafeSubstringByInclusiveEnder(FieldDataNow,iStartName,iMetaData)); } else { teTitles.SetField(iColumn,RString.SafeSubstring(FieldDataNow,iStartName)); } }//end for iColumn in title row }//end if teTitles.Columns>0 }//if bFirstRowLoadAndSaveAsTitles tearr=new TableEntry[256]; for (int iNow=0; iNow<tearr.Length; iNow++) { tearr[iNow]=null; } iRows=0; //if (!bFirstRowLoadAndSaveAsTitles||sLine!=null) { if (bAllowNewLineInQuotes) { bool bInQuotes=false; string sLineCombined=""; while ( (sLine=fsSource.ReadLine()) != null ) { if (iRows>=Maximum) Maximum=iRows+iRows/2+1; for (int iChar=0; iChar<RString.SafeLength(sLine); iChar++) { if (sLine[iChar]==this.cTextDelimiter) bInQuotes=!bInQuotes; } sLineCombined+=sLine; if (!bInQuotes) { tearr[iRows]=new TableEntry(RTable.SplitCSV(sLineCombined,cFieldDelimiter,cTextDelimiter)); iRows++; sLineCombined=""; } }//end while not end of file if (sLineCombined!="") { //get bad data so it doesn't get lost tearr[iRows]=new TableEntry(RTable.SplitCSV(sLineCombined,cFieldDelimiter,cTextDelimiter)); iRows++; } } else { while ( (sLine=fsSource.ReadLine()) != null ) { if (iRows>=Maximum) Maximum=iRows+iRows/2+1; tearr[iRows]=new TableEntry(RTable.SplitCSV(sLine,cFieldDelimiter,cTextDelimiter)); iRows++; } } //}//if any data rows if (iRows<Maximum) { for (int i=iRows; i<Maximum; i++) { tearr[i]=new TableEntry(); } } bGood=true; fsSource.Close(); } catch (Exception exn) { RReporting.ShowExn(exn,"Loading table","rtable Load(\""+RReporting.StringMessage(sFile,true)+"\",FirstRowHasTitles="+(FirstRowHasTitles?"yes":"no")+")"); try { fsSource.Close(); } catch (Exception exn2) { RReporting.ShowExn(exn2,"closing file after exception","rtable Load"); } } return bGood; }//end Load