ParseHelper.cs :  » Game » RealmForge » Axiom » Scripting » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » Scripting » ParseHelper.cs
using System;
using System.Globalization;
using System.IO;
using System.Text;
using Axiom.Core;
using Axiom.MathLib;

namespace Axiom.Scripting{
    /// <summary>
    ///   Class contining helper methods for parsing text files.
    /// </summary>
    public class ParseHelper {
        #region Methods
    
        /// <summary>
        ///    Helper method for taking a string array and returning a single concatenated
        ///    string composed of the range of specified elements.
        /// </summary>
        /// <param name="items"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public static string Combine(string[] items, int start, int end) {
            StringBuilder sb = new StringBuilder();

            for(int i = start; i < end; i++) {
                sb.AppendFormat("{0} ", items[i]);
            }

            return sb.ToString(0, sb.Length - 1);
        }

        /// <summary>
        ///    Helper method to log a formatted error when encountering problems with parsing
        ///    an attribute.
        /// </summary>
        /// <param name="attribute"></param>
        /// <param name="context"></param>
        /// <param name="expectedParams"></param>
        public static void LogParserError(string attribute, string context, string reason) {
            string error = string.Format("Bad {0} attribute in block '{1}'. Reason: {2}", attribute, context, reason);

            LogManager.Instance.Write(error);
        }

        /// <summary>
        ///    Helper method to nip/tuck the string before parsing it.  This includes trimming spaces from the beginning
        ///    and end of the string, as well as removing excess spaces in between values.
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static string ReadLine(TextReader reader) {
            string line = reader.ReadLine();

            if(line != null) {
                line = line.Replace("\t", " ");
                line = line.Trim();

                // ignore blank lines, lines without spaces, or comments
                if(line.Length == 0 || line.IndexOf(' ') == -1 || line.StartsWith("//")) {
                    return line;
                }

                StringBuilder sb = new StringBuilder();

                string[] values = line.Split(' ');

                // reduce big space gaps between values down to a single space
                for(int i = 0; i < values.Length; i++) {
                    string val = values[i];

                    if(val.Length != 0) {
                        sb.Append(val + " ");
                    }
                }
        
                line = sb.ToString();
                line = line.TrimEnd();
            } // if
      
            return line;
        }

        /// <summary>
        ///    Helper method to remove the first item from a string array and return a new array 1 element smaller
        ///    starting at the second element of the original array.  This helpe to seperate the params from the command
        ///    in the various script files.
        /// </summary>
        /// <param name="splitLine"></param>
        /// <returns></returns>
        public static string[] GetParams(string[] all) {
            // create a seperate parm list that has the command removed
            string[] parms = new string[all.Length - 1];
            Array.Copy(all, 1, parms, 0, parms.Length);

            return parms;
        }

        /// <summary>
        ///    Advances in the stream until it hits the next {.
        /// </summary>
        public static void SkipToNextOpenBrace(TextReader reader) {
            string line = "";
            while(line != null && line != "{") {
                line = ReadLine(reader);
            }
        }

        /// <summary>
        ///    Advances in the stream until it hits the next }.
        /// </summary>
        /// <param name="reader"></param>
        public static void SkipToNextCloseBrace(TextReader reader) {
            string line = "";
            while(line != null && line != "}") {
                line = ReadLine(reader);
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.