uniblab

Fundamental file helpers

Nov 30th, 2020 (edited)
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.17 KB | None | 0 0
  1. // Copyright 2020, Timothy J. Bruce
  2. using System.Linq;
  3.  
  4. namespace Icod.TestRig {
  5.  
  6.     public static class FileHelper {
  7.  
  8.         #region fields
  9.         public const System.Int32 EOL = -1;
  10.         public const System.Int32 EOF = EOL;
  11.         #endregion fields
  12.  
  13.  
  14.         #region methods
  15.         public static System.String PathCombine( System.String path, System.Char pathSeparator, System.String name ) {
  16.             if ( System.String.IsNullOrEmpty( name ) ) {
  17.                 throw new System.ArgumentNullException( "name" );
  18.             } else if ( System.String.IsNullOrEmpty( path ) ) {
  19.                 throw new System.ArgumentNullException( "path" );
  20.             }
  21.             var sep = pathSeparator.ToString();
  22.             while ( path.EndsWith( sep ) ) {
  23.                 path = path.Substring( 0, path.Length - 1 );
  24.             }
  25.             while ( name.StartsWith( sep ) ) {
  26.                 name = name.Substring( 1 );
  27.             }
  28.             return path + sep + name;
  29.         }
  30.  
  31.         public static System.String ReadLine( this System.IO.TextReader file, System.String recordSeparator, System.Char quoteChar ) {
  32.             if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  33.                 throw new System.ArgumentNullException( "recordSeparator" );
  34.             } else if ( null == file ) {
  35.                 throw new System.ArgumentNullException( "file" );
  36.             } else if ( ( 1 == recordSeparator.Length ) && recordSeparator.Equals( quoteChar.ToString() ) ) {
  37.                 throw new System.InvalidOperationException( "Quote character and record separator cannot be the same." );
  38.             } else if ( EOF == file.Peek() ) {
  39.                 return null;
  40.             }
  41.  
  42.             var output = new System.Text.StringBuilder();
  43.             var leLen = recordSeparator.Length;
  44.             var lenStop = leLen - 1;
  45.             System.Int32 i = 0;
  46.             System.Char c;
  47.             System.Boolean isPlaintext = true;
  48.             System.Int32 p = file.Read();
  49.             System.Char q;
  50.             while ( EOL != p ) {
  51.                 c = System.Convert.ToChar( p );
  52.                 output = output.Append( c );
  53.                 if ( isPlaintext ) {
  54.                     if ( quoteChar.Equals( c ) ) {
  55.                         isPlaintext = false;
  56.                     } else if ( ( leLen <= output.Length ) && ( recordSeparator[ i ].Equals( output[ ( output.Length - leLen ) - i ] ) ) ) {
  57.                         if ( lenStop <= ++i ) {
  58.                             output.Remove( output.Length - leLen, leLen );
  59.                             break;
  60.                         }
  61.                     } else {
  62.                         i = 0;
  63.                     }
  64.                 } else {
  65.                     if ( quoteChar.Equals( c ) ) {
  66.                         p = file.Peek();
  67.                         if ( EOL == p ) {
  68.                             throw new System.IO.EndOfStreamException();
  69.                         }
  70.                         c = System.Convert.ToChar( p );
  71.                         if ( quoteChar.Equals( c ) ) {
  72.                             file.Read();
  73.                             output = output.Append( c );
  74.                         } else {
  75.                             i = 0;
  76.                             isPlaintext = true;
  77.                         }
  78.                     }
  79.                 }
  80.                 p = file.Read();
  81.             }
  82.  
  83.             return output.ToString();
  84.         }
  85.         public static System.String ReadLine( this System.IO.TextReader file, System.String recordSeparator ) {
  86.             if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  87.                 throw new System.ArgumentNullException( "recordSeparator" );
  88.             } else if ( null == file ) {
  89.                 throw new System.ArgumentNullException( "file" );
  90.             } else if ( EOF == file.Peek() ) {
  91.                 return null;
  92.             }
  93.  
  94.             System.Boolean isReading = true;
  95.             var rs = recordSeparator.ToCharArray();
  96.             System.Int32 i = 0;
  97.             var maxI = rs.Length;
  98.             var line = new System.Text.StringBuilder();
  99.             System.Int32 j = 0;
  100.             System.Int32 c;
  101.             System.Boolean isNull = true;
  102.             do {
  103.                 c = file.Read();
  104.                 if ( -1 == c ) {
  105.                     maxI = 0;
  106.                     isReading = false;
  107.                     break;
  108.                 }
  109.                 isNull = false;
  110.                 line.Append( System.Convert.ToChar( c ) );
  111.                 if ( line[ j ].Equals( rs[ i ] ) ) {
  112.                     i++;
  113.                 } else {
  114.                     i = 0;
  115.                 }
  116.                 if ( i == maxI ) {
  117.                     isReading = false;
  118.                     break;
  119.                 }
  120.                 j++;
  121.             } while ( isReading );
  122.             line.Remove( line.Length - maxI, maxI );
  123.             return isNull ? null : line.ToString();
  124.         }
  125.         public static System.String ReadLine( this System.IO.StreamReader file, System.String recordSeparator, System.Char quoteChar ) {
  126.             if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  127.                 throw new System.ArgumentNullException( "recordSeparator" );
  128.             } else if ( null == file ) {
  129.                 throw new System.ArgumentNullException( "file" );
  130.             } else if ( ( 1 == recordSeparator.Length ) && recordSeparator.Equals( quoteChar.ToString() ) ) {
  131.                 throw new System.InvalidOperationException( "Quote character and record separator cannot be the same." );
  132.             } else if ( EOF == file.Peek() ) {
  133.                 return null;
  134.             }
  135.  
  136.             var output = new System.Text.StringBuilder();
  137.             var leLen = recordSeparator.Length;
  138.             var lenStop = leLen - 1;
  139.             System.Int32 i = 0;
  140.             System.Char c;
  141.             System.Boolean isPlaintext = true;
  142.             System.Int32 p = file.Read();
  143.             System.Char q;
  144.             while ( EOL != p ) {
  145.                 c = System.Convert.ToChar( p );
  146.                 output = output.Append( c );
  147.                 if ( isPlaintext ) {
  148.                     if ( quoteChar.Equals( c ) ) {
  149.                         isPlaintext = false;
  150.                     } else if ( ( leLen <= output.Length ) && ( recordSeparator[ i ].Equals( output[ ( output.Length - leLen ) - i ] ) ) ) {
  151.                         if ( lenStop <= ++i ) {
  152.                             output.Remove( output.Length - leLen, leLen );
  153.                             break;
  154.                         }
  155.                     } else {
  156.                         i = 0;
  157.                     }
  158.                 } else {
  159.                     if ( quoteChar.Equals( c ) ) {
  160.                         p = file.Peek();
  161.                         if ( EOL == p ) {
  162.                             throw new System.IO.EndOfStreamException();
  163.                         }
  164.                         c = System.Convert.ToChar( p );
  165.                         if ( quoteChar.Equals( c ) ) {
  166.                             file.Read();
  167.                             output = output.Append( c );
  168.                         } else {
  169.                             i = 0;
  170.                             isPlaintext = true;
  171.                         }
  172.                     }
  173.                 }
  174.                 p = file.Read();
  175.             }
  176.  
  177.             return output.ToString();
  178.         }
  179.         public static System.String ReadLine( this System.IO.StreamReader file, System.String recordSeparator ) {
  180.             if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  181.                 throw new System.ArgumentNullException( "recordSeparator" );
  182.             } else if ( null == file ) {
  183.                 throw new System.ArgumentNullException( "file" );
  184.             } else if ( EOF == file.Peek() ) {
  185.                 return null;
  186.             }
  187.  
  188.             System.Boolean isReading = true;
  189.             var rs = recordSeparator.ToCharArray();
  190.             System.Int32 i = 0;
  191.             var maxI = rs.Length;
  192.             var line = new System.Text.StringBuilder();
  193.             System.Int32 j = 0;
  194.             System.Int32 c;
  195.             System.Boolean isNull = true;
  196.             do {
  197.                 c = file.Read();
  198.                 if ( -1 == c ) {
  199.                     maxI = 0;
  200.                     isReading = false;
  201.                     break;
  202.                 }
  203.                 isNull = false;
  204.                 line.Append( System.Convert.ToChar( c ) );
  205.                 if ( line[ j ].Equals( rs[ i ] ) ) {
  206.                     i++;
  207.                 } else {
  208.                     i = 0;
  209.                 }
  210.                 if ( i == maxI ) {
  211.                     isReading = false;
  212.                     break;
  213.                 }
  214.                 j++;
  215.             } while ( isReading );
  216.             line.Remove( line.Length - maxI, maxI );
  217.             return isNull ? null : line.ToString();
  218.         }
  219.  
  220.  
  221.         public static System.Collections.Generic.IEnumerable<System.String> ReadRecord(
  222.             this System.IO.TextReader file, System.String recordSeparator,
  223.             System.Char quoteChar, System.Char fieldSeparator
  224.         ) {
  225.             if ( null == file ) {
  226.                 throw new System.ArgumentNullException( "file" );
  227.             } else if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  228.                 throw new System.InvalidOperationException();
  229.             }
  230.  
  231.             var line = file.ReadLine( recordSeparator, quoteChar );
  232.             if ( null == line ) {
  233.                 yield break;
  234.             }
  235.             using ( var reader = new System.IO.StringReader( line ) ) {
  236.                 System.Int32 i;
  237.                 System.Char c;
  238.                 System.String column;
  239.                 var reading = true;
  240.                 System.Nullable<System.Char> ec = null;
  241.                 var qc = quoteChar;
  242.                 do {
  243.                     i = reader.Peek();
  244.                     if ( -1 == i ) {
  245.                         reading = false;
  246.                         break;
  247.                     }
  248.                     c = System.Convert.ToChar( i );
  249.                     if ( qc.Equals( c ) ) {
  250.                         reader.Read();
  251.                         column = ReadColumn( reader, quoteChar, true );
  252.                         yield return column;
  253.                     } else {
  254.                         column = ReadColumn( reader, fieldSeparator, false );
  255.                         yield return column;
  256.                     }
  257.                 } while ( reading );
  258.             }
  259.         }
  260.         public static System.Collections.Generic.IEnumerable<System.String> ReadRecord(
  261.             this System.IO.StreamReader file, System.String recordSeparator,
  262.             System.Char quoteChar, System.Char fieldSeparator
  263.         ) {
  264.             if ( null == file ) {
  265.                 throw new System.ArgumentNullException( "file" );
  266.             } else if ( file.EndOfStream ) {
  267.                 yield break;
  268.             } else if ( System.String.IsNullOrEmpty( recordSeparator ) ) {
  269.                 throw new System.InvalidOperationException();
  270.             }
  271.  
  272.             var line = file.ReadLine( recordSeparator, quoteChar );
  273.             if ( null == line ) {
  274.                 yield break;
  275.             }
  276.             using ( var reader = new System.IO.StringReader( line ) ) {
  277.                 System.Int32 i;
  278.                 System.Char c;
  279.                 System.String column;
  280.                 var reading = true;
  281.                 System.Nullable<System.Char> ec = null;
  282.                 var qc = quoteChar;
  283.                 do {
  284.                     i = reader.Peek();
  285.                     if ( -1 == i ) {
  286.                         reading = false;
  287.                         break;
  288.                     }
  289.                     c = System.Convert.ToChar( i );
  290.                     if ( qc.Equals( c ) ) {
  291.                         reader.Read();
  292.                         column = ReadColumn( reader, quoteChar, true );
  293.                         yield return column;
  294.                     } else {
  295.                         column = ReadColumn( reader, fieldSeparator, false );
  296.                         yield return column;
  297.                     }
  298.                 } while ( reading );
  299.             }
  300.         }
  301.         public static System.String ReadColumn(
  302.             this System.IO.StringReader reader, System.Char @break, System.Boolean readNextOnBreak
  303.         ) {
  304.             if ( null == reader ) {
  305.                 throw new System.ArgumentNullException( "reader" );
  306.             }
  307.             var sb = new System.Text.StringBuilder( 128 );
  308.             System.Nullable<System.Char> ch;
  309.             var reading = true;
  310.             do {
  311.                 ch = ReadChar( reader, @break, readNextOnBreak );
  312.                 if ( ch.HasValue ) {
  313.                     sb = sb.Append( ch.Value );
  314.                 } else {
  315.                     reading = false;
  316.                     break;
  317.                 }
  318.             } while ( reading );
  319.             return sb.ToString().TrimToNull();
  320.         }
  321.         public static System.Nullable<System.Char> ReadChar(
  322.             this System.IO.StringReader reader, System.Char @break, System.Boolean readNextOnBreak
  323.         ) {
  324.             if ( null == reader ) {
  325.                 throw new System.ArgumentNullException( "reader" );
  326.             }
  327.  
  328.             var p = reader.Peek();
  329.             if ( -1 == p ) {
  330.                 return null;
  331.             }
  332.             var c = System.Convert.ToChar( reader.Read() );
  333.             if ( @break.Equals( c ) ) {
  334.                 if ( readNextOnBreak ) {
  335.                     p = reader.Peek();
  336.                     if ( -1 == p ) {
  337.                         return null;
  338.                     } else if ( @break.Equals( System.Convert.ToChar( p ) ) ) {
  339.                         return System.Convert.ToChar( reader.Read() );
  340.                     } else {
  341.                         reader.Read();
  342.                     }
  343.                 }
  344.                 return null;
  345.             }
  346.             return c;
  347.         }
  348.         #endregion methods
  349.  
  350.     }
  351.  
  352. }
Add Comment
Please, Sign In to add comment