Exemplo n.º 1
0
/*
 * internal bool WriteToTextFile()
 *  {
 *  try
 *  {
 *  MForm.ShowStatus( "Saving: " + FileName );
 *
 *  Encoding Encode = Encoding.UTF8;
 *  if( FileName.ToLower().EndsWith( ".bat" ) ||
 *      FileName.ToLower().EndsWith( ".java" ))
 *    {
 *    MForm.ShowStatus( "Using Ascii encoding." );
 *    Encode = Encoding.ASCII;
 *    }
 *
 *  using( StreamWriter SWriter = new StreamWriter( FileName, false, Encode ))
 *    {
 *    string[] Lines = MainTextBox.Lines;
 *
 *    foreach( string Line in Lines )
 *      {
 *      // SWriter.WriteLine( Line.TrimEnd() + "\r\n" );
 *      SWriter.WriteLine( Line.TrimEnd() );
 *      }
 *
 *    // SWriter.WriteLine( " " );
 *    }
 *
 *  return true;
 *  }
 *  catch( Exception Except )
 *    {
 *    MForm.ShowStatus( "Could not write to the file:" );
 *    MForm.ShowStatus( FileName );
 *    MForm.ShowStatus( Except.Message );
 *    return false;
 *    }
 *  }
 */



        internal bool WriteToTextFile()
        {
            try
            {
                if (FileName.ToLower() == "noname.txt")
                {
                    return(true);
                }

                MForm.ShowStatus("Saving: " + FileName);

                // if( FileName.ToLower().EndsWith( ".bat" ) ||
                //     FileName.ToLower().EndsWith( ".java" ))

                string[]      Lines        = MainTextBox.Lines;
                StringBuilder FileSBuilder = new StringBuilder();

                foreach (string Line in Lines)
                {
                    FileSBuilder.Append(Line.TrimEnd() + "\n");
                }

                byte[] FileBytes = UTF8Strings.StringToBytes(FileSBuilder.ToString());
                if (FileBytes == null)
                {
                    MForm.ShowStatus("FileBytes was null for:");
                    MForm.ShowStatus(FileName);
                    return(false);
                }

                File.WriteAllBytes(FileName, FileBytes);
                return(true);
            }
            catch (Exception Except)
            {
                MForm.ShowStatus("Could not write to the file:");
                MForm.ShowStatus(FileName);
                MForm.ShowStatus(Except.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
/*
 * private bool ReadFromTextFile( string FileName, bool AsciiOnly )
 *  {
 *  try
 *  {
 *  if( !File.Exists( FileName ))
 *    {
 *    // Might be adding a new file that doesn't
 *    // exist yet.
 *    MainTextBox.Text = "";
 *    return false;
 *    }
 *
 *  StringBuilder SBuilder = new StringBuilder();
 *  using( StreamReader SReader = new StreamReader( FileName, Encoding.UTF8 ))
 *    {
 *    while( SReader.Peek() >= 0 )
 *      {
 *      string Line = SReader.ReadLine();
 *      if( Line == null )
 *        continue;
 *
 *      Line = Line.Replace( "\t", "  " );
 *      Line = StringsEC.GetCleanUnicodeString( Line, 4000, false );
 *      Line = Line.TrimEnd(); // TrimStart()
 *
 *      // if( Line == "" )
 *        // continue;
 *
 *      SBuilder.Append( Line + "\r\n" );
 *      }
 *    }
 *
 *  MainTextBox.Text = SBuilder.ToString().TrimEnd();
 *  return true;
 *  }
 *  catch( Exception Except )
 *    {
 *    MForm.ShowStatus( "Could not read the file: \r\n" + FileName );
 *    MForm.ShowStatus( Except.Message );
 *    return false;
 *    }
 *  }
 */



        private bool ReadFromTextFile(string FileName, bool AsciiOnly)
        {
            try
            {
                if (FileName.ToLower() == "noname.txt")
                {
                    MainTextBox.Text = "";
                    return(true);
                }


                if (!File.Exists(FileName))
                {
                    // Might be adding a new file that doesn't
                    // exist yet.
                    MainTextBox.Text = "";
                    return(false);
                }

                // This opens the file, reads or writes all the
                // bytes, then closes the file.
                byte[] FileBytes = File.ReadAllBytes(FileName);
                if (FileBytes == null)
                {
                    MainTextBox.Text = "FileBytes was null.";
                    return(false);
                }

                string FileS = UTF8Strings.BytesToString(FileBytes, 1000000000);

                StringBuilder SBuilder     = new StringBuilder();
                StringBuilder FileSBuilder = new StringBuilder();

                int Last = FileS.Length;
                for (int Count = 0; Count < Last; Count++)
                {
                    char OneChar = FileS[Count];
                    if (OneChar == '\r')
                    {
                        continue; // Ignore it.
                    }
                    if (OneChar == '\n')
                    {
                        string Line = SBuilder.ToString();
                        SBuilder.Clear();
                        Line = Line.Replace("\t", "  ");
                        Line = StringsEC.GetCleanUnicodeString(Line, 4000, false);
                        Line = Line.TrimEnd();

                        // if( Line == "" )
                        // continue;

                        FileSBuilder.Append(Line + "\r\n");
                        continue;
                    }

                    SBuilder.Append(OneChar);
                }

                MainTextBox.Text = FileSBuilder.ToString().TrimEnd() + "\r\n";
                return(true);
            }
            catch (Exception Except)
            {
                MForm.ShowStatus("Could not read the file: \r\n" + FileName);
                MForm.ShowStatus(Except.Message);
                return(false);
            }
        }