char* ANSItoUTF8(const char* buffer, DWORD length) { wchar_t* wchar = CharToWideChar(buffer, length); if (wchar == NULL) return NULL; int len = WideCharToMultiByte(CP_UTF8, 0, wchar, -1, NULL, 0, NULL, NULL); if (len > 0) { char* ch = new char[len]; ZeroMemory(ch, len); int error = WideCharToMultiByte(CP_UTF8, 0, wchar, -1, ch, len, NULL, NULL); SAFE_DELETE_ARRAY(wchar); if (error == len) return ch; else { SAFE_DELETE_ARRAY(ch); return NULL; } } SAFE_DELETE_ARRAY(wchar); return NULL; }
bool CNameTextMgr::_CheckNameInvalid(const char* szString) { if(!szString) return false; #define CREATE_NAME_LEN 18 //取名长度限制为12个英文字符或6个中文字符,utf8编码中文字符占3字节 int len = strlen(szString); if (len <= 0 || len > CREATE_NAME_LEN) return false; /*if (!IsTextUTF8(szString, len)) { Log.Debug("Create Name:%s is not utf8", szString); return false; }*/ int i = 0; wchar_t cText[CREATE_NAME_LEN+1] = { 0 }; CharToWideChar(szString, cText, CREATE_NAME_LEN + 1); //sprintf(cText, "%s", szString); while (cText[i] != '\0') { //if (!isalpha(cText[i]) && !isdigit(cText[i]) && !IsUtf8ChsChar(cText[i], cText[++i], cText[++i])) if (!IsNumWChar(cText[i]) && !IsEngWChar(cText[i]) && !IsChsWChar(cText[i])) return false; ++i; } return true; }
wchar_t* GetFileName(const char* filename) { const char* p = strrchr(filename, '.'); const char* q = strrchr(filename, '\\'); if (p - 1 >= q + 1) return CharToWideChar(q + 1, p - q - 1); else return NULL; }
wchar_t* GetMP3Text(id3_field_textencoding encoding, const id3_field* field, const id3_ucs4_t* ucs4) { if (ucs4 == NULL) return NULL; wchar_t* content = NULL; switch(encoding) { case ID3_FIELD_TEXTENCODING_ISO_8859_1: { char* p = (char*)id3_ucs4_latin1duplicate(ucs4); content = CharToWideChar(p, strlen(p)); free(p); } break; case ID3_FIELD_TEXTENCODING_UTF_8: { char* p = (char*)id3_ucs4_utf8duplicate(ucs4); if (p != NULL) { content = UTF8toWideChar(p, strlen(p)); free(p); } } break; case ID3_FIELD_TEXTENCODING_UTF_16: { char* p = (char*)id3_ucs4_utf8duplicate(ucs4); if (p != NULL) { content = UTF8toWideChar(p, strlen(p)); free(p); } } break; default: break; } return content; }
void DecodeTag(PLAYERINFO* playerinfo) { const PLAYERDECODE* decode = &playerinfo->decode; AVFormatContext* format = decode->format; AVCodecContext* context = decode->context; PLAYERTAG* playertag = &playerinfo->tag; if (playertag->read) return; if (format != NULL && context != NULL) { const AVCodecDescriptor* descriptor = context->codec_descriptor; const char* name = descriptor->name; if (name != NULL) { CharToWideChar(name, 3, playertag->ext, 3); _wcsupr_s(playertag->ext, sizeof(playertag->ext) / sizeof(wchar_t)); } if (StrCmp(playertag->ext, L"MP3") == 0) { const char* filename = playerinfo->filename; id3_file* file = id3_file_open(filename, ID3_FILE_MODE_READONLY); id3_tag* tag = id3_file_tag(file); if (tag != NULL && tag->nframes > 0) { wchar_t* title = GetMP3Tag(tag, ID3_FRAME_TITLE); if (title == NULL) title = GetMP3Tag(tag, ID3_FRAME_ALBUM); wchar_t* artist = GetMP3Tag(tag, ID3_FRAME_ARTIST); playertag->artist = artist; playertag->title = title; } SAFE_DELETE_ARRAY(file->path); id3_file_close(file); } else { AVDictionary* dictionary = format->metadata; if (dictionary != NULL) { AVDictionaryEntry* entry = av_dict_get(dictionary, "title", NULL, 0); if (entry != NULL && entry->value != NULL) { wchar_t* title = UTF8toWideChar(entry->value, strlen(entry->value)); playertag->title = title; } else { entry = av_dict_get(dictionary, "album", NULL, 0); if (entry != NULL && entry->value != NULL) { wchar_t* title = UTF8toWideChar(entry->value, strlen(entry->value)); playertag->title = title; } } entry = av_dict_get(dictionary, "artist", NULL, 0); if (entry != NULL && entry->value != NULL) { wchar_t* artist = UTF8toWideChar(entry->value, strlen(entry->value)); playertag->artist = artist; } } } if (StrCmp(playertag->ext, L"PCM") == 0) StrCpy(playertag->ext, L"WAV"); if (playertag->title == NULL) { SAFE_DELETE_ARRAY(playertag->artist); SplitTag(playerinfo->filename, &playertag->title, &playertag->artist); if (playertag->title == NULL) { wchar_t* title = GetFileName(playerinfo->filename); if (title != NULL) { SplitTitle(title, &playertag->title); if (playertag->title == NULL) playertag->title = title; else SAFE_DELETE_ARRAY(title); } } if (playertag->title != NULL) { wchar_t* title = playertag->title; wchar_t* first = NULL; wchar_t* second = NULL; Split(title, L".", &first, &second); if (first != NULL && second != NULL) { if (IsNumber(first)) { SAFE_DELETE_ARRAY(title); playertag->title = second; } else { SAFE_DELETE_ARRAY(second); } SAFE_DELETE_ARRAY(first); } } } long duration = format->duration / 1000; playerinfo->duration = duration; duration = duration / 1000; long minute = duration / 60; long second = duration % 60; wsprintf(playertag->timer, L"%d:%02d", minute, second); wsprintf(playertag->sample, L"%dHz", context->sample_rate); if (context->bit_rate > 0) wsprintf(playertag->bitrate, L"%dkbps", context->bit_rate / 1000); else wsprintf(playertag->bitrate, L"%dkbps", format->bit_rate / 1000); switch (context->channels) { case 1: StrCpy(playertag->channels, L"单声道"); break; case 4: StrCpy(playertag->channels, L"四声道"); break; case 5: StrCpy(playertag->channels, L"4.1声道"); break; case 6: StrCpy(playertag->channels, L"5.1声道"); break; case 7: StrCpy(playertag->channels, L"6.1声道"); break; case 8: StrCpy(playertag->channels, L"7.1声道"); break; default: StrCpy(playertag->channels, L"立体声"); break; } } playertag->read = TRUE; }