Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Utilities.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8  the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and
11  the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13  the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 #pragma once
26 
27 #include <string>
28 #include <vector>
29 #include "cinder/Cinder.h"
30 #include "cinder/Url.h"
31 #include "cinder/DataSource.h"
32 #undef check
33 #include <boost/lexical_cast.hpp>
34 
35 namespace cinder {
36 
38 fs::path expandPath( const fs::path &path );
40 fs::path getHomeDirectory();
42 fs::path getDocumentsDirectory();
44 fs::path getTemporaryDirectory();
46 fs::path getTemporaryFilePath( const std::string &prefix = "" );
54 bool createDirectories( const fs::path &path, bool createParents = true );
55 
57 void launchWebBrowser( const Url &url );
58 
60 void deleteFile( const fs::path &path );
61 
63 std::vector<std::string> split( const std::string &str, char separator, bool compress = true );
65 std::vector<std::string> split( const std::string &str, const std::string &separators, bool compress = true );
66 
69 
71 void sleep( float milliseconds );
72 
74 #if (defined( CINDER_MSW ) || defined( CINDER_WINRT ))
75 inline char getPathSeparator() { return '\\'; }
76 #else
77 inline char getPathSeparator() { return '/'; }
78 #endif
79 
80 template<typename T>
81 inline std::string toString( const T &t ) { return boost::lexical_cast<std::string>( t ); }
82 template<typename T>
83 inline T fromString( const std::string &s ) { return boost::lexical_cast<T>( s ); }
84 // This specialization seems to only be necessary with more recent versions of Boost
85 template<>
86 inline Url fromString( const std::string &s ) { return Url( s ); }
87 #if defined(CINDER_COCOA_TOUCH)
88 // Necessary because boost::lexical_cast crashes when trying to convert a string to a double on iOS
89 template<>
90 inline double fromString( const std::string &s ) { return atof( s.c_str() ); }
91 #endif
92 
94 std::vector<std::string> stackTrace();
95 
96 // ENDIANNESS
97 inline int8_t swapEndian( int8_t val ) { return val; }
98 inline uint8_t swapEndian( uint8_t val ) { return val; }
99 extern int16_t swapEndian( int16_t val );
100 extern uint16_t swapEndian( uint16_t val );
101 extern int32_t swapEndian( int32_t val );
102 extern uint32_t swapEndian( uint32_t val );
103 extern float swapEndian( float val );
104 extern double swapEndian( double val );
105 
106 extern void swapEndianBlock( uint16_t *blockPtr, size_t blockSizeInBytes );
107 extern void swapEndianBlock( float *blockPtr, size_t blockSizeInBytes );
108 
109 } // namespace cinder
fs::path getTemporaryDirectory()
Returns a path to the user's temporary directory.
Definition: Utilities.cpp:123
std::string getPathFileName(const std::string &path)
Returns the file name portion of file path path. For example "C:\Images\Beyonce.jpg" returns "Beyonce...
Definition: Utilities.cpp:183
int8_t swapEndian(int8_t val)
Definition: Utilities.h:97
GLsizei const GLchar ** string
Definition: GLee.h:2427
fs::path getTemporaryFilePath(const std::string &prefix="")
Returns a path that is gauranteed to be unique and is suitable for creating a temporary file...
Definition: Utilities.cpp:146
std::vector< std::string > split(const std::string &str, char separator, bool compress=true)
Returns a vector of substrings split by the separator separator. split( "one two three", ' ' ) -> [ "one", "two", "three" ] If compress is TRUE, it will consider consecutive separators as one.
Definition: Utilities.cpp:261
void deleteFile(const fs::path &path)
Delete the file at path. Fails quietly if the path does not exist.
Definition: Utilities.cpp:248
fs::path expandPath(const fs::path &path)
Returns a canonical version of path by expanding a "~" and symlinks on the Mac "..", "." and "//".
Definition: Utilities.cpp:70
void sleep(float milliseconds)
Suspends the execution of the current thread until milliseconds have passed. Supports sub-millisecond...
Definition: Utilities.cpp:286
std::string toString(const T &t)
Definition: Utilities.h:81
fs::path getHomeDirectory()
Returns a path to the user's home directory.
Definition: Utilities.cpp:86
std::string loadString(DataSourceRef dataSource)
Loads the contents of dataSource and returns it as a std::string.
Definition: Utilities.cpp:276
char getPathSeparator()
Returns the path separator for the host operating system's file system, '\' on Windows and '/' on Mac...
Definition: Utilities.h:77
GLuint GLfloat * val
Definition: GLee.h:14636
std::vector< std::string > stackTrace()
Returns a stack trace (aka backtrace) where stackTrace()[0] == caller, stackTrace()[1] == caller's pa...
Definition: Utilities.cpp:340
void swapEndianBlock(uint16_t *blockPtr, size_t blockSizeInBytes)
Definition: Utilities.cpp:429
void launchWebBrowser(const Url &url)
Launches a path in a web browser.
Definition: Utilities.cpp:222
std::string getPathDirectory(const std::string &path)
Returns the directory portion of file path path, the last component of which must be a file name or a...
Definition: Utilities.cpp:173
GLdouble GLdouble t
Definition: GLee.h:1426
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
GLdouble s
Definition: GLee.h:1378
T fromString(const std::string &s)
Definition: Utilities.h:83
bool createDirectories(const fs::path &path, bool createParents=true)
Creates a directory at path and optionally creates any missing parent directories when createParents ...
Definition: Utilities.cpp:205
fs::path getDocumentsDirectory()
Returns a path to the user's documents directory.
Definition: Utilities.cpp:106
std::string getPathExtension(const std::string &path)
Returns the file extension of the file located at path.
Definition: Utilities.cpp:192
Definition: Url.h:31