Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Mutoh

Pages: 1 2 [3]
31
System / Problems with making my own InputStream
« on: November 17, 2013, 04:11:11 am »
Apparently, searching the forum and the internet for help regarding derivations of sf::InputStream was fruitless, so hello! I've just registered, to ask some help for making my own stream class. :)

I tried following the tutorial here: http://www.sfml-dev.org/tutorials/2.0/system-stream.php

My class is basically a copy and paste of it, and I want to simply be able to read and decrypt my own encrypted files. As of now, though, it does nothing in special, I have left the encrypting methods empty for testing. Problem is: I haven't been able to load anything with it. Every time I try to call a loadFromStream from a sf::Font or sf::Texture, be it for a .jpg or a .png on the latter, the application crashes. What could be it? Is there already a working sf::InputStream derivation out there?

Here are my codes, you can even try to to compile them by yourself. No custom libraries and all. I wonder if it's a problem of my machine?

#pragma once

// "EncryptedStream.h"
// ---

#include <string>
#include <cstdio>
#include <stdexcept>

#include <SFML/System/InputStream.hpp>

namespace ds {

        class FileCouldntOpen : public std::runtime_error {
                public:
                        FileCouldntOpen(const std::string& filePath);

                        const std::string& filePath() const;

                private:
                        std::string m_filePath;
        };

        // ---

        void encryptFile (const std::string& inputPath, const std::string& outputPath);

        class EncryptedStream : public sf::InputStream {
                public:
                        EncryptedStream();
                        EncryptedStream(const EncryptedStream&) =delete;
                        EncryptedStream(const std::string& filePath);

                        ~EncryptedStream();

                        void open (const std::string& filePath);
                        void close();

                        sf::Int64 read (void*, sf::Int64);
                        sf::Int64 seek (sf::Int64 position);
                        sf::Int64 tell();
                        sf::Int64 getSize();

                private:
                        void decrypt (char*, int);

                        std::FILE* m_file;
        };

}
 

#include <do/EncryptedStream.h>

// "EncryptedStream.cpp"
// ---

namespace ds {

        FileCouldntOpen::FileCouldntOpen(const std::string& filePath)
                : m_filePath(filePath),
                  std::runtime_error("Couldn't open " + filePath)
        {       }

        const std::string& FileCouldntOpen::filePath() const {
                return m_filePath;
        }

        // ---

        EncryptedStream::EncryptedStream()
                : m_file(nullptr)
        {       }

        EncryptedStream::EncryptedStream
                (const std::string& filePath)
        {
                open(filePath);
        }

        void EncryptedStream::open (const std::string& filePath) {
                if (m_file)
                        std::fclose(m_file);

                m_file = std::fopen(filePath.c_str(), "rb");

                if (!m_file)
                        throw FileCouldntOpen(filePath);
        }

        void EncryptedStream::close() {
                if (m_file)
                        std::fclose(m_file);
        }

        void encrypt (const std::string& inputPath, const std::string& outputPath) {
                // ...
        }

        void EncryptedStream::decrypt (char* data, int size) {
                // ...
        }

        // ---

        sf::Int64 EncryptedStream::read (void* data, sf::Int64 size) {
                if (m_file) {
                        sf::Int64 result = std::fread(data, 1, static_cast<std::size_t>(size), m_file);
                        decrypt(static_cast<char*>(data), size);

                        return result;
                }
                else {
                        return -1;
                }
        }

        sf::Int64 EncryptedStream::seek (sf::Int64 position) {
                if (m_file) {
                        std::fseek(m_file, static_cast<std::size_t>(position), SEEK_SET);
                        return tell();
                }
                else {
                        return -1;
                }
        }

        sf::Int64 EncryptedStream::tell() {
                if (m_file)
                        return std::ftell(m_file);
                else
                        return -1;
        }

        sf::Int64 EncryptedStream::getSize() {
                if (m_file) {
                        sf::Int64 position = tell();
                        std::fseek(m_file, 0, SEEK_END);
                        sf::Int64 size = tell();
                        seek(position);
                        return size;
                }
                else {
                        return -1;
                }
        }

        // ---

        EncryptedStream::~EncryptedStream() {
                if (m_file)
                        std::fclose(m_file);
        }

}

 

Pages: 1 2 [3]