Пример #1
0
int FileManager::GetFilesAux(int argc, LPTSTR argv[], CHAR* filter, vector<string>& filePathes)
{
    BOOL flags[MAX_OPTIONS], ok = TRUE;
    TCHAR searchPattern[MAX_PATH + 1], parentPath[MAX_PATH_LONG+1];
    LPTSTR pSlash, pSearchPattern;
    int i, fileIndex;
    DWORD pathLength;

    flags[0] = 1;
    flags[1] = 0;
    fileIndex = 2; //Options(argc, argv, _T("Rl"), &flags[0], &flags[1], NULL);

    /* "Parse" the search pattern into two parts: the "parent"
        and the file name or wild card expression. The file name
        is the longest suffix not containing a slash.
        The parent is the remaining prefix with the slash.
        This is performed for all command line search patterns.
        If no file is specified, use * as the search pattern. */

    pathLength = _tcslen(currPath);

    if (pathLength == 0 || pathLength >= MAX_PATH_LONG) { /* pathLength >= MAX_PATH_LONG (32780) should be impossible */
        std::cerr << "GetCurrentDirectory failed";
        return FALSE;
    }

    if (argc < fileIndex + 1) 
        ok = TraverseDirectory(currPath, _T("*"), MAX_OPTIONS, flags, filter, filePathes);
    else for (i = fileIndex; i < argc; i++) {
        if (_tcslen(argv[i]) >= MAX_PATH) {
            std::cerr << "The command line argument is longer than the maximum this program supports";
            return FALSE;
        }
        _tcscpy(searchPattern, argv[i]);
        _tcscpy(parentPath, argv[i]);

        /* Find the rightmost slash, if any.
            Set the path and use the rest as the file name. */
        pSlash = _tstrrchr(parentPath, _T('\\')); 
        if (pSlash != NULL) {
            *pSlash = _T('\0');
            _tcscat(parentPath, _T("\\"));         
            SetCurrentDirectory(parentPath); /* Now restore searchPattern. */
            pSlash = _tstrrchr(searchPattern, _T('\\'));  
            pSearchPattern = pSlash + 1;
        } else {
            _tcscpy(parentPath, _T(".\\"));
            pSearchPattern = searchPattern;
        }
        ok = TraverseDirectory(parentPath, pSearchPattern, MAX_OPTIONS, flags, filter, filePathes) && ok;
        SetCurrentDirectory(currPath);  /* Restore working directory. */
    }

    return ok ? 0 : 1;
}
Пример #2
0
	bool File::listDirectoryFiles( Path const & p_folderPath, PathArray & p_files, bool p_recursive )
	{
		struct FileFunction
		{
			explicit FileFunction( PathArray & p_files )
				: m_files( p_files )
			{
			}
			void operator()( Path const & p_path )
			{
				m_files.push_back( p_path );
			}
			PathArray & m_files;
		};

		if ( p_recursive )
		{
			struct DirectoryFunction
			{
				explicit DirectoryFunction( PathArray & p_files )
					: m_files( p_files )
				{
				}
				bool operator()( Path const & p_path )
				{
					return TraverseDirectory( p_path, DirectoryFunction( m_files ), FileFunction( m_files ) );
				}
				PathArray & m_files;
			};

			return TraverseDirectory( p_folderPath, DirectoryFunction( p_files ), FileFunction( p_files ) );
		}
		else
		{
			struct DirectoryFunction
			{
				DirectoryFunction()
				{
				}
				bool operator()( Path const & p_path )
				{
					return true;
				}
			};

			return TraverseDirectory( p_folderPath, DirectoryFunction(), FileFunction( p_files ) );
		}
	}
Пример #3
0
	bool File::directoryDelete( Path const & p_path )
	{
		struct FileFunction
		{
			void operator()( Path const & p_path )
			{
				File::deleteFile( p_path );
			}
		};

		struct DirectoryFunction
		{
			bool operator()( Path const & p_path )
			{
				bool result = TraverseDirectory( p_path, DirectoryFunction(), FileFunction() );

				if ( result )
				{
					result = DeleteEmptyDirectory( p_path );
				}

				return result;
			}
		};

		bool result = TraverseDirectory( p_path, DirectoryFunction(), FileFunction() );

		if ( result )
		{
			result = DeleteEmptyDirectory( p_path );
		}

		return result;
	}
Пример #4
0
			bool operator()( Path const & p_path )
			{
				bool result = TraverseDirectory( p_path, DirectoryFunction(), FileFunction() );

				if ( result )
				{
					result = DeleteEmptyDirectory( p_path );
				}

				return result;
			}
Пример #5
0
static bool TraverseDirectory(TCHAR* pathName,TCHAR* newPathName) {
	HANDLE SearchHandle;
	WIN32_FIND_DATA FindData;
	DWORD FType;
	TCHAR cpy[100];
	lstrcpy(cpy, pathName);
	bool sw = false;
	//_tprintf(_T("\n---> Compare %s:\n"),newPathName);

	SetCurrentDirectory(pathName);
	SearchHandle = FindFirstFile(_T("*"), &FindData);
	do {
		FType = FileType(&FindData);
		if (FType == TYPE_FILE){
			//	_tprintf(_T("FILE: %s\n"),FindData.cFileName);
				if (strcmp(newPathName, FindData.cFileName) == 0){
					sw= true;
				}
		}
		if (FType == TYPE_DIR) {

			//_tprintf(_T("DIR : %s\n"), FindData.cFileName);
			if (strcmp(newPathName, FindData.cFileName) == 0){
				sw = true;
			}
			lstrcat(cpy, "\\");
			lstrcat(cpy, FindData.cFileName);

	
			if (TraverseDirectory(cpy,newPathName) == true){
				sw = true;
			}
			lstrcpy(cpy, pathName);

		}
	} while (FindNextFile(SearchHandle, &FindData));

	FindClose(SearchHandle);
	if (sw == true){
		return true;
	}
	else{
		return false;
	}
}
Пример #6
0
void FeaturesTable::TraverseDirectory(const std::string& path, std::string& pattern, bool subdirectories, std::vector<std::string>& fileNames) {
	struct _finddatai64_t data;
	std::string fname = path + "\\" + pattern;
	// start the finder -- on error _findfirsti64() will return -1, otherwise if no
	// error it returns a handle greater than -1.
	intptr_t h = _findfirsti64(fname.c_str(),&data);
	if(h >= 0) {
		do {
			if( (data.attrib & _A_SUBDIR) ) {
				if( subdirectories && strcmp(data.name,".") != 0 && strcmp(data.name,"..") != 0) {
					fname = path + "\\" + data.name;
					TraverseDirectory(fname,pattern,true, fileNames);
				}
			} else {
				fileNames.push_back(path + "\\" + data.name);
			}
		} while( _findnexti64(h,&data) == 0);

		_findclose(h);
	}
}
Пример #7
0
int FeaturesTable::LoadDataSet() {
	std::vector<std::string> fNames;
	std::string pattern("Class*");
	TraverseDirectory(Folder, pattern, false, fNames);

	size_t numFeatures = size_t(-1);
	CumSamplesPerClass.assign(fNames.size()+1,0);
	ClassDistribution.assign(fNames.size(), 0);
	// ClassDistributionSize = fNames.size();
	// ClassDistribution = new size_t[ClassDistributionSize];
	// std::fill(ClassDistribution, ClassDistribution + ClassDistributionSize, 0);
	std::string delimStr = "\t";
	for(size_t k=0;k<fNames.size();++k) {
		// std::cout << fNames[k] << std::endl;
		std::ifstream ifs(fNames[k].c_str(), std::ios_base::in);
		std::string line;
		while(!ifs.eof()) {
			line.clear();
			std::getline(ifs, line, '\n');
			std::vector<double>* L = new std::vector<double>;
			size_t tmpNumFeatures = convertStr(*L,line, delimStr, false);
			if(numFeatures==size_t(-1)) {
				numFeatures = tmpNumFeatures;
			} else {
				if(numFeatures!=tmpNumFeatures) {
					continue;
				}
			}
			size_t numEl = FlatData.size();
			FlatData.push_back(L);
			ValidDataIDXToLine.insert(std::make_pair<size_t,size_t>(numEl,numEl));
		}
		ifs.close();
		CumSamplesPerClass[k+1] = FlatData.size();
		ClassDistribution[k] = CumSamplesPerClass[k+1]-CumSamplesPerClass[k];
	}
	ValidClassDistribution = ClassDistribution;
	ValidCumSamplesPerClass = CumSamplesPerClass;
	return 0;
}
Пример #8
0
DWORD WINAPI compThread(LPVOID param){
	TCHAR* dir = ((TCHAR*)param);
	while (true){
		//_tprintf(_T("->Hi comp\n"));
		WaitForSingleObject(cmp, INFINITE);
		//_tprintf(_T("->%s\n"),dir);
		if(TraverseDirectory(dir, compa)==true){
			//_tprintf(_T("->GOOOOOD!\n"));
			swF = true;
		}
		else{
			swF = false;
			_tprintf(_T("->Different Directories!\n"));
			exit(0);
			
		}
		//_tprintf(_T("->Bye comp\n"));
		ReleaseSemaphore(rd, 1, NULL);
	}

	return 0;
}
Пример #9
0
int main()
{
    int argc;
    wchar_t **argv = CommandLineToArgvW(GetCommandLineW(), &argc);
#else
int main(int argc, char *argv[])
{
#endif // _WIN32

    is_fast = false;
    is_verbose = false;
    iterations = 15;
    depth = 1;
    max_depth = INT_MAX;

#ifdef _WIN32
    is_pause = !getenv("PROMPT");
#endif // _WIN32

    int i;
    for (i = 1; i < argc && argv[i][0] == L'-'; i++)
    {
#ifdef _WIN32
        // do not pause if any options are given
        is_pause = false;
#endif // _WIN32
        int num_optargs = 0;
        for (int j = 1; argv[i][j]; j++)
        {
            switch (argv[i][j])
            {
            case 'f':
                is_fast = true;
                break;
            case 'i':
                if (i < argc - 1)
                {
                    iterations = STRTOL(argv[i + ++num_optargs], nullptr, 10);
                    // strtol will return 0 on fail
                    if (iterations == 0)
                    {
                        std::cerr << "There should be a positive number after -i option." << std::endl;
                        PrintInfo();
                        return 1;
                    }
                }
                break;
            case 'd':
                if (i < argc - 1)
                {
                    max_depth = STRTOL(argv[i + ++num_optargs], nullptr, 10);
                    // strtol will return 0 on fail
                    if (max_depth == 0)
                    {
                        std::cerr << "There should be a positive number after -d option." << std::endl;
                        PrintInfo();
                        return 1;
                    }
                }
                break;
            case 'q':
                std::cout.setstate(std::ios::failbit);
                is_verbose = false;
                break;
            case 'v':
                std::cout.clear();
                is_verbose = true;
                break;
            case '-':
                if (STRCMP(argv[i] + j + 1, "fastmode") == 0)
                {
                    j += 7;
                    argv[i][j + 1] = 'f';
                }
                else if (STRCMP(argv[i] + j + 1, "iteration") == 0)
                {
                    j += 8;
                    argv[i][j + 1] = 'i';
                }
                else if (STRCMP(argv[i] + j + 1, "max_depth") == 0)
                {
                    j += 8;
                    argv[i][j + 1] = 'd';
                }
                else if (STRCMP(argv[i] + j + 1, "quiet") == 0)
                {
                    j += 4;
                    argv[i][j + 1] = 'q';
                }
                else if (STRCMP(argv[i] + j + 1, "verbose") == 0)
                {
                    j += 6;
                    argv[i][j + 1] = 'v';
                }
                else if (STRCMP(argv[i] + j + 1, "keep-exif") == 0)
                {
                    j += 9;
                    Jpeg::keep_exif = true;
                }
                else
                {
#ifdef _WIN32
                    char mbs[64] = { 0 };
                    WideCharToMultiByte(CP_ACP, 0, argv[i] + j + 1, -1, mbs, sizeof(mbs) - 1, nullptr, nullptr);
                    std::cerr << "Unknown option: " << mbs << std::endl;
#else
                    std::cerr << "Unknown option: " << argv[i] + j + 1 << std::endl;
#endif // _WIN32
                    PrintInfo();
                    return 1;
                }
                break;
            default:
                std::cerr << "Unknown option: " << (char)argv[i][j] << std::endl;
                PrintInfo();
                return 1;
            }
        }
        i += num_optargs;
    }

    if (i == argc)
    {
        std::cerr << "No file path provided." << std::endl;
        PrintInfo();
        return 1;
    }


    std::cout << std::fixed;
    std::cout.precision(2);

    // support multiple input file
    do
    {
        if (IsDirectory(argv[i]))
        {
            // directory
            TraverseDirectory(argv[i], ProcessFile);
        }
        else
        {
            // file
            ProcessFile(argv[i]);
        }

    }
    while (++i < argc);


    PauseIfNotTerminal();

    return 0;
}
Пример #10
0
				bool operator()( Path const & p_path )
				{
					return TraverseDirectory( p_path, DirectoryFunction( m_files ), FileFunction( m_files ) );
				}
Пример #11
0
//----------------------------------------------------------------------------------------------
BOOL FileManager::TraverseDirectory(LPTSTR parentPath, LPTSTR searchPattern, DWORD numFlags, LPBOOL flags, CHAR* filter, vector<string>& filePathes)
/* Traverse a directory, carrying out an implementation specific "action" for every
    name encountered. The action in this version is "list, with optional attributes". */
/* searchPattern: Relative or absolute searchPattern to traverse in the parentPath.  */
/* On entry, the current directory is parentPath, which ends in a \ */
{
    HANDLE searchHandle;
    WIN32_FIND_DATA findData;
    BOOL recursive = flags[0];
    DWORD fType, iPass, lenParentPath;

    /* Open up the directory search handle and get the
        first file name to satisfy the path name.
        Make two passes. The first processes the files
        and the second processes the directories. */

    if ( _tcslen(searchPattern) == 0 ) {
        _tcscat(searchPattern, _T("*"));
    }
    /* Remove a backslash, if any, at the end of the parent path */
    if (parentPath[_tcslen(parentPath)-1] != _T('\\') ) { /* Add a \ to the end of the parent path, unless there already is one */
        _tcscat (parentPath, _T("\\"));
    }

    /* Open up the directory search handle and get the
        first file name to satisfy the path name. Make two passes.
        The first processes the files and the second processes the directories. */

    CHAR* szDirct = (CHAR*)malloc(MAX_PATH);
    GetCurrentDirectory(MAX_PATH, szDirct);

    for (iPass = 1; iPass <= 2; iPass++) {
        searchHandle = FindFirstFile("*", &findData);
        if (searchHandle == INVALID_HANDLE_VALUE) {
            std::cerr << "Error opening Search Handle.";
            return FALSE;
        }

        /* Scan the directory and its subdirectories for files satisfying the pattern. */
        do {

        /* For each file located, get the type. List everything on pass 1.
            On pass 2, display the directory name and recursively process
            the subdirectory contents, if the recursive option is set. */
            fType = FileType(&findData);
            if (iPass == 1) /* ProcessItem is "print attributes". */
            {
                if (CompareExtension(findData.cFileName, filter))
                {
                    string filePath = currPath;
                    filePath += findData.cFileName;
                    filePathes.push_back(filePath);
                }
            }

            lenParentPath = (DWORD)_tcslen(parentPath);
            /* Traverse the subdirectory on the second pass. */
            if (fType == TYPE_DIR && iPass == 2 && recursive) {
                _tprintf(_T("\n%s%s:"), parentPath, findData.cFileName);
                SetCurrentDirectory(findData.cFileName);
                if (_tcslen(parentPath) + _tcslen(findData.cFileName) >= MAX_PATH_LONG-1) {
                    std::cerr << "Path Name is too long";
                }
                _tcscat (parentPath, findData.cFileName); /* The parent path terminates with \ before the _tcscat call */
                TraverseDirectory(parentPath, _T("*"), numFlags, flags, filter, filePathes);
                /* Restore parent path - It will then terminate with \ */
                parentPath[lenParentPath] = _T('\0');
                SetCurrentDirectory(parentPath); /* SetCurrentDirectory(_T("..")); would also work */
            }

            /* Get the next file or directory name. */

        } while (FindNextFile(searchHandle, &findData));

        FindClose(searchHandle);
    }
    return TRUE;
}
Пример #12
0
size_t VT::list_files( const std::wstring& directory, const std::wstring& mask, std::list<std::wstring>& result )
{
    return TraverseDirectory(directory, mask, FILE_ATTRIBUTE_DIRECTORY, 0xFFFFFFFF, result);
}
Пример #13
0
size_t VT::list_files_folders( const std::wstring& directory, const std::wstring& mask, std::list<std::wstring>& result )
{
    return TraverseDirectory(directory, mask, 0, 0xFFFFFFFF, result);
}