|
| 1 | +#include <getopt.h> |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +/* TagLib */ |
| 7 | +#include <aifffile.h> |
| 8 | +#include <id3v2header.h> |
| 9 | +#include <tag.h> |
| 10 | +#include <fileref.h> |
| 11 | +#include <tbytevector.h> |
| 12 | + |
| 13 | +/* LibJson */ |
| 14 | +#include <json/json.h> |
| 15 | + |
| 16 | + |
| 17 | +static struct option long_options[] = { |
| 18 | + {"help", no_argument, 0, 'h'}, |
| 19 | + {"file", required_argument, 0, 'f'}, |
| 20 | + {"tags", required_argument, 0, 't'}, |
| 21 | + {0, 0, 0, 0} |
| 22 | +}; |
| 23 | + |
| 24 | +string read_file(string file_name) |
| 25 | +{ |
| 26 | + ifstream input(file_name.c_str()); |
| 27 | + string line; |
| 28 | + string contents; |
| 29 | + if (input.is_open()) { |
| 30 | + while (input.good()) { |
| 31 | + getline(input, line); |
| 32 | + contents += line; |
| 33 | + } |
| 34 | + input.close(); |
| 35 | + } else { |
| 36 | + cerr << "File not found: " << file_name << endl; |
| 37 | + } |
| 38 | + return contents; |
| 39 | +} |
| 40 | + |
| 41 | +void usage(char **argv) |
| 42 | +{ |
| 43 | + cerr << "Usage: ./" << *argv << " -h --help -f --file [file] -t --tags [file]" << endl; |
| 44 | +} |
| 45 | + |
| 46 | +int main(int argc, char **argv) |
| 47 | +{ |
| 48 | + setlocale(LC_ALL, ""); |
| 49 | + int return_code; |
| 50 | + int option_index = 0; |
| 51 | + string file_name; |
| 52 | + string tag_file; |
| 53 | + |
| 54 | + while(42) { |
| 55 | + return_code = getopt_long(argc, argv, "hf:o:a:", long_options, |
| 56 | + &option_index); |
| 57 | + if(return_code == -1) |
| 58 | + break; |
| 59 | + |
| 60 | + switch(return_code) { |
| 61 | + case 'h': /* --help */ |
| 62 | + usage(argv); |
| 63 | + return 0; |
| 64 | + case 'f': /* --file*/ |
| 65 | + cout << "Tagging file: " << optarg << endl; |
| 66 | + file_name = optarg; |
| 67 | + break; |
| 68 | + case 't': /* --file*/ |
| 69 | + cout << "Using tag file: " << optarg << endl; |
| 70 | + tag_file = optarg; |
| 71 | + break; |
| 72 | + default: /* ??? */ |
| 73 | + usage(argv); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Json::Value root; // will contains the root value after parsing. |
| 79 | + Json::Reader reader; |
| 80 | + |
| 81 | + //string json_tags = read_file(tag_file); |
| 82 | + //json::Value *v = json::parse(json_tags); |
| 83 | + //json::Object *obj = dynamic_cast<json::Object *>(v); |
| 84 | + |
| 85 | + //for (json::Object::const_iterator i = obj->begin(); i != obj->end(); ++i) { |
| 86 | + //if (i.key() == "tags") { |
| 87 | + //json::Object tags_obj = dynamic_cast<const json::Object &>(i.value()); |
| 88 | + //for (json::Object::const_iterator j = tags_obj.begin(); j != tags_obj.end(); ++j) { |
| 89 | + //if (j->type() == json::TYPE_STRING) { |
| 90 | + //string tag_value = j.key() + " => " + dynamic_cast<const json::String &>(*j).value(); |
| 91 | + //cout << tag_value << endl; |
| 92 | + //} |
| 93 | + //} |
| 94 | + //} |
| 95 | + //cout << i.key() << endl; |
| 96 | + //} |
| 97 | + |
| 98 | + |
| 99 | + TagLib::FileRef f(file_name.c_str()); |
| 100 | + f.tag()->setAlbum("Album"); |
| 101 | + f.tag()->setArtist("Foobar"); |
| 102 | + f.save(); |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
0 commit comments