Exemple #1
0
bool test_sf_file_write_fhandle(const char *filename, const int kib_rw) {
    bool result = true;
    FileHandle* file = sd.open(filename, O_WRONLY | O_CREAT | O_TRUNC);
    if (file != NULL) {
        int byte_write = 0;
        timer.start();
        for (int i = 0; i < kib_rw; i++) {
            if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
                result = false;
                file->close();
                printf("Write error!\r\n");
                break;
            } else {
                byte_write++;
            }
        }
        timer.stop();
        file->close();
        double test_time_sec = timer.read_us() / 1000000.0;
        double speed = kib_rw / test_time_sec;
        printf("%d KiB write in %.3f sec with speed of %.4f KiB/s\r\n", byte_write, test_time_sec, speed);
        notify_performance_coefficient("write_kibps", speed);
    } else {
        printf("File '%s' not opened\r\n", filename);
        result = false;
    }
    timer.reset();
    return result;
}
Exemple #2
0
void FileHandle::copyTo (FileHandle& dest) { 
    ifstream_ptr input;
    ofstream_ptr output;
    try {
        input = read();
        output = dest.write(false);
        char buffer[4096];
        while (true) {
            input->read(buffer, 4096);
            output->write(buffer, input->gcount());
            if(input->eof() || input->peek() == EOF) break;
        }
    } catch(std::runtime_error ex) {
        if(input->is_open()) input->close();
        if(output->is_open()) output->close();
        throw std::runtime_error("Error copying source file: " + path() + " (" + typetoString() + ")\n" + "To destination: " + dest.path() + " (" + dest.typetoString() + ")");
    }
   if(input->is_open()) input->close();
   if(output->is_open()) output->close();
}
void Space::Sync(Space* pSpace, FileHandle& pHandler)
{
  if (NULL == pSpace || !pHandler.isWritable())
    return;

  switch(pSpace->type()) {
    case Space::ALLOCATED_ARRAY: {
      if (!pHandler.write(pSpace->memory(),
                          pSpace->start(),
                          pSpace->size())) {
        error(diag::err_cannot_write_file) << pHandler.path()
                                           << pSpace->start()
                                           << pSpace->size();
      }
      return;
    }
    case Space::MMAPED:
    default: {
      // system will eventually write bakc the memory after
      // calling ::munmap
      return;
    }
  } // end of switch
}
Exemple #4
0
static u32_t ppp_output(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
{
    FileHandle *stream = my_stream;
    if (!stream) {
        return 0;
    }

    pollfh fhs;
    fhs.fh = stream;
    fhs.events = POLLOUT;

    // LWIP expects us to block on write
    // File handle will be in non-blocking mode, because of read events.
    // Therefore must use poll to achieve the necessary block for writing.

    uint32_t written = 0;
    while (len != 0) {
        // Block forever until we're selected - don't care about reason we wake;
        // return from write should tell us what's up.
        poll(&fhs, 1, -1);
        // This write will be non-blocking, but blocking would be fine.
        ssize_t ret = stream->write(data, len);
        if (ret == -EAGAIN) {
            continue;
        } else if (ret < 0) {
            break;
        }
        written += ret;
        data += ret;
        len -= ret;
    }

//    /tr_debug("> %ld bytes of data written\n", (long) written);

    return written;
}
Exemple #5
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());
	}
}
Exemple #6
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();
}