Example #1
0
        /// <summary>
        /// write the content to a file.
        /// deal with conditional defines etc.
        /// </summary>
        /// <param name="AXAMLFilename"></param>
        /// <param name="ADestFileExtension"></param>
        /// <param name="ACheckTemplateCompletion"></param>
        /// <returns></returns>
        public Boolean FinishWriting(string AXAMLFilename, string ADestFileExtension, Boolean ACheckTemplateCompletion)
        {
//            TLogging.Log("1");
            ReplaceCodelets();
//            TLogging.Log("2");
            ProcessIFDEFs(ref FTemplateCode);
            ProcessIFNDEFs(ref FTemplateCode);
            BeautifyCode(ref FTemplateCode);

            AXAMLFilename = AXAMLFilename.Replace('\\', Path.DirectorySeparatorChar);
            AXAMLFilename = AXAMLFilename.Replace('/', Path.DirectorySeparatorChar);

            FDestinationFile = System.IO.Path.GetDirectoryName(AXAMLFilename) +
                               System.IO.Path.DirectorySeparatorChar +
                               System.IO.Path.GetFileNameWithoutExtension(AXAMLFilename) +
                               ADestFileExtension;

            if (!ACheckTemplateCompletion || CheckTemplateCompletion(FTemplateCode))
            {
                Console.WriteLine("Writing " + Path.GetFileName(FDestinationFile));

                // just one line break at the end
                string code = FTemplateCode.ToString().TrimEnd(new char[] { ' ', '\t', '\r', '\n' });

                StreamWriter sw = new StreamWriter(FDestinationFile + ".new", false, System.Text.Encoding.UTF8);
                sw.Write(code);
                sw.Close();

                TTextFile.UpdateFile(FDestinationFile, true);

                return(true);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="AFilename"></param>
        public TYml2Xml(string AFilename)
        {
            filename = AFilename;

            // file should be in unicode encoding
            // StreamReader DetectEncodingFromByteOrderMarks does not work for ANSI?
            TextReader reader = new StreamReader(filename, TTextFile.GetFileEncoding(filename), false);

            lines = reader.ReadToEnd().Replace("\r", "").Split(new char[] { '\n' });
            reader.Close();
            currentLine = -1;
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="AfileName"></param>
        /// <returns></returns>
        public Boolean OpenCsvFile(String AfileName)
        {
            // We don't need to read the whole file for this because we will only display the first 5 lines
            bool hasBOM, isAmbiguousUTF;

            if (TTextFile.AutoDetectTextEncodingAndOpenFile(AfileName, out FFileContent, out FCurrentEncoding,
                                                            out hasBOM, out isAmbiguousUTF, out FRawBytes) == false)
            {
                return(false);
            }

            TTextFileEncoding.SetComboBoxProperties(cmbTextEncoding, hasBOM, isAmbiguousUTF, FCurrentEncoding);
            DisplayGrid();
            return(true);
        }
Example #4
0
        /// <summary>
        /// convert a CSV file to an XmlDocument.
        /// the first line is expected to contain the column names/captions, in quotes.
        /// from the header line, the separator can be determined, if the parameter ASeparator is empty
        /// </summary>
        /// <param name="ACSVFilename">The filename</param>
        /// <param name="AFallbackEncoding">The file encoding will be automatically determined, but if a fallback is specified that does not match
        /// the encoding that was determined, it will be used.  Usually this paramter can be null.</param>
        /// <param name="ASeparator">A column separator</param>
        public static XmlDocument ParseCSVFile2Xml(string ACSVFilename, string ASeparator = null, Encoding AFallbackEncoding = null)
        {
            string   fileContent;
            Encoding fileEncoding;
            bool     hasBOM, isAmbiguous;

            byte[] rawBytes;

            if (TTextFile.AutoDetectTextEncodingAndOpenFile(ACSVFilename, out fileContent, out fileEncoding,
                                                            out hasBOM, out isAmbiguous, out rawBytes))
            {
                if ((AFallbackEncoding != null) && !fileEncoding.Equals(AFallbackEncoding))
                {
                    fileContent = AFallbackEncoding.GetString(rawBytes);
                }

                return(ParseCSVContent2Xml(fileContent, ASeparator));
            }

            // Either we could not open the file or it is empty
            return(ParseCSV2Xml(new List <string>(), ASeparator));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="AfileName"></param>
        /// <returns></returns>
        public Boolean OpenCsvFile(String AfileName)
        {
            System.Text.Encoding FileEncoding = TTextFile.GetFileEncoding(AfileName);

            //
            // If it failed to open the file, GetFileEncoding returned null.
            if (FileEncoding == null)
            {
                return(false);
            }

            StreamReader reader = new StreamReader(AfileName, FileEncoding, false);

            FCSVRows = new List <string>();

            while (!reader.EndOfStream && FCSVRows.Count < 6)
            {
                FCSVRows.Add(reader.ReadLine());
            }

            reader.Close();
            RbtCheckedChanged(null, null);
            return(true);
        }
Example #6
0
        /// <summary>
        /// convert a CSV file to an XmlDocument.
        /// the first line is expected to contain the column names/captions, in quotes.
        /// from the header line, the separator can be determined, if the parameter ASeparator is empty
        /// </summary>
        public static XmlDocument ParseCSV2Xml(string ACSVFilename, string ASeparator = null, Encoding AEncoding = null)
        {
            StreamReader sr = new StreamReader(ACSVFilename, TTextFile.GetFileEncoding(ACSVFilename, AEncoding), false);

            List <string> Lines = new List <string>();

            try
            {
                while (!sr.EndOfStream)
                {
                    Lines.Add(sr.ReadLine());
                }

                return(ParseCSV2Xml(Lines, ASeparator));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                sr.Close();
            }
        }