~njh-aelius/maxosx/musicbrainz-tags

« back to all changes in this revision

Viewing changes to Frameworks/taglib/taglib/examples/tagwriter.cpp

  • Committer: stephen_booth
  • Date: 2008-04-30 01:48:01 UTC
  • Revision ID: svn-v4:6b6cea13-1402-0410-9567-a7afb52bf336:trunk:1371
Fixing the taglib source tree

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2004 Scott Wheeler <wheeler@kde.org>
2
 
 *
3
 
 * Redistribution and use in source and binary forms, with or without
4
 
 * modification, are permitted provided that the following conditions
5
 
 * are met:
6
 
 *
7
 
 * 1. Redistributions of source code must retain the above copyright
8
 
 *    notice, this list of conditions and the following disclaimer.
9
 
 * 2. Redistributions in binary form must reproduce the above copyright
10
 
 *    notice, this list of conditions and the following disclaimer in the
11
 
 *    documentation and/or other materials provided with the distribution.
12
 
 *
13
 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14
 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16
 
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17
 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18
 
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19
 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20
 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
 
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
 */
24
 
 
25
 
#include <iostream>
26
 
#include <string.h>
27
 
 
28
 
#include <stdio.h>
29
 
#include <sys/types.h>
30
 
#include <sys/stat.h>
31
 
#include <unistd.h>
32
 
#include <stdlib.h>
33
 
 
34
 
#include <tlist.h>
35
 
#include <fileref.h>
36
 
#include <tfile.h>
37
 
#include <tag.h>
38
 
 
39
 
using namespace std;
40
 
 
41
 
bool isArgument(const char *s)
42
 
{
43
 
  return strlen(s) == 2 && s[0] == '-';
44
 
}
45
 
 
46
 
bool isFile(const char *s)
47
 
{
48
 
  struct stat st;
49
 
  return ::stat(s, &st) == 0 && (st.st_mode & (S_IFREG | S_IFLNK));
50
 
}
51
 
 
52
 
void usage()
53
 
{
54
 
  cout << endl;
55
 
  cout << "Usage: tagwriter <fields> <files>" << endl;
56
 
  cout << endl;
57
 
  cout << "Where the valid fields are:" << endl;
58
 
  cout << "  -t <title>"   << endl;
59
 
  cout << "  -a <artist>"  << endl;
60
 
  cout << "  -A <album>"   << endl;
61
 
  cout << "  -c <comment>" << endl;
62
 
  cout << "  -g <genre>"   << endl;
63
 
  cout << "  -y <year>"    << endl;
64
 
  cout << "  -T <track>"   << endl;
65
 
  cout << endl;
66
 
 
67
 
  exit(1);
68
 
}
69
 
 
70
 
int main(int argc, char *argv[])
71
 
{
72
 
  TagLib::List<TagLib::FileRef> fileList;
73
 
 
74
 
  while(argc > 0 && isFile(argv[argc - 1])) {
75
 
 
76
 
    TagLib::FileRef f(argv[argc - 1]);
77
 
 
78
 
    if(!f.isNull() && f.tag())
79
 
      fileList.append(f);
80
 
 
81
 
    argc--;
82
 
  }
83
 
 
84
 
  if(fileList.isEmpty())
85
 
    usage();
86
 
 
87
 
  for(int i = 1; i < argc - 1; i += 2) {
88
 
 
89
 
    if(isArgument(argv[i]) && i + 1 < argc && !isArgument(argv[i + 1])) {
90
 
 
91
 
      char field = argv[i][1];
92
 
      TagLib::String value = argv[i + 1];
93
 
 
94
 
      TagLib::List<TagLib::FileRef>::Iterator it;
95
 
      for(it = fileList.begin(); it != fileList.end(); ++it) {
96
 
 
97
 
        TagLib::Tag *t = (*it).tag();
98
 
 
99
 
        switch (field) {
100
 
        case 't':
101
 
          t->setTitle(value);
102
 
          break;
103
 
        case 'a':
104
 
          t->setArtist(value);
105
 
          break;
106
 
        case 'A':
107
 
          t->setAlbum(value);
108
 
          break;
109
 
        case 'c':
110
 
          t->setComment(value);
111
 
          break;
112
 
        case 'g':
113
 
          t->setGenre(value);
114
 
          break;
115
 
        case 'y':
116
 
          t->setYear(value.toInt());
117
 
          break;
118
 
        case 'T':
119
 
          t->setTrack(value.toInt());
120
 
          break;
121
 
        default:
122
 
          usage();
123
 
          break;
124
 
        }
125
 
      }
126
 
    }
127
 
    else
128
 
      usage();
129
 
  }
130
 
 
131
 
  TagLib::List<TagLib::FileRef>::Iterator it;
132
 
  for(it = fileList.begin(); it != fileList.end(); ++it)
133
 
    (*it).file()->save();
134
 
 
135
 
  return 0;
136
 
}