chore: kdis basic, extras and network
[KDIS.git] / KDIS / Extras / KUtils.h
bloba734f6e6b8b412926f03c61e53f1528ebd7ffbfd
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 /********************************************************************
31 KUtils
32 created: 23/9/2008
33 author: Karl Jones
35 purpose: Utility Features
36 *********************************************************************/
38 #ifndef _USE_MATH_DEFINES
39 #define _USE_MATH_DEFINES
40 #endif
41 #include <cmath>
43 #include "./../KDefines.h"
44 #include "./KConversions.h"
45 #include "./../KEncodersDecoders.h"
47 #pragma once
49 namespace KDIS {
50 namespace UTILS {
52 /************************************************************************/
53 /* Add a tab to all new lines in a string, used for formatting PDU data */
54 /************************************************************************/
56 static inline void AddTabsToStringStream( KStringStream & Stream, KUINT16 Tabs, KCHAR8 Tab )
58 for( KUINT16 i = 0; i < Tabs; ++i )
60 Stream << Tab;
64 //////////////////////////////////////////////////////////////////////////
66 static inline KString IndentString( const KString & S, KUINT16 Tabs = 1, KCHAR8 Tab = '\t' )
68 KStringStream NewS;
70 // Add tab to first item and then check all new lines
71 AddTabsToStringStream( NewS, Tabs, Tab );
73 for( KUINT16 i = 0; i < S.size(); ++i )
75 if( S.c_str()[i] == '\n' && i != ( S.size() - 1 ) )
77 NewS << S.c_str()[i];
78 AddTabsToStringStream( NewS, Tabs, Tab );
80 else
82 NewS << S.c_str()[i];
86 return NewS.str();
89 /************************************************************************/
90 /* Determine the endian of the machine at run time */
91 /************************************************************************/
93 static inline KBOOL IsMachineBigEndian()
95 static KUINT16 ui = 0x01;
97 static NetToKUINT16 conv( ui, false );
99 if( conv.m_Octs[0] == 0x01 )
101 return false;
103 else
105 return true;
109 } // END namespace UTILS
110 } // END namespace KDIS