Source

libtaginfo / tests / write_image_flac / 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.flac";
    std::string image = TESTDIR "samples/test.jpg";
    
    //std::cout << std::endl << "val: " << val << std::endl;
    std::string target = "/tmp/out_01.flac";
//    std::string val   = TESTDIR "samples/sample_v2_only.mp3";
//    std::string image = TESTDIR "samples/test.jpg";
//    
//    //std::cout << std::endl << "val: " << val << std::endl;
//    std::string target = "/tmp/out_01.mp3";
    
    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_COVER_FRONT;
    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_BACK;
    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;
    }
    
    //std::cout <<  "++1 " << std::endl;
    info = Info::create(target);
    if(info) {
        Image ** imgs = new Image*[2];
        imgs[0] = img;
        imgs[1] = img2;
        info->set_images((const Image **)imgs, 2);
        if(imgs)
            delete [] imgs;
        info->save();
    }
    delete info;
    info = NULL;
    
    info = Info::create(target);
    Image * read_image;
    Image * read_image2;
    if(info) {
        if(info->load()) {
            int len = 0;
            Image ** images = info->get_images(len);
            if(!images) {
                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(images)
                        delete [] images;
                    return EXIT_FAILURE;
                }
            }
            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;
            }
            int i;
            for(i = 0; i<img->data_length; i++) {
                if(img->data[i] != read_image->data[i]) {
                    if(images)
                        delete [] images;
                    return EXIT_FAILURE;
                }
            }
            for(i = 0; i<img2->data_length; i++) {
                if(img2->data[i] != read_image2->data[i]) {
                    if(images)
                        delete [] images;
                    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;
}