put local functions into unnamed namespaces

This commit is contained in:
unK
2008-09-27 20:54:32 +02:00
parent d61cb54653
commit 9b65b39126
7 changed files with 229 additions and 222 deletions

View File

@@ -23,62 +23,65 @@
extern ncmpcpp_keys Key; extern ncmpcpp_keys Key;
string DisplayKeys(int *key, int size = 2) namespace
{ {
bool backspace = 1; string DisplayKeys(int *key, int size = 2)
string result = "\t";
for (int i = 0; i < size; i++)
{ {
if (key[i] == null_key); bool backspace = 1;
else if (key[i] == 259) string result = "\t";
result += "Up"; for (int i = 0; i < size; i++)
else if (key[i] == 258)
result += "Down";
else if (key[i] == 339)
result += "Page Up";
else if (key[i] == 338)
result += "Page Down";
else if (key[i] == 262)
result += "Home";
else if (key[i] == 360)
result += "End";
else if (key[i] == 32)
result += "Space";
else if (key[i] == 10)
result += "Enter";
else if (key[i] == 330)
result += "Delete";
else if (key[i] == 261)
result += "Right";
else if (key[i] == 260)
result += "Left";
else if (key[i] == 9)
result += "Tab";
else if (key[i] >= 1 && key[i] <= 26)
{ {
result += "Ctrl-"; if (key[i] == null_key);
result += key[i]+64; else if (key[i] == 259)
result += "Up";
else if (key[i] == 258)
result += "Down";
else if (key[i] == 339)
result += "Page Up";
else if (key[i] == 338)
result += "Page Down";
else if (key[i] == 262)
result += "Home";
else if (key[i] == 360)
result += "End";
else if (key[i] == 32)
result += "Space";
else if (key[i] == 10)
result += "Enter";
else if (key[i] == 330)
result += "Delete";
else if (key[i] == 261)
result += "Right";
else if (key[i] == 260)
result += "Left";
else if (key[i] == 9)
result += "Tab";
else if (key[i] >= 1 && key[i] <= 26)
{
result += "Ctrl-";
result += key[i]+64;
}
else if (key[i] >= 265 && key[i] <= 276)
{
result += "F";
result += key[i]-216;
}
else if ((key[i] == 263 || key[i] == 127) && !backspace);
else if ((key[i] == 263 || key[i] == 127) && backspace)
{
result += "Backspace";
backspace = 0;
}
else
result += key[i];
result += " ";
} }
else if (key[i] >= 265 && key[i] <= 276) if (result.length() > 12)
{ result = result.substr(0, 12);
result += "F"; for (int i = result.length(); i <= 12; result += " ", i++);
result += key[i]-216; result += ": ";
} return result;
else if ((key[i] == 263 || key[i] == 127) && !backspace);
else if ((key[i] == 263 || key[i] == 127) && backspace)
{
result += "Backspace";
backspace = 0;
}
else
result += key[i];
result += " ";
} }
if (result.length() > 12)
result = result.substr(0, 12);
for (int i = result.length(); i <= 12; result += " ", i++);
result += ": ";
return result;
} }
string GetKeybindings() string GetKeybindings()

View File

@@ -32,30 +32,33 @@ pthread_mutex_t curl = PTHREAD_MUTEX_INITIALIZER;
bool data_ready = 0; bool data_ready = 0;
#endif #endif
size_t write_data(char *buffer, size_t size, size_t nmemb, string data) namespace
{ {
int result = 0; size_t write_data(char *buffer, size_t size, size_t nmemb, string data)
if (buffer)
{ {
data += buffer; int result = 0;
result = size*nmemb; if (buffer)
{
data += buffer;
result = size*nmemb;
}
return result;
} }
return result;
}
void EscapeHtml(string &str) void EscapeHtml(string &str)
{
for (int i = str.find("<"); i != string::npos; i = str.find("<"))
{ {
int j = str.find(">")+1; for (int i = str.find("<"); i != string::npos; i = str.find("<"))
str.replace(i, j-i, ""); {
int j = str.find(">")+1;
str.replace(i, j-i, "");
}
for (int i = str.find("&#039;"); i != string::npos; i = str.find("&#039;"))
str.replace(i, 6, "'");
for (int i = str.find("&quot;"); i != string::npos; i = str.find("&quot;"))
str.replace(i, 6, "\"");
for (int i = str.find("&amp;"); i != string::npos; i = str.find("&amp;"))
str.replace(i, 5, "&");
} }
for (int i = str.find("&#039;"); i != string::npos; i = str.find("&#039;"))
str.replace(i, 6, "'");
for (int i = str.find("&quot;"); i != string::npos; i = str.find("&quot;"))
str.replace(i, 6, "\"");
for (int i = str.find("&amp;"); i != string::npos; i = str.find("&amp;"))
str.replace(i, 5, "&");
} }
#ifdef HAVE_CURL_CURL_H #ifdef HAVE_CURL_CURL_H

View File

@@ -30,7 +30,6 @@
void * GetArtistInfo(void *); void * GetArtistInfo(void *);
#endif #endif
void EscapeHtml(string &);
void * GetLyrics(void *); void * GetLyrics(void *);
#endif #endif

View File

@@ -197,29 +197,6 @@ void DefaultConfiguration(ncmpcpp_config &conf)
conf.message_delay_time = 4; conf.message_delay_time = 4;
} }
void GetKeys(string line, int *key)
{
int i = line.find("=")+1;
line = line.substr(i, line.length()-i);
i = 0;
if (line[i] == ' ')
while (line[++i] == ' ');
line = line.substr(i, line.length()-i);
i = line.find(" ");
string one;
string two;
if (i != string::npos)
{
one = line.substr(0, i);
i++;
two = line.substr(i, line.length()-i);
}
else
one = line;
key[0] = !one.empty() && one[0] == '\'' ? one[1] : (atoi(one.c_str()) == 0 ? null_key : atoi(one.c_str()));
key[1] = !two.empty() && two[0] == '\'' ? two[1] : (atoi(two.c_str()) == 0 ? null_key : atoi(two.c_str()));
}
string GetLineValue(const string &line, char a, char b) string GetLineValue(const string &line, char a, char b)
{ {
int i = 0; int i = 0;
@@ -240,54 +217,80 @@ string GetLineValue(const string &line, char a, char b)
return ""; return "";
} }
string IntoStr(Color color) namespace
{ {
string result; void GetKeys(string line, int *key)
{
int i = line.find("=")+1;
line = line.substr(i, line.length()-i);
i = 0;
if (line[i] == ' ')
while (line[++i] == ' ');
line = line.substr(i, line.length()-i);
i = line.find(" ");
string one;
string two;
if (i != string::npos)
{
one = line.substr(0, i);
i++;
two = line.substr(i, line.length()-i);
}
else
one = line;
key[0] = !one.empty() && one[0] == '\'' ? one[1] : (atoi(one.c_str()) == 0 ? null_key : atoi(one.c_str()));
key[1] = !two.empty() && two[0] == '\'' ? two[1] : (atoi(two.c_str()) == 0 ? null_key : atoi(two.c_str()));
}
if (color == clDefault) string IntoStr(Color color)
result = "default"; {
else if (color == clBlack) string result;
result = "black";
else if (color == clRed) if (color == clDefault)
result = "red"; result = "default";
else if (color == clGreen) else if (color == clBlack)
result = "green"; result = "black";
else if (color == clYellow) else if (color == clRed)
result = "yellow"; result = "red";
else if (color == clBlue) else if (color == clGreen)
result = "blue"; result = "green";
else if (color == clMagenta) else if (color == clYellow)
result = "magenta"; result = "yellow";
else if (color == clCyan) else if (color == clBlue)
result = "cyan"; result = "blue";
else if (color == clWhite) else if (color == clMagenta)
result = "white"; result = "magenta";
else if (color == clCyan)
return result; result = "cyan";
} else if (color == clWhite)
result = "white";
return result;
}
Color IntoColor(const string &color) Color IntoColor(const string &color)
{ {
Color result = clDefault; Color result = clDefault;
if (color == "black") if (color == "black")
result = clBlack; result = clBlack;
else if (color == "red") else if (color == "red")
result = clRed; result = clRed;
else if (color == "green") else if (color == "green")
result = clGreen; result = clGreen;
else if (color == "yellow") else if (color == "yellow")
result = clYellow; result = clYellow;
else if (color == "blue") else if (color == "blue")
result = clBlue; result = clBlue;
else if (color == "magenta") else if (color == "magenta")
result = clMagenta; result = clMagenta;
else if (color == "cyan") else if (color == "cyan")
result = clCyan; result = clCyan;
else if (color == "white") else if (color == "white")
result = clWhite; result = clWhite;
return result; return result;
}
} }
void ReadKeys(ncmpcpp_keys &keys) void ReadKeys(ncmpcpp_keys &keys)

View File

@@ -147,12 +147,10 @@ struct ncmpcpp_config
void DefaultKeys(ncmpcpp_keys &); void DefaultKeys(ncmpcpp_keys &);
void DefaultConfiguration(ncmpcpp_config &); void DefaultConfiguration(ncmpcpp_config &);
void GetKeys(string, int *);
string GetLineValue(const string &, char = '"', char = '"');
string IntoStr(Color);
Color IntoColor(const string &);
void ReadKeys(ncmpcpp_keys &); void ReadKeys(ncmpcpp_keys &);
void ReadConfiguration(ncmpcpp_config &); void ReadConfiguration(ncmpcpp_config &);
string GetLineValue(const string &, char = '"', char = '"');
#endif #endif

View File

@@ -189,98 +189,101 @@ bool WriteTags(Song &s)
return false; return false;
} }
SongSetFunction IntoSetFunction(char c) namespace
{ {
switch (c) SongSetFunction IntoSetFunction(char c)
{ {
case 'a': switch (c)
return &Song::SetArtist; {
case 't': case 'a':
return &Song::SetTitle; return &Song::SetArtist;
case 'b': case 't':
return &Song::SetAlbum; return &Song::SetTitle;
case 'y': case 'b':
return &Song::SetYear; return &Song::SetAlbum;
case 'n': case 'y':
return &Song::SetTrack; return &Song::SetYear;
case 'g': case 'n':
return &Song::SetGenre; return &Song::SetTrack;
case 'c': case 'g':
return &Song::SetComposer; return &Song::SetGenre;
case 'p': case 'c':
return &Song::SetPerformer; return &Song::SetComposer;
case 'd': case 'p':
return &Song::SetDisc; return &Song::SetPerformer;
case 'C': case 'd':
return &Song::SetComment; return &Song::SetDisc;
default: case 'C':
return NULL; return &Song::SetComment;
default:
return NULL;
}
} }
}
string GenerateFilename(const Song &s, string &pattern) string GenerateFilename(const Song &s, string &pattern)
{
const string unallowed_chars = "\"*/:<>?\\|";
string result = Window::OmitBBCodes(DisplaySong(s, &pattern));
for (string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); it++)
{ {
for (int i = 0; i < result.length(); i++) const string unallowed_chars = "\"*/:<>?\\|";
string result = Window::OmitBBCodes(DisplaySong(s, &pattern));
for (string::const_iterator it = unallowed_chars.begin(); it != unallowed_chars.end(); it++)
{ {
if (result[i] == *it) for (int i = 0; i < result.length(); i++)
result.erase(result.begin()+i); {
if (result[i] == *it)
result.erase(result.begin()+i);
}
} }
return result;
} }
return result;
}
string ParseFilename(Song &s, string mask, bool preview) string ParseFilename(Song &s, string mask, bool preview)
{
std::stringstream result;
vector<string> separators;
vector< std::pair<char, string> > tags;
string file = s.GetName().substr(0, s.GetName().find_last_of("."));
try
{ {
for (int i = mask.find("%"); i != string::npos; i = mask.find("%")) std::stringstream result;
{ vector<string> separators;
tags.push_back(make_pair(mask.at(i+1), "")); vector< std::pair<char, string> > tags;
mask = mask.substr(i+2); string file = s.GetName().substr(0, s.GetName().find_last_of("."));
i = mask.find("%");
if (!mask.empty())
separators.push_back(mask.substr(0, i));
}
int i = 0;
for (vector<string>::const_iterator it = separators.begin(); it != separators.end(); it++, i++)
{
int j = file.find(*it);
tags.at(i).second = file.substr(0, j);
file = file.substr(j+it->length());
}
if (!file.empty())
tags.at(i).second = file;
}
catch (std::out_of_range)
{
return "Error while parsing filename!";
}
for (vector< std::pair<char, string> >::iterator it = tags.begin(); it != tags.end(); it++)
{
for (string::iterator j = it->second.begin(); j != it->second.end(); j++)
if (*j == '_')
*j = ' ';
if (!preview) try
{ {
SongSetFunction set = IntoSetFunction(it->first); for (int i = mask.find("%"); i != string::npos; i = mask.find("%"))
if (set) {
(s.*set)(it->second); tags.push_back(make_pair(mask.at(i+1), ""));
mask = mask.substr(i+2);
i = mask.find("%");
if (!mask.empty())
separators.push_back(mask.substr(0, i));
}
int i = 0;
for (vector<string>::const_iterator it = separators.begin(); it != separators.end(); it++, i++)
{
int j = file.find(*it);
tags.at(i).second = file.substr(0, j);
file = file.substr(j+it->length());
}
if (!file.empty())
tags.at(i).second = file;
} }
else catch (std::out_of_range)
result << "%" << it->first << ": " << it->second << "\n"; {
return "Error while parsing filename!";
}
for (vector< std::pair<char, string> >::iterator it = tags.begin(); it != tags.end(); it++)
{
for (string::iterator j = it->second.begin(); j != it->second.end(); j++)
if (*j == '_')
*j = ' ';
if (!preview)
{
SongSetFunction set = IntoSetFunction(it->first);
if (set)
(s.*set)(it->second);
}
else
result << "%" << it->first << ": " << it->second << "\n";
}
return result.str();
} }
return result.str();
} }
void __deal_with_filenames(SongList &v) void __deal_with_filenames(SongList &v)

View File

@@ -41,8 +41,6 @@ string DisplayTag(const Song &, void *, const Menu<Song> *);
bool GetSongTags(Song &); bool GetSongTags(Song &);
bool WriteTags(Song &); bool WriteTags(Song &);
string GenerateFilename(const Song &, string &);
string ParseFilename(Song &, string, bool);
void __deal_with_filenames(SongList &); void __deal_with_filenames(SongList &);
#endif #endif