BluetoothFactoryConfig.cs :  » Business-Application » 32feet.NET » InTheHand » Net » Bluetooth » 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 » Business Application » 32feet.NET 
32feet.NET » InTheHand » Net » Bluetooth » BluetoothFactoryConfig.cs
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.Factory.BluetoothFactoryConfg
// 
// Copyright (c) 2003-2010 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt

using System;
using System.IO;
using System.Xml;

namespace InTheHand.Net.Bluetooth{
#if !V1
    static
#endif
 class BluetoothFactoryConfig
    {
        internal static readonly Type MsftFactoryType = typeof(InTheHand.Net.Bluetooth.SocketsBluetoothFactory);
#if ! V1
        internal static readonly Type WidcommFactoryType = typeof(InTheHand.Net.Bluetooth.Widcomm.WidcommBluetoothFactory);
#endif

        internal static readonly string[] s_knownStacks = {
            MsftFactoryType.FullName,
#if ! V1
            WidcommFactoryType.FullName,
#endif
#if ! V1
            //--typeof(InTheHand.Net.Bluetooth.NullBluetoothFactory).FullName,
#endif
        };


        internal const bool Default_oneStackOnly = true;
        internal const bool Default_reportAllErrors = false;

#if NETCF
        internal static bool s_loaded;
        internal static bool s_oneStackOnly = Default_oneStackOnly;
        internal static bool s_reportAllErrors = Default_reportAllErrors;
#endif

        //----
        internal static string[] KnownStacks
        {
            get
            {
#if !NETCF
                return BluetoothFactorySection.Instance.StackList2;
#else
                LoadManuallyOnce();
                return s_knownStacks;
#endif
            }
        }

        internal static bool ReportAllErrors
        {
            get
            {
#if !NETCF
                return BluetoothFactorySection.Instance.ReportAllErrors;
#else
                LoadManuallyOnce();
                return s_reportAllErrors;
#endif
            }
        }

        internal static bool OneStackOnly
        {
            get
            {
#if !NETCF
                return BluetoothFactorySection.Instance.OneStackOnly;
#else
                LoadManuallyOnce();
                return s_oneStackOnly;
#endif
            }
        }

        internal static bool WidcommICheckIgnorePlatform
        {
            get
            {
#if !NETCF
                return BluetoothFactorySection.Instance.WidcommICheckIgnorePlatform;
#else
                throw new NotSupportedException();
#endif
            }
        }

        //--------
#if NETCF

        // Returns the full path to the running executable on CE (Equivalent to Assembly.GetEntryAssembly() on desktop).
        // Overcomes issue if 32feet .dll is in a different folder to the application (e.g. GAC).
        private static string GetEntryAssemblyPath()
        {
            System.Text.StringBuilder buffer = new System.Text.StringBuilder(NativeMethods.MAX_PATH);

            int chars = NativeMethods.GetModuleFileName(IntPtr.Zero, buffer, NativeMethods.MAX_PATH);

            System.Diagnostics.Debug.WriteLine(buffer.ToString());

            return buffer.ToString();
        }


        static void LoadManuallyOnce()
        {
            if (s_loaded) {
                return;
            }
            s_loaded = true;
            Values vs = new Values();
            bool success = false;

            string exePath = GetEntryAssemblyPath();
            string path = exePath + ".config"; // e.g. "\Program Files\MyApp\MyApp.exe.config"
            bool pathExists = File.Exists(path);

            if (pathExists)
            {

                using (TextReader rdr = File.OpenText(path))
                {
                    success = LoadManually(rdr, vs);
                }

                System.Diagnostics.Debug.WriteLine(string.Format("Successfully read from {0} = {1}", path, success));
            }

            if (!success | !pathExists)
            {
                //use legacy path
                path = Path.Combine(Path.GetDirectoryName(exePath), "32feet.config");

                if (File.Exists(path))
                {
                    using (TextReader rdr = File.OpenText(path))
                    {
                        success = LoadManually(rdr, vs);
                    }

                    System.Diagnostics.Debug.WriteLine(string.Format("Successfully read from {0} = {1}", path, success));
                }
            }

            //if one of the reads was successful set the values retrieved
            if (success)
            {
                if (vs.oneStackOnly != null)
                    s_oneStackOnly = (bool)vs.oneStackOnly;
                if (vs.reportAllErrors != null)
                    s_reportAllErrors = (bool)vs.reportAllErrors;
            }//if
        }
#endif

#if true || WinCE
        internal class Values
        {
            public bool? oneStackOnly;
            public bool? reportAllErrors;
        }

        static readonly string[] ElementNames = { "configuration", "InTheHand.Net.Personal", "BluetoothFactory" };

        internal static bool LoadManually(TextReader src, Values v)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(src);
            return LoadManually(xd, v);
        }

        static bool LoadManually(XmlDocument xd, Values v)
        {
            XmlNode cur = xd;
            for (int depth = 0; depth < ElementNames.Length; ++depth) {
                XmlNode prev = cur;
                cur = cur[ElementNames[depth]];
                if (cur == null) {
                    return false;
                }
            }
            //--
            XmlElement elem = (XmlElement)cur;
            bool found = GetBoolOptionalAttribute(elem, "oneStackOnly", ref v.oneStackOnly);
            found |= GetBoolOptionalAttribute(elem, "reportAllErrors", ref v.reportAllErrors);

            //return true if one or both settings were found
            return found;
        }

        static bool GetBoolOptionalAttribute(XmlElement elem, string name, ref bool? var)
        {
            string str = elem.GetAttribute(name);
            if (!StringUtilities.IsNullOrEmpty(str)) {
                var = XmlConvert.ToBoolean(str);
                return true;
            }
            //attribute was not found
            return false;
        }
#endif

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