StringConverter.cs :  » Game » RealmForge » Axiom » Core » 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 » Core » StringConverter.cs
using System;
using System.Globalization;
using System.Text;
using Axiom.MathLib;

namespace Axiom.Core{
    /// <summary>
    ///     Helper class for going back and forth between strings and various types.
    /// </summary>
    public sealed class StringConverter {
        #region Fields

        /// <summary>
        ///    Culture info to use for parsing numeric data.
        /// </summary>
        private static CultureInfo englishCulture = new CultureInfo("en-US");

        #endregion Fields

        #region Constructor

        /// <summary>
        ///     Private constructor so no instances can be created.
        /// </summary>
        private StringConverter() {
        }

        #endregion Constructor

        #region Static Methods

        /// <summary>
        ///    Parses a boolean type value 
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool ParseBool(string val) {
            switch (val) {
                case "true":
                case "on":
                    return true;
                case "false":
                case "off":
                    return false;
            }

            // make the compiler happy
            return false;
        }

        /// <summary>
        ///    Parses an array of params and returns a color from it.
        /// </summary>
        /// <param name="val"></param>
        public static ColorEx ParseColor(string[] values) {
            ColorEx color = new ColorEx();
            color.r = ParseFloat(values[0]);
            color.g = ParseFloat(values[1]);
            color.b = ParseFloat(values[2]);
            color.a = (values.Length > 3) ? ParseFloat(values[3]) : 1.0f;

            return color;
        }

        /// <summary>
        ///    Parses an array of params and returns a color from it.
        /// </summary>
        /// <param name="val"></param>
        public static ColorEx ParseColor(string val) {
            ColorEx color = new ColorEx();
            string[] vals = val.Split(' ');

            color.r = ParseFloat(vals[0]);
            color.g = ParseFloat(vals[1]);
            color.b = ParseFloat(vals[2]);
            color.a = (vals.Length == 4) ? ParseFloat(vals[3]) : 1.0f;

            return color;
        }

        /// <summary>
        ///    Parses an array of params and returns a color from it.
        /// </summary>
        /// <param name="val"></param>
        public static Vector3 ParseVector3(string[] values) {
            Vector3 vec = new Vector3();
            vec.x = ParseFloat(values[0]);
            vec.y = ParseFloat(values[1]);
            vec.z = ParseFloat(values[2]);

            return vec;
        }

        /// <summary>
        ///    Parses an array of params and returns a color from it.
        /// </summary>
        /// <param name="val"></param>
        public static Vector3 ParseVector3(string val) {
            string[] values = val.Split(' ');

            Vector3 vec = new Vector3();
            vec.x = ParseFloat(values[0]);
            vec.y = ParseFloat(values[1]);
            vec.z = ParseFloat(values[2]);

            return vec;
        }

        /// <summary>
        ///    Parses an array of params and returns a color from it.
        /// </summary>
        /// <param name="val"></param>
        public static Vector4 ParseVector4(string[] values) {
            Vector4 vec = new Vector4();
            vec.x = ParseFloat(values[0]);
            vec.y = ParseFloat(values[1]);
            vec.z = ParseFloat(values[2]);
            vec.w = ParseFloat(values[3]);

            return vec;
        }

        /// <summary>
        ///    Parse a float value from a string.
        /// </summary>
        /// <remarks>
        ///    Since our file formats assume the 'en-US' style format for numbers, we need to
        ///    let the framework know that where numbers are being parsed.
        /// </remarks>
        /// <param name="val">String value holding the float.</param>
        /// <returns>A float representation of the string value.</returns>
        public static float ParseFloat(string val) {
            return float.Parse(val, englishCulture);
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string ToString(ColorEx color) {
            return string.Format(englishCulture, "{0} {1} {2} {3}", color.r, color.g, color.b, color.a);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vec"></param>
        /// <returns></returns>
        public static string ToString(Vector4 vec) {
            return string.Format(englishCulture, "{0} {1} {2} {3}", vec.x, vec.y, vec.z, vec.w);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="vec"></param>
        /// <returns></returns>
        public static string ToString(Vector3 vec) {
            return string.Format(englishCulture, "{0} {1} {2}", vec.x, vec.y, vec.z);
        }

        /// <summary>
        ///     Converts a 
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string ToString(float val) {
            return val.ToString(englishCulture);
        }

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