libtaginfo / tests / write_image_mp4 / main.cc

#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.m4a";
    std::string image = TESTDIR "samples/test.jpg";
    
    std::string target = "/tmp/out_01.m4a";
    
    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;
    }
    
    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;
                }
            }
            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;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.