/* create a list suitable for GCompletion from path */ GList * gcmp_list_from_path(struct mpdclient *c, const gchar *path, GList *list, gint types) { struct mpd_connection *connection; struct mpd_entity *entity; connection = mpdclient_get_connection(c); if (connection == NULL) return list; mpd_send_list_meta(connection, path); while ((entity = mpd_recv_entity(connection)) != NULL) { char *name; if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY && types & GCMP_TYPE_DIR) { const struct mpd_directory *dir = mpd_entity_get_directory(entity); gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir)); gsize size = strlen(tmp)+2; name = g_malloc(size); g_strlcpy(name, tmp, size); g_strlcat(name, "/", size); g_free(tmp); } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG && types & GCMP_TYPE_FILE) { const struct mpd_song *song = mpd_entity_get_song(entity); name = utf8_to_locale(mpd_song_get_uri(song)); } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST && types & GCMP_TYPE_PLAYLIST) { const struct mpd_playlist *playlist = mpd_entity_get_playlist(entity); name = utf8_to_locale(mpd_playlist_get_path(playlist)); } else { mpd_entity_free(entity); continue; } list = g_list_append(list, name); mpd_entity_free(entity); } return list; }
static char * song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag) { const char *value = mpd_song_get_tag(song, tag, 0); char *result; #ifndef NCMPC_MINI char *all; #endif /* !NCMPC_MINI */ if (value == NULL) return NULL; #ifndef NCMPC_MINI all = song_more_tag_values(song, tag, value); if (all != NULL) value = all; #endif /* !NCMPC_MINI */ result = utf8_to_locale(value); #ifndef NCMPC_MINI g_free(all); #endif /* !NCMPC_MINI */ return result; }
/** * This function is called by the gidle.c library when MPD sends us an * idle event (or when the connection dies). */ static void idle_callback(enum mpd_error error, enum mpd_server_error server_error, const char *message, enum mpd_idle events, void *ctx) { struct mpdclient *c = ctx; c->idle = false; assert(mpdclient_is_connected(c)); if (error != MPD_ERROR_SUCCESS) { char *allocated; if (error == MPD_ERROR_SERVER && server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) { /* the "idle" command is not supported - fall back to timer based polling */ mpd_glib_free(c->source); c->source = NULL; auto_update_timer(); return; } if (error == MPD_ERROR_SERVER) message = allocated = utf8_to_locale(message); else allocated = NULL; screen_status_message(message); g_free(allocated); screen_bell(); doupdate(); mpdclient_disconnect(c); screen_update(mpd); reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL); return; } c->events |= events; mpdclient_update(c); #ifndef NCMPC_MINI if (options.enable_xterm_title) update_xterm_title(); #endif screen_update(mpd); c->events = 0; mpdclient_put_connection(c); check_reconnect(); auto_update_timer(); }
char * replace_utf8_to_locale(char *src) { #ifdef ENABLE_LOCALE assert(src != NULL); if (noconvert) return src; return utf8_to_locale(src); #else return src; #endif }
std::string load_string<char>(const binary& data) { //std::string result; bool bom; const char* charset = test_charset(data, bom); unsigned int cp = charset_to_codepage(charset); if (cp == 65001 && bom) { // utf-8 return utf8_to_locale(std::string(data.begin() + 3, data.end())); } else if ((cp == 1200 || cp == 1201) && bom) { // utf-16 return wstring_to_locale( charset_to_wstring(std::string(data.begin() + 2, data.end()), charset)); } else return wstring_to_locale( charset_to_wstring(std::string(data.begin(), data.end()), charset)); }
static char *get_output_str(char *str, Bool is_utf8) { char *out; if (!str) { return NULL; } if (envir_utf8) { if (is_utf8) { out = strdup(str); } else { out=locale_to_utf8(str); if (!out) { out = strdup(str); } } } else { if (is_utf8) { out = utf8_to_locale(str); if (!out) { out = strdup(str); } } else { out = strdup(str); } } return out; }
/*---------------------------------------------------------------------- Print a single character, translate from UTF-8 to user's locale charset. Args: c -- char to print Returns: 1 on success, 0 on ps_global->print->err ----*/ int print_char(int c) { #ifndef _WINDOWS int i, outchars; unsigned char obuf[MAX(MB_LEN_MAX,32)]; if(!ps_global->print->err && (outchars = utf8_to_locale(c, &cb, obuf, sizeof(obuf)))){ for(i = 0; i < outchars && !ps_global->print->err; i++) if(putc(obuf[i], ps_global->print->fp) == EOF) ps_global->print->err = 1; } #else /* _WINDOWS */ if(!ps_global->print->err && (ps_global->print->err = mswin_print_char_utf8(c))) q_status_message1(SM_ORDER, 0, 9, "Print cancelled: %s", mswin_print_error((unsigned short)ps_global->print->err)); #endif /* _WINDOWS */ return(!ps_global->print->err); }
static gsize _strfsong(gchar *s, gsize max, const gchar *format, const struct mpd_song *song, const gchar **last) { const gchar *p, *end; gchar *temp; gsize n, length = 0; gboolean found = FALSE; memset(s, 0, max); if (song == NULL) return 0; for (p = format; *p != '\0' && length<max;) { /* OR */ if (p[0] == '|') { ++p; if(!found) { memset(s, 0, max); length = 0; } else { p = skip(p); } continue; } /* AND */ if (p[0] == '&') { ++p; if(!found) { p = skip(p); } else { found = FALSE; } continue; } /* EXPRESSION START */ if (p[0] == '[') { temp = g_malloc0(max); if( _strfsong(temp, max, p+1, song, &p) >0 ) { g_strlcat(s, temp, max); length = strlen(s); found = TRUE; } g_free(temp); continue; } /* EXPRESSION END */ if (p[0] == ']') { if(last) *last = p+1; if(!found && length) { memset(s, 0, max); length = 0; } return length; } /* pass-through non-escaped portions of the format string */ if (p[0] != '#' && p[0] != '%' && length<max) { s[length++] = *p; p++; continue; } /* let the escape character escape itself */ if (p[0] == '#' && p[1] != '\0' && length<max) { s[length++] = *(p+1); p+=2; continue; } /* advance past the esc character */ /* find the extent of this format specifier (stop at \0, ' ', or esc) */ temp = NULL; end = p+1; while(*end >= 'a' && *end <= 'z') { end++; } n = end - p + 1; if(*end != '%') n--; else if (strncmp("%file%", p, n) == 0) temp = utf8_to_locale(mpd_song_get_uri(song)); else if (strncmp("%artist%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_ARTIST); else if (strncmp("%title%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_TITLE); else if (strncmp("%album%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_ALBUM); else if (strncmp("%shortalbum%", p, n) == 0) { temp = song_tag_locale(song, MPD_TAG_ALBUM); if (temp) { gchar *temp2 = g_strndup(temp, 25); if (strlen(temp) > 25) { temp2[24] = '.'; temp2[23] = '.'; temp2[22] = '.'; } g_free(temp); temp = temp2; } } else if (strncmp("%track%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_TRACK); else if (strncmp("%name%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_NAME); else if (strncmp("%date%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_DATE); else if (strncmp("%genre%", p, n) == 0) temp = song_tag_locale(song, MPD_TAG_GENRE); else if (strncmp("%shortfile%", p, n) == 0) { const char *uri = mpd_song_get_uri(song); if (strstr(uri, "://") != NULL) temp = utf8_to_locale(uri); else temp = utf8_to_locale(g_basename(uri)); } else if (strncmp("%time%", p, n) == 0) { unsigned duration = mpd_song_get_duration(song); if (duration > 0) { char buffer[32]; format_duration_short(buffer, sizeof(buffer), duration); temp = g_strdup(buffer); } } if( temp == NULL) { gsize templen=n; /* just pass-through any unknown specifiers (including esc) */ /* drop a null char in so printf stops at the end of this specifier, but put the real character back in (pseudo-const) */ if( length+templen > max ) templen = max-length; g_strlcat(s, p,max); length+=templen; } else { gsize templen = strlen(temp); found = TRUE; if( length+templen > max ) templen = max-length; g_strlcat(s, temp, max); length+=templen; g_free(temp); } /* advance past the specifier */ p += n; } if(last) *last = p; return length; }