std::string FileUtility::findDir(std::string name, std::string base, int depth)
{
    std::string found = "";
    
    DIR *bd;    
    if ((bd = opendir(base.c_str())) == NULL)
    {
        perror("Failed to open directory");
        err = "Failed to open base directory " + base;
        return "";
    }

    /* The search is breadth first because the expected structure is relative flat and not 
     * deeply nested. */
    struct dirent *dent;
    while ((dent = readdir(bd)) != NULL)
    {        
        if (dent->d_type != DT_DIR || is_relative_path(dent->d_name)) continue;

        if (FileUtility::exists(FileUtility::makePath(base, dent->d_name), name))
        {
            found = FileUtility::makePath(base, dent->d_name);
            goto close_dir;
        }
    }

    if (depth > 0)
    {
        /* Not found, search nested directories. */
        rewinddir(bd);
        while ((dent = readdir(bd)) != NULL)
        {
            if (dent->d_type != DT_DIR || is_relative_path(dent->d_name)) continue;

            if ((found = FileUtility::findDir(name, FileUtility::makePath(base, dent->d_name), depth -1)).length())
            {
                /* Found directory. */
                break;
            }
        }
    }

close_dir:
    if (closedir(bd))
    {
        perror("Failed to close directory");
        err = "Failed to close directory " + base;
    }
    
    return found;
}
Пример #2
0
char *make_bb_path(HINSTANCE h, char *dest, const char *src)
{
    dest[0]=0;
    if (is_relative_path(src))
		get_exe_path(h, dest, sizeof dest);
    return strcat(dest, src);
}
Пример #3
0
/*
 * This function does not canonicalize the path! It just prepends CWD before a
 * relative path. If the path is no relative than returns NULL. The path does
 * not have to exist.
 */
char *absolute_path(const char *path)
{
	char cwd[PATH_MAX], *res, *p;
	size_t psz, csz;

	if (!is_relative_path(path)) {
		errno = EINVAL;
		return NULL;
	}
	if (!getcwd(cwd, sizeof(cwd)))
		return NULL;

	/* simple clean up */
	if (startswith(path, "./"))
		path += 2;
	else if (strcmp(path, ".") == 0)
		path = NULL;

	if (!path || !*path)
		return strdup(cwd);

	csz = strlen(cwd);
	psz = strlen(path);

	p = res = malloc(csz + 1 + psz + 1);
	if (!res)
		return NULL;

	memcpy(p, cwd, csz);
	p += csz;
	*p++ = '/';
	memcpy(p, path, psz + 1);

	return res;
}
Пример #4
0
char *make_bb_path(char *dest, const char *src)
{
	dest[0]=0;
	if (is_relative_path(src))
		GetBlackboxPath(dest, MAX_PATH);
	return strcat(dest, src);
}
bool FileUtility::copyDir(std::string src, std::string dest, uid_t user, gid_t group)
{
    DIR *dir;
    struct dirent *ent;
    
    if ((dir = opendir(src.c_str())) == NULL)
    {
        perror("Failed to open directory");
        err = "Failed to open directory " + src;
        return false;
    }

    std::string dd;
    struct stat ds;

    while ((ent = readdir(dir)))
    {
        /* Don't want to recursive copy self or parent. */
        if (is_relative_path(ent->d_name)) continue;

	if (stat((src + "/" + ent->d_name).c_str(), &ds))
	{
	    perror("Unable to stat file");
	    err = std::string("Unable to state file ") + ent->d_name;
	    return false;
	}

	if (S_ISDIR(ds.st_mode))
	{
	    /* Directory - recursively copy. */
	    dd = dest + "/" + ent->d_name;
            if (!(FileUtility::createDir(dd, ds.st_mode, user, group) &&
                  FileUtility::copyDir(src + "/" + ent->d_name, dd, user, group))) return false;
	}
	else if (S_ISREG(ds.st_mode))
	{
	    /* Regular file - copy. */
	    if (!FileUtility::copyFile(src + "/" + ent->d_name, dest + "/" + ent->d_name,
				       user, group)) return false;
	}
	else
	{
	    /* Special file - ignore. */
	    std::cout << "Path " << ent->d_name << " is not a directory or a regular file, "
		      << "not copying it." << std::endl;
	}
    }

    if (closedir(dir))
    {
        perror("Failed to close directory");
        err = "Failed to close directory " + src;
        return false;
    }

    return true;
}
Пример #6
0
LPITEMIDLIST get_folder_pidl (const char *rawpath)
{
	if (NULL==rawpath) return NULL;

	char path [MAX_PATH];
	char temp [MAX_PATH];
	unquote(temp, rawpath);

	if (false == is_relative_path(temp))
	{
		if (is_alpha(temp[0]) && temp[1] == ':' && temp[2] == 0)
			temp[2] = '\\', temp[3] = 0;
		return sh_getpidl(NULL, temp);
	}

	const char *p = temp;
	int id = get_csidl(&p);
	if (NO_CSIDL == id || CSIDL_BLACKBOX == id || CSIDL_CURTHEME == id)
	{
		GetBlackboxPath(path, MAX_PATH);
		if (NO_CSIDL != id) path[strlen(path)-1] = 0;
		if (p) strcat(path, p);
		return sh_getpidl(NULL, path);
	}

	// special folders, like CONTROLS
	LPITEMIDLIST pID1, pID;

	if (NOERROR != SHGetSpecialFolderLocation(NULL, id, &pID1))
		return sh_getpidl(NULL, temp);

	if (NULL == p)
	{
		pID = duplicateIDlist(pID1);
	}
	else
	{
		pID = NULL;
		// a subdirectory is specified, like APPDATA\microsoft
		// so get its local pidl and append it
		IShellFolder*  pThisFolder = sh_get_folder_interface(pID1);
		if (pThisFolder)
		{
			LPITEMIDLIST pID2 = sh_getpidl(pThisFolder, p+1);
			if (pID2)
			{
				pID = joinIDlists(pID1, pID2);
				m_free(pID2);
			}
		}
	}

	SHMalloc_Free(pID1);
	return pID;
}
Пример #7
0
FeSound *FePresent::add_sound( const char *n, bool reuse )
{
    //
    // Behaviour:
    //
    // - if n is empty, return a new (empty) sound object
    // - if n is supplied:
    //      - if n matches an existing sound and reuse is true,
    //        return that matching sound object
    //      - otherwise return a new sound object loaded with n
    //

    std::string path;
    std::string name=n;
    if ( !name.empty() )
    {
        if ( is_relative_path( name ) )
        {
            int script_id = get_script_id();
            if ( script_id < 0 )
                m_feSettings->get_path( FeSettings::Current, path );
            else
                m_feSettings->get_plugin_full_path( script_id, path );
        }

        if ( reuse )
        {
            std::string test;
            if ( is_supported_archive( path ) )
                test = name;
            else
                test = path + name;

            for ( std::vector<FeSound *>::iterator itr=m_sounds.begin();
                    itr!=m_sounds.end(); ++itr )
            {
                if ( test.compare( (*itr)->get_file_name() ) == 0 )
                    return (*itr);
            }
        }
    }

    // Sound not found, try loading now
    //
    FeSound *new_sound = new FeSound();

    if ( !name.empty() )
        new_sound->load( path, name );

    new_sound->set_volume(
        m_feSettings->get_play_volume( FeSoundInfo::Sound ) );

    m_sounds.push_back( new_sound );
    return new_sound;
}
Пример #8
0
inline std::basic_string<Ch> require(const Ch* cmd) {
    // char wdb[PATH_MAX];     // working directory buffer
    // auto wd = ::getcwd(wdb, sizeof(wdb));
    if (is_absolute_path(cmd)) {
        return cmd;
    } else if (is_relative_path(cmd)) {
        return cmd;
    } else if (is_local_path(cmd)) {
        return find_executable_by_name(cmd);
    } else {
        return cmd;
    }
 }
Пример #9
0
char *replace_shellfolders(char *buffer, const char *path, bool search_path)
{
	char temp [MAX_PATH];
	const char *p = unquote(temp, path);

	if (false == is_relative_path(temp))
		return strcpy(buffer, temp);

	int id = get_csidl(&p);

	if (CSIDL_BLACKBOX == id || CSIDL_CURTHEME == id)
	{
		GetBlackboxPath(buffer, MAX_PATH);
		buffer[strlen(buffer)-1] = 0;
	}
	else
	if (NO_CSIDL == id)
	{
		if (search_path)
		{
			if (SearchPath(NULL, temp, ".exe", MAX_PATH, buffer, NULL))
				return buffer;
			else
				return strcpy(buffer, temp);
		}
		GetBlackboxPath(buffer, MAX_PATH);
	}
	else
	{
		// special folders, like CONTROLS
		LPITEMIDLIST pID;
		HRESULT hr = SHGetSpecialFolderLocation(NULL, id, &pID);
		if (NOERROR != hr)
			return strcpy(buffer, temp);

		// returns also things like "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
		// (unlike SHGetPathFromIDList)
		BOOL result = sh_get_displayname(NULL, pID, SHGDN_FORPARSING, buffer);
		SHMalloc_Free(pID);
		if (FALSE == result)
			return strcpy(buffer, temp);
	}

	if (p) strcat(buffer, p);
	return buffer;
}
Пример #10
0
FeSound *FePresent::add_sound( const char *n, bool reuse )
{
	//
	// Behaviour:
	//
	// - if n is empty, return a new (empty) sound object
	// - if n is supplied:
	//      - if n matches an existing sound and reuse is true,
	//        return that matching sound object
	//      - otherwise return a new sound object loaded with n
	//

	std::string name=n;
	if ( !name.empty() )
	{
		if ( is_relative_path( name ) )
		{
			name.clear();
			m_feSettings->get_path( FeSettings::Current, name );
			name += n;
		}

		if ( reuse )
		{
			for ( std::vector<FeSound *>::iterator itr=m_sounds.begin();
						itr!=m_sounds.end(); ++itr )
			{
				if ( name.compare( (*itr)->get_file_name() ) == 0 )
					return (*itr);
			}
		}
	}

	// Sound not found, try loading now
	//
	FeSound *new_sound = new FeSound();

	if ( !name.empty() )
		new_sound->load( name.c_str() );

	new_sound->set_volume(
		m_feSettings->get_play_volume( FeSoundInfo::Sound ) );

	m_sounds.push_back( new_sound );
	return new_sound;
}
Пример #11
0
void FeTextureContainer::load_from_archive( const char *a, const char *n )
{
	std::string path, filename = clean_path( n );

	if ( filename.empty() )
	{
		clear();
		notify_texture_change();
		return;
	}

	if ( a && ( strlen( a ) > 0 ))
		path = clean_path( a );

	// If it is a relative path we assume it is in the
	// layout/screensaver/intro directory
	//
	else if ( is_relative_path( filename ) )
		path = FePresent::script_get_base_path();

	bool is_image=false;

	int i=0;
	while ( FE_ART_EXTENSIONS[i] )
	{
		if ( tail_compare( filename, FE_ART_EXTENSIONS[i] ) )
		{
			is_image=true;
			break;
		}
		i++;
	}

	try_to_load( path, filename, is_image );
	notify_texture_change();
}
Пример #12
0
FeImage *FePresent::add_image( bool is_artwork,
		const std::string &n,
		int x,
		int y,
		int w,
		int h,
		std::vector<FeBasePresentable *> &l )
{
	FeTextureContainer *new_tex = new FeTextureContainer( is_artwork, n );
	new_tex->set_smooth( m_feSettings->get_info_bool( FeSettings::SmoothImages ) );

	FeImage *new_image = new FeImage( new_tex, x, y, w, h );
	new_image->set_scale_factor( m_layoutScale.x, m_layoutScale.y );

	// if this is a static image/video then load it now
	//
	if (( !is_artwork ) && ( n.find_first_of( "[" ) == std::string::npos ))
	{
		std::string name;
		if ( is_relative_path( n ) )
		{
			m_feSettings->get_path( FeSettings::Current, name );
			name += n;
		}
		else
			name = n;

		new_tex->load_static( name );
	}

	flag_redraw();
	m_texturePool.push_back( new_tex );
	l.push_back( new_image );

	return new_image;
}
Пример #13
0
BOOL CCmdLineParser::_DoParser(LPCTSTR lpCmdLine,
							  LPCTSTR lpCurDir, 
							  LPCTSTR lpEnvVars, 
							  LPCTSTR lpExtNames,
							  LPCTSTR lpParentPath
							  )
{
	BOOL	bResult = FALSE;
	LPCTSTR	lpCmdBegin = NULL;
	BOOL	bQuotation = FALSE;
	
	m_szCmd[0] = 0;

    if (m_lpParam != NULL)
    {
        delete[] m_lpParam;
        m_lpParam = NULL;
    }

	if ( is_empty_str(lpCmdLine) ) return FALSE;
	if ( is_empty_str(lpParentPath) ) lpParentPath = NULL;
	if ( is_empty_str(lpCurDir) ) lpCurDir = NULL;
	if ( is_empty_str(lpEnvVars) ) lpEnvVars = NULL;
	if ( is_empty_str(lpExtNames) ) lpExtNames = NULL;

	/*
	1、The directory from which the application loaded. 
	2、The current directory for the parent process. 
	3、The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
	Windows Me/98/95:  The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
	4、The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. 
	5、The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. 
	6、The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function. 
	*/
	lpCmdBegin = lpCmdLine;
	if ( *lpCmdBegin == _T('\"') )
	{
		lpCmdBegin = skip_quotation(lpCmdBegin);
		bQuotation = TRUE;
	}

	if ( (m_dwFlag & DEF_EXT_NAME) && lpExtNames == NULL )
	{
		// 默认扩展名
		lpExtNames = _T("bat;cmd;exe;pif");
	}

	if ( _tcsnicmp(lpCmdBegin, _T("\\??\\"), 4) == 0 )
	{
		lpCmdBegin += 4;
	}

	if ( _tcsnicmp(lpCmdBegin, _T("file://"), 7) == 0 )
	{
		if ( lpCmdBegin[8] == _T(':') )
		{
			lpCmdBegin += 7;
		}
	}

	/*
		宏展开
	*/
	if ( *lpCmdBegin == _T('%') )
	{
		LPTSTR lpCmdBuff = ExpandMacro(lpCmdBegin);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);

			delete lpCmdBuff;
			lpCmdBuff = NULL;
		}
		
		goto _Exit;
	}
	
	/*
		绝对路径展开
	*/
	if ( is_absolute_path(lpCmdBegin) )
	{
		bResult = ExpandAbsolute(lpCmdBegin, lpExtNames, bQuotation);
		goto _Exit;
	}

	/*
		相对路径转换
	*/
	if ( (lpCurDir != NULL) || (m_dwFlag & DEF_CUR_DIR) )
	{
		LPTSTR lpCmdBuff = ExpandRelative(lpCmdBegin, lpCurDir, bQuotation);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);

			delete lpCmdBuff;
			lpCmdBuff = NULL;

			if ( bResult )
			{
				goto _Exit;
			}
		}
	}
	
	/*
		二次宏展开
	*/
	if ( m_dwFlag & DEF_ENV_VAR )
	{
		LPTSTR lpCmdBuff = ExpandMacro2(lpCmdBegin);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);
			
			delete lpCmdBuff;
			lpCmdBuff = NULL;

			if ( bResult )
			{
				goto _Exit;
			}
		}
	}

	/*
		在父程序目录下查找
	*/
	if ( lpParentPath != NULL )
	{
		if ( MyGetLongPathName(lpParentPath, m_szCmd, MAX_PATH) != 0 )
		{
			LPTSTR lpFileName = (LPTSTR)get_file_name(m_szCmd);
			*lpFileName = 0;
			DWORD dwLen2 = (DWORD)_tcslen(m_szCmd);

			LPTSTR lpCmdBuff = new TCHAR[dwLen2 + _tcslen(lpCmdBegin) + 2];
			if ( lpCmdBuff != NULL )
			{
				_tcscpy(lpCmdBuff, m_szCmd);
				if ( lpCmdBuff[dwLen2 - 1] != _T('\\') )
				{
					lpCmdBuff[dwLen2++] = _T('\\');
				}
				_tcscpy(lpCmdBuff + dwLen2, lpCmdBegin);
				//MessageBox(0, lpCmdBuff, NULL, 0);

				bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);
				
				delete lpCmdBuff;
				lpCmdBuff = NULL;

				if ( bResult )
				{
					goto _Exit;
				}
			}
		}
	}

	/*
	环境变量中查找
	*/
	if ( lpEnvVars != NULL || (m_dwFlag & DEF_ENV_VAR) )
	{
		LPCTSTR lpCmdEnd = NULL;

		if ( bQuotation )
		{
			lpCmdEnd = _tcschr(lpCmdBegin, _T('\"'));
			if ( lpCmdEnd == NULL )
			{
				goto _Exit;
			}
		}
		else
		{
			lpCmdEnd = skip_no_blank(lpCmdBegin);
		}

		if ( !fix_path_separator(m_szCmd, MAX_PATH, lpCmdBegin, lpCmdEnd - lpCmdBegin) )
		{
			goto _Exit;
		}

		if ( !is_relative_path(m_szCmd) )
		{
			bResult = ExpandEnvVars(m_szCmd, lpEnvVars, lpExtNames);
			if ( bResult )
			{
				ExpandParam(lpCmdEnd);
			}
		}
	}

_Exit:
	if ( !bResult )
	{
		m_szCmd[0] = 0;
	}

	return bResult;
}
Пример #14
0
template<class T> inline pure bool is_local_path(const T& path) { return not (is_absolute_path(path) or
                                                                                               is_relative_path(path)); }
Пример #15
0
FeShader *FePresent::add_shader( FeShader::Type type, const char *shader1, const char *shader2 )
{
    std::string path;
    m_feSettings->get_path( FeSettings::Current, path );

    std::string s1 = clean_path( shader1 );

    m_scriptShaders.push_back( new FeShader() );
    FeShader *sh = m_scriptShaders.back();

    if ( !is_relative_path( s1 ) )
        path.clear();

    switch ( type )
    {
    case FeShader::VertexAndFragment:
        if ( is_supported_archive( path ) )
        {
            //
            // Known Issue: We don't properly handle the
            // situation where one shader is specified as a
            // relative path and is in a zip, while the other
            // is specified as an absolute path.  If the first
            // is in a zip, the second is assumed to be in the
            // same zip as well.
            //
            FeZipStream zs1( path );
            zs1.open( shader1 );

            FeZipStream zs2( path );
            zs2.open( shader2 );

            sh->load( zs1, zs2 );
        }
        else
        {
            std::string path2 = path;
            std::string s2 = clean_path( shader2 );
            if ( !is_relative_path( s2 ) )
                path2.clear();

            sh->load( path + s1, path2 + s2 );
        }
        break;

    case FeShader::Vertex:
    case FeShader::Fragment:
        if ( is_supported_archive( path ) )
        {
            FeZipStream zs( path );
            zs.open( shader1 );

            sh->load( zs, type );
        }
        else
        {
            sh->load( path + s1, type );
        }
        break;

    case FeShader::Empty:
    default:
        break;
    }

    return sh;
}