Пример #1
0
 /*----------------------------------------
       Create a directory
       If it already exists, do nothing
 ------------------------------------------*/
 uint8_t ADM_mkdir(const char *dirname)
 {

    int dirNameLength = utf8StringToWideChar(dirname, -1, NULL);
    wchar_t *dirname2 = new wchar_t[dirNameLength];
    utf8StringToWideChar(dirname, -1, dirname2);
    int er = _wmkdir(dirname2);
    delete[] dirname2;
    uint8_t retVal = 0;


    if (er != -1)
        retVal = true;
    else
    {
        if (errno == EEXIST)
            retVal = true;
        else
        {
            ADM_warning("Failed to create folder %s with error %d\n", dirname, errno);
            retVal = false;
        }
    }

 	
 	return retVal;
 }
Пример #2
0
 /**
     \fn ADM_fopen
     \brief utf8 aware fopen, so that we can use utf8 string even on win32
 */
 FILE *ADM_fopen(const char *file, const char *mode)
 {

 	// Override fopen to handle Unicode filenames and to ensure exclusive access when initially writing to a file.
 	int fileNameLength = utf8StringToWideChar(file, -1, NULL);
 	wchar_t *wcFile = new wchar_t[fileNameLength];
 	int creation = 0, access = 0;
 	HANDLE hFile;

 	utf8StringToWideChar(file, -1, wcFile);

 	while (true)
 	{
 		if (strchr(mode, 'w'))
 		{
 			creation = CREATE_ALWAYS;
 			access = GENERIC_WRITE;

 			if (strchr(mode, '+'))
 				access |= GENERIC_READ;
 		}
 		else if (strchr(mode, 'r'))
 		{
 			creation = OPEN_EXISTING;
 			access = GENERIC_READ;

 			if (strchr(mode, '+'))
 				access = GENERIC_WRITE;
 		}
 		else if (strchr(mode, 'a'))
 		{
 			creation = OPEN_ALWAYS;
 			access = GENERIC_WRITE;

 			if (strchr(mode, '+'))
 				access |= GENERIC_READ;
 		}

 		if (creation & GENERIC_WRITE)
 		{
 			hFile = CreateFileW(wcFile, access, 0, NULL, creation, 0, NULL);

 			if (hFile == INVALID_HANDLE_VALUE)
 				break;
 			else
 				CloseHandle(hFile);
 		}

 		hFile = CreateFileW(wcFile, access, FILE_SHARE_READ, NULL, creation, 0, NULL);
 		break;
 	}

 	delete [] wcFile;

 	if (hFile == INVALID_HANDLE_VALUE)
 		return NULL;
 	else
 		return _fdopen(_open_osfhandle((intptr_t)hFile, 0), mode);
 }
Пример #3
0
bool spawnProcess(const char *processName, int argc, const string argv[])
{
    ADM_info("Starting <%s>\n",processName);
    string command=string(processName);
    for(int i=0;i<argc;i++)
    {
        ADM_info("%d : %s\n",i,argv[i].c_str());
        command+=string(" ")+argv[i];
    }
    ADM_info("=>%s\n",command.c_str());
    ADM_info("==================== Start of spawner process job ================\n");

#ifdef _WIN32
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    // utf8 -> utf16
    const char *c=command.c_str();
    int size=utf8StringToWideChar(c,strlen(c),NULL);
    wchar_t* w = new wchar_t[size+1];

    utf8StringToWideChar(c,strlen(c),w);
    // Start the child process. 
    if( !CreateProcessW( 
        NULL,   // No module name (use command line)
        w,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    )
    {
        delete [] w;
        ADM_error("Cannot spawn process! (%s)\n",c);
        return false;
    }

    delete [] w;
    
#else
    system(command.c_str());
#endif
    ADM_info("==================== End of spawner process job ================\n");
    return true;
}
Пример #4
0
int ADM_access(const char *path, int mode)
{
#ifdef __WIN32
	int pathLength = utf8StringToWideChar(path, -1, NULL);
	wchar_t wcPath[pathLength];

	utf8StringToWideChar(path, -1, wcPath);

	return _waccess(wcPath, mode);
#else
	return access(path, mode);
#endif
}
Пример #5
0
int ADM_unlink(const char *filename)
{
#ifdef __WIN32
	int filenameLength = utf8StringToWideChar(filename, -1, NULL);
	wchar_t wcFilename[filenameLength];

	utf8StringToWideChar(filename, -1, wcFilename);

	return _wunlink(wcFilename);
#else
	return unlink(filename);
#endif
}
Пример #6
0
 	// libavformat uses open (in the file_open function) so we need to override that too.
 	// Following the same rules as ADM_fopen.
 	int ADM_open(const char *path, int oflag, ...)
 	{
 		int fileNameLength = utf8StringToWideChar(path, -1, NULL);
 		wchar_t *wcFile = new wchar_t[fileNameLength];
 		int creation = 0, access = 0;
 		HANDLE hFile;

 		utf8StringToWideChar(path, -1, wcFile);

 		delete [] wcFile;

 		if (oflag & O_WRONLY || oflag & O_RDWR)
 		{
 			access = GENERIC_WRITE;

 			if (oflag & O_RDWR)
 				access |= GENERIC_READ;

 			if (oflag & O_CREAT)
 			{
 				if (oflag & O_EXCL)
 					creation = CREATE_NEW;
 				else if (oflag & O_TRUNC)
 					creation = CREATE_ALWAYS;
 				else
 					creation = OPEN_ALWAYS;
 			}
 			else if (oflag & O_TRUNC)
 				creation = TRUNCATE_EXISTING;
 		}
 		else if (oflag & O_RDONLY)
 			creation = OPEN_EXISTING;

 		if (creation & GENERIC_WRITE)
 		{
 			hFile = CreateFileW(wcFile, access, 0, NULL, creation, 0, NULL);

 			if (hFile == INVALID_HANDLE_VALUE)
 				return -1;
 			else
 				CloseHandle(hFile);
 		}

 		hFile = CreateFileW(wcFile, access, FILE_SHARE_READ, NULL, creation, 0, NULL);

 		if (hFile == INVALID_HANDLE_VALUE)
 			return -1;
 		else
 			return _open_osfhandle((intptr_t)hFile, oflag);
 	}
Пример #7
0
 /**
     \fn ADM_copyFile
 */
 uint8_t ADM_renameFile(const char *source, const char *target)
 {

     int sourceFileNameLength = utf8StringToWideChar(source, -1, NULL);
     int targetFileNameLength = utf8StringToWideChar(target, -1, NULL);
     wchar_t *wcFileSource=(wchar_t*)_alloca(sourceFileNameLength*sizeof(wchar_t));
     wchar_t *wcFileTarget= (wchar_t*)_alloca(targetFileNameLength * sizeof(wchar_t));

     utf8StringToWideChar(source, -1, wcFileSource);
     utf8StringToWideChar(target, -1, wcFileTarget);

     if(!_wrename(wcFileSource,wcFileTarget)) return true;
     ADM_error("Failed to rename %s to %s\n",source,target);
     return false;
 }
Пример #8
0
/*----------------------------------------
      Create a directory
      If it already exists, do nothing
------------------------------------------*/
uint8_t ADM_mkdir(const char *dirname)
{
	DIR *dir = NULL;

#ifdef __WIN32
	int dirNameLength = utf8StringToWideChar(dirname, -1, NULL);
	wchar_t dirname2[dirNameLength];

	utf8StringToWideChar(dirname, -1, dirname2);
#else
	const char* dirname2 = dirname;
#endif

	// Check it already exists ?
	dir = opendir(dirname2);

	if (dir)
	{ 
		printf("Directory %s exists.Good.\n", dirname);
		closedir(dir);
		return 1;
	}
#ifdef __WIN32
	if (_wmkdir(dirname2))
	{
		printf("Oops: mkdir failed on %s\n", dirname);
		return 0;
	}
#else
	char *sys = new char[strlen(dirname2) + strlen("mkdir ") + 2];

	strcpy(sys, "mkdir ");
	strcat(sys, dirname2);
	printf("Creating dir :%s\n", sys);
	system(sys);

	delete [] sys;
#endif

	if ((dir = opendir(dirname2)) == NULL)
		return 0;

	closedir(dir);

	return 1;
}
Пример #9
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;
 }
bool CDirectorySearch::Init(std::string sDirectory)
{   // begin Init
    // check for no search query
    if(sDirectory[sDirectory.length()-1] == DIRECTORY_DELIMITOR)
    {
        printf("End with directory delimitor\n");
        return false;
    }

#ifdef __WIN32
    const char *old = sDirectory.c_str();

    int len = utf8StringToWideChar(old, -1, NULL);
    wchar_t s[len + 2];

    utf8StringToWideChar(old, -1, s);
    wcscat(s, L"\\*");

    printf(">%ls<\n", s);
#else
    const char *s = sDirectory.c_str();

    printf(">%s<\n",s);
#endif

    m_hSearch = _wfindfirst(s,&m_fdData);

    if(m_hSearch == -1)
    {
        printf("Find first failed\n");
        return false;
    }

    m_sDirectory = sDirectory;

    return true;
}// end Init
Пример #11
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)
{
	DIR *dir;
	struct dirent *direntry;
	int dirmax = 0, len;
	int extlen = strlen(ext);

	ADM_assert(extlen);

#ifdef __WIN32
	int dirNameLength = utf8StringToWideChar(base, -1, NULL);
	wchar_t base2[dirNameLength];

	utf8StringToWideChar(base, -1, base2);
#else
	const char *base2 = base;
#endif

	dir = opendir(base2);
	if (!dir)
		return 0;

	while (direntry = readdir(dir))
	{
#ifdef __WIN32
		int dirLength = wideCharStringToUtf8(direntry->d_name, -1, NULL);
		char d_name[dirLength];

		wideCharStringToUtf8(direntry->d_name, -1, d_name);
#else
		const char *d_name = direntry->d_name;
#endif

		len = strlen(d_name);

		if (len < (extlen + 1))
			continue;

		int xbase = len - extlen;

		if (memcmp(d_name + xbase, ext, extlen))
			//if (direntry->d_name[len-1]!='s' || direntry->d_name[len-2]!='j' || direntry->d_name[len-3]!='.')
		{
			printf("ignored: %s\n", d_name);
			continue;
		}

		jobName[dirmax] = (char *)ADM_alloc(strlen(base) + strlen(d_name) + 2);
		strcpy(jobName[dirmax], base);
		strcat(jobName[dirmax], "/");
		strcat(jobName[dirmax], d_name);
		dirmax++;

		if (dirmax >= maxElems)
		{
			printf("[jobs]: Max # of jobs exceeded\n");
			break;
		}
	}

	closedir(dir);
	*outnb = dirmax;

	return 1;
}
Пример #12
0
// 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;
}