Source

libtaginfo / tests / write_image_speex / main.cc

Full commit
#include "taginfo.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace TagInfo;


int main( void ) {
    Info * info;
    std::string val   = TESTDIR "samples/sample_empty.spx";
    std::string image = TESTDIR "samples/test.jpg";
    
    std::string target = "/tmp/out_01.spx";
    //std::cout << "##1: " << std::endl;
    std::ifstream  src(val.c_str());
    std::ofstream  dst(target.c_str());
    dst << src.rdbuf();
    

    Image * img = new Image();
    img->image_file_type = IMAGE_FILE_TYPE_JPEG;
    img->image_type      = IMAGE_TYPE_OTHER;
    img->description     = ::strdup(String("Bescräibunk").toCString(false));

    Image * img2 = new Image();
    img2->image_file_type = IMAGE_FILE_TYPE_JPEG;
    img2->image_type      = IMAGE_TYPE_COVER_FRONT;
    img2->description     = ::strdup(String("Beßcräibunk").toCString(false));
    
    struct stat filestatus;
    stat( image.c_str(), &filestatus );
    //cout << filestatus.st_size << " bytes\n";
    img->data_length  = filestatus.st_size;
    img2->data_length = filestatus.st_size;
    int fd;
    fd = open(image.c_str(), O_RDONLY);
    if (fd == -1) {
        perror("Error opening file for reading");
        EXIT_FAILURE;
    }
    
    img->data = (char*)mmap(0, img->data_length, PROT_READ, MAP_SHARED, fd, 0);
    img2->data = (char*)mmap(0, img2->data_length, PROT_READ, MAP_SHARED, fd, 0);
    if(img->data == MAP_FAILED || img2->data == MAP_FAILED) {
        close(fd);
        perror("Error mmapping the file");
        EXIT_FAILURE;
    }
    
    info = Info::create(target);
    if(info) {
        Image ** imgs = new Image*[2];
        imgs[0] = img;
        imgs[1] = img2;
        info->set_images((const Image **)imgs, 2);
        info->save();
    }
    delete info;
    info = NULL;
    
    info = Info::create(target);
    Image * read_image = NULL;
    Image * read_image2 = NULL;
    
    if(info) {
        if(info->load()) {
            int len = 0;
            Image ** images = info->get_images(len);
            if(!images || len != 2) {
                delete info;
                if(remove(target.c_str()) != 0 ) {
                    return EXIT_FAILURE;
                }
                return EXIT_FAILURE;
            }
            read_image = images[0];
            read_image2 = images[1];
            if(!images[0] || !read_image|| !read_image2) {
                delete info;
                if(remove(target.c_str()) != 0 ) {
                    if(img)
                        delete img;
                    if(img2)
                        delete img2;
                    return EXIT_FAILURE;
                }
            }
            int i;
            if(img->image_type != read_image->image_type) {
                if(images)
                    delete [] images;
                if(read_image)
                    delete read_image;
                return EXIT_FAILURE;
            }
            String st = String(read_image->description);
            if(strcmp(img->description, st.toCString(false)) != 0) {
                if(images)
                    delete [] images;
                if(read_image)
                    delete read_image;
                return EXIT_FAILURE;
            }
            st = String(read_image2->description);
            if(strcmp(img2->description, st.toCString(false)) != 0) {
                if(images)
                    delete [] images;
                if(read_image)
                    delete read_image;
                return EXIT_FAILURE;
            }
            if(img2->image_type != read_image2->image_type) {
                if(images)
                    delete [] images;
                if(read_image)
                    delete read_image;
                return EXIT_FAILURE;
            }
            for(i = 0; i<img->data_length; i++) {
                if(img->data[i] != read_image->data[i]) {
                    if(images)
                        delete [] images;
                    if(read_image)
                        delete read_image;
                    return EXIT_FAILURE;
                }
            }
            for(i = 0; i<img2->data_length; i++) {
                if(img2->data[i] != read_image->data[i]) {
                    if(images)
                        delete [] images;
                    if(read_image2)
                        delete read_image;
                    return EXIT_FAILURE;
                }
            }
            if(images)
                delete [] images;
            if(remove(target.c_str()) != 0 )
                return EXIT_FAILURE;
            return EXIT_SUCCESS;
        }
        delete info;
        if(remove(target.c_str()) != 0 )
            return EXIT_FAILURE;
        return EXIT_FAILURE;
    }
    else {
       delete info;
        if(remove(target.c_str()) != 0 )
            return EXIT_FAILURE;
        return EXIT_FAILURE;
    }
    return EXIT_FAILURE;
}