1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <utility>
5 #include "dumptag.h"
6
7 /*
8
9 serializing tag info to/from human-readable text
10
11 copyright (c) 2015 squell <squell@alumina.nl>
12
13 use, modification, copying and distribution of this software is permitted
14 under the conditions described in the file 'COPYING'.
15
16 */
17
18 namespace tag {
19
20 using namespace std;
21
22 #define DIRECTIVE "#"
23 #define COMPACT false
24
output(metadata::array::const_iterator begin,metadata::array::const_iterator end,FILE * out)25 void output(metadata::array::const_iterator begin, metadata::array::const_iterator end, FILE* out)
26 {
27 while(begin < end) {
28 const pair<string,string> line = *begin++;
29 const char* key = line.first.c_str();
30 const char* value = line.second.c_str();
31 const char* lnbrk = strchr(value, '\n');
32 if(COMPACT && !lnbrk && !strchr(key, ':')) {
33 // emit one a single line
34 fprintf(out, "{%s}\t%s\n", key, value);
35 } else {
36 // emit as header-paragraph
37 fprintf(out, "{%s}\n", key);
38 while(lnbrk) {
39 int const span = (lnbrk - value) & 0xFFFF;
40 fprintf(out, "\t%.*s\n", (int)span, value);
41 value = lnbrk+1;
42 lnbrk = strchr(value, '\n');
43 }
44 fprintf(out, "\t%s\n", value);
45 }
46 }
47 }
48
output(combined<reader> const & tags,const char * filename,FILE * out)49 void output(combined<reader> const& tags, const char* filename, FILE* out)
50 {
51 fprintf(out, DIRECTIVE"file\t%s\n", filename);
52 combined<reader>::const_iterator p = tags.begin();
53 while( p != tags.end() ) {
54 std::auto_ptr<metadata> info( (*p++)->read(filename) );
55 if(info.get() && *info) {
56 metadata::array list = info->listing();
57 const pair<string,string> hdr = list[0];
58 fprintf(out, DIRECTIVE"tag\t%s %s\n", hdr.first.c_str(), hdr.second.c_str());
59 output(list.begin()+1, list.end(), out);
60 }
61 }
62 fprintf(out, "\n");
63 if(ferror(out))
64 throw failure("could not emit tag data");
65 }
66
67 }
68