root/src/N0SlibUtils.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
////////////////////////////////////////////////////////////////////////
// Copyright (c) Nehmulos 2011-2014
// This file is part of N0 Strain Serialization Library.
//
// N0Strain-Serialization-Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// N0Strain-Serialization-Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with N0Strain-Serialization-Library.  If not, see https://gnu.org/licenses/lgpl-3.0
////////////////////////////////////////////////////////////////////////




#ifndef N0Slib_UTILS_H_
#define N0Slib_UTILS_H_

#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <streambuf>
#include "PlatformDetection.h"

#ifdef N0ware_System_Unix
        #include <unistd.h>
        #include "sys/stat.h"
        #include "pwd.h"
#elif defined N0ware_System_Windows
        #include "windows.h"
#endif



/// Contains helpful functions to interact with the filesystem.
/// And converting binary data into human readable data.
namespace nw {
namespace utils {

typedef std::string String;

/// Returns true if a file exists on the filesystem.
extern bool fileExists(const char *filename);
/// Creates an empty file if it doesn't already exists
extern bool touchFile(const char* filename);
/// Creates a new Directory. Returns false if it failed. Permissions aren't working yet.
extern bool createDirectory(const char* filename, unsigned int permissions = 0x0755);
/// Returns a path to the system's homedirectory does not append a slash example: /home/user
extern String getHomeDirectory();
/// Converts a binary blob into Hex String.
extern String toHex(const char* blob, unsigned int size);
/// A shortcut to call toHex(value.data(), value.size())
extern String toHex(String value);
/// Converts a Hex String to it's binary value
extern String fromHex(String hexString);
/// Converts a binary blob into a Base64 String.
extern String toBase64(const char* blob, unsigned int size);
/// Shortcut that calls toBase64 with blob parameters; returns a Base64 String.
extern String toBase64(String value);
/// decodes a Base64 String and creates a binary blob, set's the size reference to it's size.
extern String fromBase64(String encodedBlob);

/// give unicode 32 bit value, receive a series of UTF8 bytes
extern String utf8FromIntCode(unsigned int code);

int lengthOfCharOrString(const char* str);
int lengthOfCharOrString(const char);
int lengthOfCharOrString(const String& str);

template <class nowType, class thenType>
extern void replaceAll(String& str, const nowType& before, const thenType& after, bool searchReplacedString = false) {
        const int lengthToReplace = lengthOfCharOrString(before);
        const int lengthOfReplacement = searchReplacedString ? 0 : lengthOfCharOrString(after);
        size_t foundPosition = str.find(before, 0);
        while (foundPosition != std::string::npos) {
                str.replace(foundPosition, lengthToReplace, after);
                foundPosition = str.find(before, foundPosition + lengthOfReplacement);
        }
}

}  // namespace utils
}  // namespace nw

#endif /* N0Slib_UTILS_H_ */