1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
26 void ToLower(std::string
&s
)
28 transform(s
.begin(), s
.end(), s
.begin(), tolower
);
31 int StrToInt(const std::string
&str
)
33 return atoi(str
.c_str());
36 long StrToLong(const std::string
&str
)
38 return atol(str
.c_str());
41 std::string
IntoStr(int l
)
43 std::ostringstream ss
;
48 std::string
IntoStr(mpd_tag_type tag
) // this is only for left column's title in media library
64 case MPD_TAG_COMPOSER
:
66 case MPD_TAG_PERFORMER
:
77 std::string
IntoStr(NCurses::Color color
)
81 if (color
== NCurses::clDefault
)
83 else if (color
== NCurses::clBlack
)
85 else if (color
== NCurses::clRed
)
87 else if (color
== NCurses::clGreen
)
89 else if (color
== NCurses::clYellow
)
91 else if (color
== NCurses::clBlue
)
93 else if (color
== NCurses::clMagenta
)
95 else if (color
== NCurses::clCyan
)
97 else if (color
== NCurses::clWhite
)
103 NCurses::Color
IntoColor(const std::string
&color
)
105 NCurses::Color result
= NCurses::clDefault
;
107 if (color
== "black")
108 result
= NCurses::clBlack
;
109 else if (color
== "red")
110 result
= NCurses::clRed
;
111 else if (color
== "green")
112 result
= NCurses::clGreen
;
113 else if (color
== "yellow")
114 result
= NCurses::clYellow
;
115 else if (color
== "blue")
116 result
= NCurses::clBlue
;
117 else if (color
== "magenta")
118 result
= NCurses::clMagenta
;
119 else if (color
== "cyan")
120 result
= NCurses::clCyan
;
121 else if (color
== "white")
122 result
= NCurses::clWhite
;
127 mpd_tag_type
IntoTagItem(char c
)
132 return MPD_TAG_ARTIST
;
136 return MPD_TAG_GENRE
;
138 return MPD_TAG_COMPOSER
;
140 return MPD_TAG_PERFORMER
;
142 return MPD_TAG_ARTIST
;
147 MPD::Song::SetFunction
IntoSetFunction(mpd_tag_type tag
)
152 return &MPD::Song::SetArtist
;
154 return &MPD::Song::SetAlbum
;
156 return &MPD::Song::SetTitle
;
158 return &MPD::Song::SetTrack
;
160 return &MPD::Song::SetGenre
;
162 return &MPD::Song::SetDate
;
163 case MPD_TAG_COMPOSER
:
164 return &MPD::Song::SetComposer
;
165 case MPD_TAG_PERFORMER
:
166 return &MPD::Song::SetPerformer
;
167 case MPD_TAG_COMMENT
:
168 return &MPD::Song::SetComment
;
170 return &MPD::Song::SetDisc
;
175 #endif // HAVE_TAGLIB_H
177 std::string
Shorten(const std::basic_string
<my_char_t
> &s
, size_t max_length
)
179 if (s
.length() <= max_length
)
183 std::basic_string
<my_char_t
> result(s
, 0, max_length
/2-1);
185 result
+= s
.substr(s
.length()-max_length
/2+1);
186 return TO_STRING(result
);
189 void EscapeUnallowedChars(std::string
&s
)
191 static const std::string unallowed_chars
= "\"*/:<>?\\|";
193 for (std::string::const_iterator it
= unallowed_chars
.begin(); it
!= unallowed_chars
.end(); ++it
)
195 for (size_t i
= 0; i
< s
.length(); ++i
)
199 s
.erase(s
.begin()+i
);
206 void EscapeHtml(std::string
&s
)
209 for (size_t i
= s
.find("<"); i
!= std::string::npos
; i
= s
.find("<"))
211 size_t j
= s
.find(">")+1;
212 s
.replace(i
, j
-i
, "");
214 Replace(s
, "'", "'");
215 Replace(s
, """, "\"");
216 Replace(s
, "&", "&");
217 for (size_t i
= 0; i
< s
.length(); ++i
)
221 s
.erase(s
.begin()+i
);
224 if (s
[i
] == 13) // ascii code for windows line ending, get rid of this shit
229 else if (s
[i
] == '\t')
234 void Trim(std::string
&s
)
240 size_t e
= s
.length()-1;
242 while (s
[b
] == ' ' || s
[b
] == '\n')
244 while (s
[e
] == ' ' || s
[e
] == '\n')
248 if (b
!= 0 || e
!= s
.length()-1)
249 s
= s
.substr(b
, e
-b
);