Debian Codegraph @

src/utils.cpp

raw
1 /*
2 * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSE.md for more information.
6 */
7
8 #ifdef TARGET_WINDOWS
9 #pragma warning(disable:4244) //wchar to char = loss of data
10 #endif
11
12 #include "utils.h"
13 #include "settings.h"
14 #include <string>
15 #include <stdio.h>
16 #include <kodi/tools/StringUtils.h>
17
18 using namespace kodi::tools;
19 using namespace std;
20
21 void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ")
22 {
23 // Skip delimiters at beginning.
24 //string::size_type lastPos = str.find_first_not_of(delimiters, 0);
25 // Don't skip delimiters at beginning.
26 string::size_type start_pos = 0;
27 // Find first "non-delimiter".
28 string::size_type delim_pos = 0;
29
30 while (string::npos != delim_pos)
31 {
32 delim_pos = str.find_first_of(delimiters, start_pos);
33 // Found a token, add it to the vector.
34 tokens.push_back(str.substr(start_pos, delim_pos - start_pos));
35 start_pos = delim_pos + 1;
36
37 // Find next "non-delimiter"
38 }
39 }
40
41
42 std::string WStringToString(const std::wstring& s)
43 {
44 std::string temp(s.length(), ' ');
45 std::copy(s.begin(), s.end(), temp.begin());
46 return temp;
47 }
48
49 std::wstring StringToWString(const std::string& s)
50 {
51 std::wstring temp(s.length(),L' ');
52 std::copy(s.begin(), s.end(), temp.begin());
53 return temp;
54 }
55
56 std::string lowercase(const std::string& s)
57 {
58 std::string t;
59 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
60 {
61 t += tolower(*i);
62 }
63 return t;
64 }
65
66 bool stringtobool(const std::string& s)
67 {
68 std::string temp = lowercase(s);
69
70 if(temp.compare("false") == 0)
71 return false;
72 else if(temp.compare("0") == 0)
73 return false;
74 else
75 return true;
76 }
77
78 const char* booltostring(const bool b)
79 {
80 return (b==true) ? "True" : "False";
81 }
82
83 std::string ToThumbFileName(const char* strChannelName)
84 {
85 std::string strThumbName = strChannelName;
86
87 StringUtils::Replace(strThumbName, ":","_");
88 StringUtils::Replace(strThumbName, "/","_");
89 StringUtils::Replace(strThumbName, "\\","_");
90 StringUtils::Replace(strThumbName, ">","_");
91 StringUtils::Replace(strThumbName, "<","_");
92 StringUtils::Replace(strThumbName, "*","_");
93 StringUtils::Replace(strThumbName, "?","_");
94 StringUtils::Replace(strThumbName, "\"","_");
95 StringUtils::Replace(strThumbName, "|","_");
96
97 return strThumbName;
98 }
99
100 std::string ToKodiPath(const std::string& strFileName)
101 {
102 std::string strKodiFileName(strFileName);
103
104 if (StringUtils::Left(strKodiFileName, 2) == "\\\\")
105 {
106 std::string SMBPrefix = "smb://";
107
108 if (!CSettings::Get().GetSMBusername().empty())
109 {
110 SMBPrefix += CSettings::Get().GetSMBusername();
111 if (!CSettings::Get().GetSMBpassword().empty())
112 {
113 SMBPrefix += ":" + CSettings::Get().GetSMBpassword();
114 }
115 SMBPrefix += "@";
116 }
117 StringUtils::Replace(strKodiFileName, "\\\\", SMBPrefix.c_str());
118 StringUtils::Replace(strKodiFileName, '\\', '/');
119 }
120
121 return strKodiFileName;
122 }
123
124 std::string ToWindowsPath(const std::string& strFileName)
125 {
126 std::string strWinFileName;
127 std::size_t found = strFileName.find_first_of('@');
128
129 if (found != std::string::npos)
130 {
131 strWinFileName = "\\\\" + strFileName.substr(found+1);
132 }
133 else
134 {
135 strWinFileName = strFileName;
136 StringUtils::Replace(strWinFileName, "smb://","\\\\");
137 }
138
139 StringUtils::Replace(strWinFileName, '/','\\');
140
141 return strWinFileName;
142 }
143
144 //////////////////////////////////////////////////////////////////////////////