Ejemplo n.º 1
0
std::tr1::shared_ptr<std::vector<char> > ImageServer::GetImage(std::string& category, uint32 id, uint32 size)
{
    if (!ValidateCategory(category) || !ValidateSize(category, size))
        return std::tr1::shared_ptr<std::vector<char> >();

    //std::ifstream stream;
    std::string path(GetFilePath(category, id, size));
    FILE * fp = fopen(path.c_str(), "rb");
    if (fp == NULL)
        return std::tr1::shared_ptr<std::vector<char> >();
    fseek(fp, 0, SEEK_END);
    size_t length = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    //stream.open(path, std::ios::binary | std::ios::in);
    // not found or other error
    //if (stream.fail())
    //    return std::tr1::shared_ptr<std::vector<char> >();

    // get length
    //stream.seekg(0, std::ios::end);
    //int length = stream.tellg();
    //stream.seekg(0, std::ios::beg);

    std::tr1::shared_ptr<std::vector<char> > ret = std::tr1::shared_ptr<std::vector<char> >(new std::vector<char>());
    ret->resize(length);

    // HACK
    //stream.read(&((*ret)[0]), length);
    fread(&((*ret)[0]), 1, length, fp);

    return ret;
}
Ejemplo n.º 2
0
std::shared_ptr<std::vector<char>> ImageServer::GetImage(std::string& category, uint32 id, uint32 size)
{
	if (!ValidateCategory(category) || !ValidateSize(category, size))
		return std::shared_ptr<std::vector<char>>();

	std::ifstream stream;
	std::string path(GetFilePath(category, id, size));
	stream.open(path, std::ios::binary | std::ios::in);
	// not found or other error
	if (stream.fail())
		return std::shared_ptr<std::vector<char>>();

	// get length
	stream.seekg(0, std::ios::end);
	int length = stream.tellg();
	stream.seekg(0, std::ios::beg);

	auto ret = std::shared_ptr<std::vector<char>>(new std::vector<char>());
	ret->resize(length);

	// HACK
	stream.read(&((*ret)[0]), length);

	return ret;
}
    void Consume(int *items, std::size_t &count) override
    {
        assert(count <= buffer_.Capacity());

        Log("\tConsumer is trying to get the lock...\n");

        SingleLock<decltype(mutex_)> lock(mutex_);
        while (buffer_.Empty())
            fill_.Wait(lock);

        count = std::min<std::size_t>(count, buffer_.Size());

        Log("\tConsumer gets the lock...\n");

        assert(ValidateSize());
        buffer_.PopFrontMany(items, count);

        Log("\tConsume ", count, " items.\n");

        empty_.NotifyOne();
    }
    void Produce(const int *items, std::size_t &count) override
    {
        assert(count <= buffer_.Capacity());

        Log("Producer is trying to get the lock...\n");

        SingleLock<decltype(mutex_)> lock(mutex_);
        while (buffer_.Full())
            empty_.Wait(lock);

        count = std::min<std::size_t>(count, buffer_.Reserve());

        Log("Producer gets the lock...\n");

        assert(ValidateSize());
        buffer_.PushBackMany(items, count);

        Log("Produce ", count, " items.\n");

        fill_.NotifyOne();
    }