Ejemplo n.º 1
0
/** Copies this file or directory to the specified file or directory. If this handle is a file, then 1) if the destination is a
* file, it is overwritten, or 2) if the destination is a directory, this file is copied into it, or 3) if the destination
* doesn't exist, {@link #mkdirs()} is called on the destination's parent and this file is copied into it with a new name. If
* this handle is a directory, then 1) if the destination is a file, GdxRuntimeException is thrown, or 2) if the destination is
* a directory, this directory is copied recursively into it as a subdirectory, overwriting existing files, or 3) if the
* destination doesn't exist, {@link #mkdirs()} is called on the destination and this directory is copied recursively into it
* as a subdirectory.
* @throw GdxRuntimeException if the destination file handle is a {@link FileType#Classpath} or {@link FileType#Internal} file,
*        or copying failed. */
void FileHandle::copyTo(const FileHandle& destination) const
{
	/*
	boolean sourceDir = isDirectory();
	if (!sourceDir) {
	if (dest.isDirectory()) dest = dest.child(name());
	copyFile(this, dest);
	return;
	}
	if (dest.exists()) {
	if (!dest.isDirectory()) throw GdxRuntimeException("Destination exists but is not a directory: " + dest);
	} else {
	dest.mkdirs();
	if (!dest.isDirectory()) throw GdxRuntimeException("Destination directory cannot be created: " + dest);
	}
	if (!sourceDir) dest = dest.child(name());
	copyDirectory(this, dest);
	*/
	FileHandle dest = destination;
	bool sourceIsDir = isDirectory();
	if (!sourceIsDir) 
	{
		if (dest.isDirectory()) 
		{
			dest = dest.child(name());
		}
		copyFile(*this, dest);
		return;
	}

	if (dest.exists()) 
	{
		if (!dest.isDirectory()) 
			throw GdxRuntimeException("Destination exists but is not a directory: " + dest.toString());
	} 
	else 
	{
		dest.mkdirs();
		if (!dest.isDirectory()) 
			throw GdxRuntimeException("Destination directory cannot be created: " + dest.toString());
	}

	if (!sourceIsDir)
	{
		dest = dest.child(name());
	}
	copyDirectory(*this, dest);
}
Ejemplo n.º 2
0
void FileHandle::copyDirectory(const FileHandle& sourceDir, const FileHandle& destDir) 
{
	destDir.mkdirs();
	vector<FileHandle> files;
	sourceDir.list(files);
	for (int i = 0, n = (int)files.size(); i < n; i++) 
	{
		FileHandle srcFile = files[i];
		FileHandle destFile = destDir.child(srcFile.name());
		if (srcFile.isDirectory())
			copyDirectory(srcFile, destFile);
		else
			copyFile(srcFile, destFile);
	}
}
Ejemplo n.º 3
0
void FilesTest::testAbsolute()
{
	std::string path; 
	Gdx.files->getExternalStoragePath(path);
	path += "/meow";

	FileHandle handle = Gdx.files->absoluteHandle(path);
	handle.remove();
	if(handle.exists()) 
		fail();
	if(handle.isDirectory()) 
		fail();
	if(handle.remove()) 
		fail();

	std::vector<FileHandle> handles;
	handle.list(handles);
	if(handles.size() != 0)
		fail();

	if(handle.child("meow").exists()) 
		fail();

	if(!handle.parent().exists()) 
		fail();
	try 
	{
		std::ifstream in;
		handle.read(in);
		in.close();
		fail();
	}
	catch(GdxRuntimeException ignored)
	{
	}
	handle.mkdirs();
	if(!handle.exists()) 
		fail();
	if(!handle.isDirectory()) 
		fail();
	handle.list(handles);
	if(handles.size() != 0)
		fail();

	handle.child("meow").mkdirs();

	handle.list(handles);
	if(handles.size() != 1) 
		fail();
	
	FileHandle child = handles[0];
	if(child.name() != "meow")
		fail();
	
	if(!child.parent().exists()) 
		fail();
	
	if(!handle.removeRecursive()) 
		fail();
	
	
	if(handle.exists()) 
		fail();
	
	std::ofstream output;
	handle.write(false, output);
	output << "moo";
	output.close();
	if(!handle.exists()) 
		fail();

	if(handle.length() != 3) 
		fail();
	
	FileHandle copy = Gdx.files->absoluteHandle(path + "-copy");
	copy.remove();
	if(copy.exists()) 
		fail();
	
	handle.copyTo(copy);
	if(!copy.exists()) 
		fail();

	if(copy.length() != 3) 
		fail();
	
	FileHandle move = Gdx.files->absoluteHandle(path + "-move");
	move.remove();
	if(move.exists()) 
		fail();
	copy.moveTo(move);
	if(!move.exists()) 
		fail();
	if(move.length() != 3) 
		fail();
	move.removeRecursive();
	if(move.exists()) 
		fail();

	std::ifstream input;
	handle.read(input);

	char bytes[7];
	input.read(bytes, 3);
	bytes[3] = 0;
	if(strcmp("moo", bytes))
		fail();
	input.close();

	handle.write(true, output);
	output << "cow";
	output.close();
	if(handle.length() != 6) 
		fail();

	handle.readBytes((unsigned char*)bytes, 6);
	bytes[6] = 0;
	if(strcmp("moocow", bytes))
		fail();
	if(handle.isDirectory()) fail();
	
	std::vector<FileHandle> files;
	handle.list(files);
	if(files.size() != 0) 
		fail();

	if(handle.name() != "meow")
		fail();
	
	if(handle.nameWithoutExtension() != "meow")
		fail();
	if(handle.extension() != "")
		fail();
	
	handle.remove();

	if(handle.exists()) 
		fail();
	if(handle.isDirectory()) 
		fail();
	
	handle.remove();
	handle.removeRecursive();
}
Ejemplo n.º 4
0
void FilesTest::create()
{
	font = new BitmapFont();
	batch = new SpriteBatch();

	if(Gdx.files->isExternalStorageAvailable())
	{
		message += "External storage available\n";
		std::string externalStoragePath;
		Gdx.files->getExternalStoragePath(externalStoragePath);
		message += "External storage path: " + externalStoragePath + "\n";
		try
		{
			FileHandle cube = Gdx.files->internalHandle("data/cube.obj");
			std::ifstream in;
			cube.read(in);
			try
			{
				in.close();
			}
			catch(std::exception e)
			{
			}
			message += "Open internal success\n";
		}
		catch(std::exception e)
		{
			message += "Couldn't open internal data/cube.obj\n";
			message += e.what();
			message += "\n";
		}

		try
		{
			FileHandle testFile = Gdx.files->externalHandle("test.txt");
			std::ofstream testStream;
			testFile.write(false, testStream);
			testStream << "test";
			testStream.close();
			
			message += "Write external success\n";
		}
		catch(GdxRuntimeException ex)
		{
			message += "Couldn't open externalstorage/test.txt\n";
		}
		catch(std::exception e)
		{
			message += "Couldn't write externalstorage/test.txt\n";
		}
		
		try
		{
			FileHandle testFile = Gdx.files->externalHandle("test.txt");
			std::ifstream in;
			testFile.read(in);
			in.close();
			message += "Open external success\n";
		}
		catch(GdxRuntimeException e)
		{
			message += "Couldn't open internal externalstorage/test.txt\n";
		}

		FileHandle file = Gdx.files->externalHandle("test.txt");
		if(!file.remove())
		{
			message += "Couldn't delete externalstorage/test.txt";
		}

		file = Gdx.files->externalHandle("this/is/a/test");
		file.mkdirs();
		file.remove();

		if(!file.parent().remove())
			message += "failed to remove this/is/a/ directory";
		
		if(!file.parent().parent().parent().removeRecursive())
			message += "failed to remove this directory";
	}
	else
	{
		message += "External storage not available";
	}

	FileHandle tmp = FileHandle::tempFile();
	tmp.length();
	message += "Temp file created: " + tmp.toString() + "\n";
	tmp.remove();

	tmp = FileHandle::tempDirectory();
	if(!tmp.isDirectory())
		fail();
	message += "Temp directory created: " + tmp.toString() + "\n";
	tmp.remove();

	try
	{
		testInternal();
		testExternal();
		testAbsolute();
	}
	catch(std::exception ex)
	{
		throw GdxRuntimeException(ex.what());
	}
}
Ejemplo n.º 5
0
void FilesTest::testInternal()
{
	FileHandle handle = Gdx.files->internalHandle("data/badlogic.jpg");
	if(!handle.exists()) 
		fail();
	if(handle.isDirectory()) 
		fail();

	try 
	{
		handle.remove();
		fail();
	}
	catch(GdxRuntimeException expected)
	{
	}

	std::vector<FileHandle> files;
	handle.list(files);
	if(files.size() != 0)
		fail();
	if(!handle.parent().exists()) 
		fail();
	try 
	{
		std::ifstream input;
		handle.read(input);
		input.close();
		//TODO: why???
		fail();
	}
	catch(GdxRuntimeException ignored)
	{
	}

	FileHandle dir;
	dir = Gdx.files->internalHandle("data");

	if(!dir.exists()) 
		fail();
	if(!dir.isDirectory()) 
		fail();

	dir.list(files);
	if(files.size() == 0)
		fail();
	
	FileHandle child = dir.child("badlogic.jpg");
	if(child.name() != "badlogic.jpg")
		fail();

	if(child.nameWithoutExtension() != "badlogic")
		fail();

	if(child.extension() != "jpg") 
		fail();
	
	if(!child.parent().exists()) 
		fail();

	FileHandle copy = Gdx.files->externalHandle("badlogic.jpg-copy");
	copy.remove();
	if(copy.exists()) 
		fail();
	handle.copyTo(copy);

	if(!copy.exists()) 
		fail();
	if(copy.length() != 68465) 
		fail();
	copy.remove();
	
	if(copy.exists()) 
		fail();
}