Beispiel #1
0
 /**
 *  \fn buildDirectoryContent
 * 	\brief Returns the content of a dir with the extension ext. The receiving array must be allocated by caller
 * (just the array, not the names themselves)
 */
 uint8_t buildDirectoryContent(uint32_t *outnb, const char *base, char *jobName[], int maxElems, const char *ext)
 {

    std::string joker = std::string(base) + std::string("/*.") + std::string(ext);
 	int dirNameLength = utf8StringToWideChar(joker.c_str(), -1, NULL);
 	wchar_t *base2 = new wchar_t[dirNameLength];
    utf8StringToWideChar(joker.c_str(), -1, base2);
    int dirmax = 0;
//--
    HANDLE hFind;
    WIN32_FIND_DATAW FindFileData;

    hFind = FindFirstFileW(base2,&FindFileData);
    if(hFind == INVALID_HANDLE_VALUE)
    {
        ADM_warning("Cannot list content of %s\n", base);
        delete[] base2;
        *outnb = 0;
        return true;
    }

    do 
    {
        WCHAR *wname = FindFileData.cFileName;
        //int wideCharStringToAnsi(const wchar_t *wideCharString, int wideCharStringLength, char *ansiString, const char *filler)
        int nameLength = wideCharStringToAnsi(wname, -1, NULL, "?");       
        char *shortName = new char[nameLength];
        nameLength = wideCharStringToAnsi(wname, -1, shortName, "?");
        std::string item = std::string(base) + std::string("/") + std::string(shortName);
        delete[] shortName;

        int targetLength = item.length();
        jobName[dirmax] = (char *)ADM_alloc(targetLength);
        strcpy(jobName[dirmax], item.c_str());
        dirmax++;
        if (dirmax > maxElems)
            break;
            
    } while (FindNextFileW(hFind, &FindFileData));
    FindClose(hFind);
    
    *outnb = dirmax;

    delete[] base2;
    return true;
 }
// Convert UTF-8 file path to an ANSI path with short 8.3 directories
void convertPathToAnsi(const char *path, char **ansiPath)
{
	const char *filename = GetFileName(path);
	bool statFile = false;
	int filenameLength = strlen(filename);
	int directoryLength = filename - path;

	// Clip off .stat extension and tack it on later
	if (filenameLength >= 5 && strcmp(filename + filenameLength - 5, ".stat") == 0)
	{
		statFile = true;
		filenameLength -= 5;
	}

	// Convert directory to wide char
	int wcDirLength = utf8StringToWideChar(path, directoryLength, NULL) + 1;
	int wcFileLength = utf8StringToWideChar(filename, filenameLength, NULL) + 1;
	wchar_t *wcDirectory = new wchar_t[wcDirLength];

	memset(wcDirectory, 0, wcDirLength * sizeof(wchar_t));
	utf8StringToWideChar(path, directoryLength, wcDirectory);

	// Get short directory
	int shortDirLength = GetShortPathNameW(wcDirectory, NULL, 0);
	wchar_t *wcShortDir = new wchar_t[shortDirLength + wcFileLength];

	memset(wcShortDir, 0, (shortDirLength + wcFileLength) * sizeof(wchar_t));
	GetShortPathNameW(wcDirectory, wcShortDir, shortDirLength);
	delete [] wcDirectory;

	// Append filename to directory
	utf8StringToWideChar(filename, filenameLength, wcShortDir + (shortDirLength - 1));

	// Convert path to ANSI
	int dirtyAnsiPathLength = wideCharStringToAnsi(wcShortDir, -1, NULL, "?");
	char *dirtyAnsiPath = new char[dirtyAnsiPathLength];

	wideCharStringToAnsi(wcShortDir, -1, dirtyAnsiPath, "?");

	// Clean converted path
	std::string cleanPath = std::string(dirtyAnsiPath);
	std::string::iterator lastPos = std::remove(cleanPath.begin(), cleanPath.end(), '?');

	cleanPath.erase(lastPos, cleanPath.end());
	delete [] dirtyAnsiPath;

	// Make sure we have a filename, otherwise use "avidemux" as default
	if (filenameLength || statFile)
	{
		filename = GetFileName(cleanPath.c_str());

		int filenameStart = filename - cleanPath.c_str();

		if (filenameStart)
		{
			int filenameEnd = cleanPath.length();

			// Ignore extension if it exists
			int extensionIndex = cleanPath.rfind(".", filenameEnd);

			if (extensionIndex != std::string::npos)
				filenameEnd = extensionIndex;

			// Create a filename if one doesn't exist
			if (filenameEnd - filenameStart == 0)
				cleanPath.insert(filenameStart, "avidemux");
		}
		else
			cleanPath.append("avidemux");

		if (statFile)
			cleanPath.append(".stat");
	}

	*ansiPath = new char[cleanPath.length() + 1];
	strcpy(*ansiPath, cleanPath.c_str());

	delete [] wcShortDir;
}