BOOL FileExists(wchar_t * pFilename) { if (0 == wcscmp(pFilename, L"-") || 0 == wcscmp(pFilename, L"/dev/stdin")) return TRUE; #ifdef _WIN32 BOOL bFound = FALSE; WIN32_FIND_DATA WFD; HANDLE hFind = FindFirstFile(pFilename, &WFD); if (hFind != INVALID_HANDLE_VALUE) { bFound = TRUE; CloseHandle(hFind); } return bFound; #else CSmartPtr<char> spANSI(GetANSIFromUTF16(pFilename), TRUE); struct stat b; if (stat(spANSI, &b) != 0) return FALSE; if (!S_ISREG(b.st_mode)) return FALSE; return TRUE; #endif }
int CWinFileIO::Open(const wchar_t * pName, BOOL bOpenReadOnly) { Close(); #ifdef _UNICODE CSmartPtr<wchar_t> spName((wchar_t *) pName, TRUE, FALSE); #else CSmartPtr<char> spName(GetANSIFromUTF16(pName), TRUE); #endif // open (read / write) if (bOpenReadOnly == FALSE) m_hFile = ::CreateFile(spName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { // open (read-only) m_hFile = ::CreateFile(spName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { return -1; } else { m_bReadOnly = TRUE; } } else { m_bReadOnly = FALSE; } wcscpy(m_cFileName, pName); return 0; }
int CWinFileIO::Delete() { Close(); #ifdef _UNICODE CSmartPtr<wchar_t> spName(m_cFileName, TRUE, FALSE); #else CSmartPtr<char> spName(GetANSIFromUTF16(m_cFileName), TRUE); #endif return DeleteFile(spName) ? 0 : -1; }
int CAPETagField::SaveField(char * pBuffer) { *((int *) pBuffer) = m_nFieldValueBytes; pBuffer += 4; *((int *) pBuffer) = m_nFieldFlags; pBuffer += 4; CSmartPtr<char> spFieldNameANSI((char *) GetANSIFromUTF16(m_spFieldNameUTF16), TRUE); strcpy(pBuffer, spFieldNameANSI); pBuffer += strlen(spFieldNameANSI) + 1; memcpy(pBuffer, m_spFieldValue, m_nFieldValueBytes); return GetFieldSize(); }
int CWinFileIO::Create(const wchar_t * pName) { Close(); #ifdef _UNICODE CSmartPtr<wchar_t> spName((wchar_t *) pName, TRUE, FALSE); #else CSmartPtr<char> spName(GetANSIFromUTF16(pName), TRUE); #endif m_hFile = CreateFile(spName, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (m_hFile == INVALID_HANDLE_VALUE) { return -1; } m_bReadOnly = FALSE; wcscpy(m_cFileName, pName); return 0; }
int CStdLibFileIO::Open(const wchar_t * pName) { Close(); m_bReadOnly = false; char * wpName = GetANSIFromUTF16(pName); if (0 == wcscmp(pName, L"-") || 0 == wcscmp(pName, L"/dev/stdin")) { m_pFile = SETBINARY_IN(stdin); m_bReadOnly = true; // ReadOnly } else if (0 == wcscmp (pName, L"/dev/stdout")) { m_pFile = SETBINARY_OUT(stdout); m_bReadOnly = false; // WriteOnly } else { m_pFile = fopen(wpName, "r+b"); if (!m_pFile) { /* * if we fail to open it with Read/Write permission, * we will try Read-Only. */ m_pFile = fopen(wpName, "rb"); // ReadOnly m_bReadOnly = true; } else m_bReadOnly = false; // Read/Write } if (!m_pFile) return -1; wcscpy(m_cFileName, pName); return 0; }
int CStdLibFileIO::Create(const wchar_t * pName) { Close(); if (0 == wcscmp (pName, L"-") || 0 == wcscmp (pName, L"/dev/stdout")) { m_pFile = SETBINARY_OUT(stdout); m_bReadOnly = false; // WriteOnly } else { char * wpName = GetANSIFromUTF16(pName); m_pFile = fopen (wpName, "wb+"); // Read/Write m_bReadOnly = false; } if (!m_pFile) return -1; wcscpy (m_cFileName, pName); return 0; }
int CStdLibFileIO::Delete() { Close(); return unlink (GetANSIFromUTF16(m_cFileName)); // 0 success, -1 error }
int main(int argc, char* argv[]) { /////////////////////////////////////////////////////////////////////////////// // error check the command line parameters /////////////////////////////////////////////////////////////////////////////// if (argc != 2) { printf("~~~Improper Usage~~~\r\n\r\n"); printf("Usage Example: Sample 1.exe 'c:\\1.ape'\r\n\r\n"); return 0; } /////////////////////////////////////////////////////////////////////////////// // variable declares /////////////////////////////////////////////////////////////////////////////// int nRetVal = 0; // generic holder for return values char cTempBuffer[256]; ZeroMemory(&cTempBuffer[0], 256); // generic buffer for string stuff char * pFilename = argv[1]; // the file to open IAPEDecompress * pAPEDecompress = NULL; // APE interface CSmartPtr<wchar_t> spInput; spInput.Assign(GetUTF16FromANSI(argv[1]), TRUE); //* /////////////////////////////////////////////////////////////////////////////// // open the file and error check /////////////////////////////////////////////////////////////////////////////// pAPEDecompress = CreateIAPEDecompress(spInput, &nRetVal); if (pAPEDecompress == NULL) { printf("Error opening APE file. (error code %d)\r\n\r\n", nRetVal); return 0; } /////////////////////////////////////////////////////////////////////////////// // display some information about the file /////////////////////////////////////////////////////////////////////////////// printf("Displaying information about '%s':\r\n\r\n", pFilename); // file format information printf("File Format:\r\n"); printf("\tVersion: %.2f\r\n", float(pAPEDecompress->GetInfo(APE_INFO_FILE_VERSION)) / float(1000)); switch (pAPEDecompress->GetInfo(APE_INFO_COMPRESSION_LEVEL)) { case COMPRESSION_LEVEL_FAST: printf("\tCompression level: Fast\r\n\r\n"); break; case COMPRESSION_LEVEL_NORMAL: printf("\tCompression level: Normal\r\n\r\n"); break; case COMPRESSION_LEVEL_HIGH: printf("\tCompression level: High\r\n\r\n"); break; case COMPRESSION_LEVEL_EXTRA_HIGH: printf("\tCompression level: Extra High\r\n\r\n"); break; } // audio format information printf("Audio Format:\r\n"); printf("\tSamples per second: %" PRIiPTR "\r\n", pAPEDecompress->GetInfo(APE_INFO_SAMPLE_RATE)); printf("\tBits per sample: %" PRIiPTR "\r\n", pAPEDecompress->GetInfo(APE_INFO_BITS_PER_SAMPLE)); printf("\tNumber of channels: %" PRIiPTR "\r\n", pAPEDecompress->GetInfo(APE_INFO_CHANNELS)); printf("\tPeak level: %" PRIiPTR "\r\n\r\n", pAPEDecompress->GetInfo(APE_INFO_PEAK_LEVEL)); // size and duration information printf("Size and Duration:\r\n"); printf("\tLength of file (s): %" PRIiPTR "\r\n", pAPEDecompress->GetInfo(APE_INFO_LENGTH_MS) / 1000); printf("\tFile Size (kb): %" PRIiPTR "\r\n\r\n", pAPEDecompress->GetInfo(APE_INFO_APE_TOTAL_BYTES) / 1024); // tag information printf("Tag Information:\r\n"); CAPETag * pAPETag = (CAPETag *) pAPEDecompress->GetInfo(APE_INFO_TAG); BOOL bHasID3Tag = pAPETag->GetHasID3Tag(); BOOL bHasAPETag = pAPETag->GetHasAPETag(); if (bHasID3Tag || bHasAPETag) { printf("\tID3 Tag: %s, APE Tag: %s", bHasID3Tag ? "Yes" : "No", bHasAPETag ? "" : "No"); if (bHasAPETag) { printf("%d", pAPETag->GetAPETagVersion() / 1000); } printf("\n\n"); // iterate through all the tag fields //BOOL bFirst = TRUE; CAPETagField * pTagField; // while (pAPETag->GetNextTagField(bFirst, &pTagField)) int index = 0; while ((pTagField = pAPETag->GetTagField(index)) != NULL) { //bFirst = FALSE; index ++; // output the tag field properties (don't output huge fields like images, etc.) if (pTagField->GetFieldValueSize() > 128) { printf("\t%s: --- too much data to display ---\r\n", GetANSIFromUTF16(pTagField->GetFieldName())); } else { /* const wchar_t *fieldName; char *name; wchar_t fieldValue[255]; char *value; fieldName = pTagField->GetFieldName(); name = GetANSIFromUTF16(fieldName); memset(fieldValue, 0, 255 * sizeof(wchar_t)); int len; pAPETag->GetFieldString(fieldName, fieldValue, &len); value = GetANSIFromUTF16(fieldValue); */ const wchar_t *fieldName; char *name; const char *fieldValue; char *value; fieldName = pTagField->GetFieldName(); name = GetANSIFromUTF16(fieldName); fieldValue = pTagField->GetFieldValue(); if (pAPETag->GetAPETagVersion() == CURRENT_APE_TAG_VERSION) { value = GetANSIFromUTF8((unsigned char *)fieldValue); } else { value = (char *)fieldValue; } printf("\t%s : %s\n", name, value); } } } else { printf("\tNot tagged\r\n\r\n"); } /////////////////////////////////////////////////////////////////////////////// // cleanup (just delete the object /////////////////////////////////////////////////////////////////////////////// delete pAPEDecompress; /////////////////////////////////////////////////////////////////////////////// // quit /////////////////////////////////////////////////////////////////////////////// return 0; }
int CAPETag::GetFieldString(const str_utf16 * pFieldName, str_ansi * pBuffer, int * pBufferCharacters, BOOL bUTF8Encode) { int nOriginalCharacters = *pBufferCharacters; str_utf16 * pUTF16 = new str_utf16 [*pBufferCharacters + 1]; pUTF16[0] = 0; int nRetVal = GetFieldString(pFieldName, pUTF16, pBufferCharacters); if (nRetVal == ERROR_SUCCESS) { CSmartPtr<str_ansi> spANSI(bUTF8Encode ? (str_ansi *) GetUTF8FromUTF16(pUTF16) : GetANSIFromUTF16(pUTF16), TRUE); if (int(strlen(spANSI)) > nOriginalCharacters) { memset(pBuffer, 0, nOriginalCharacters * sizeof(str_ansi)); *pBufferCharacters = 0; nRetVal = ERROR_UNDEFINED; } else { strcpy(pBuffer, spANSI); *pBufferCharacters = strlen(spANSI); } } delete [] pUTF16; return nRetVal; }
int CAPETagField::GetFieldSize() { CSmartPtr<char> spFieldNameANSI(GetANSIFromUTF16(m_spFieldNameUTF16), TRUE); return (strlen(spFieldNameANSI) + 1) + m_nFieldValueBytes + 4 + 4; }