Add framework for ptb2abc
authorJelmer Vernooij <jelmer@samba.org>
Sun, 13 Mar 2005 04:23:34 +0000 (05:23 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 13 Mar 2005 04:23:34 +0000 (05:23 +0100)
ChangeLog
Makefile
configure.in
ptb2abc.c [new file with mode: 0644]
ptb2ascii.c
ptb2ly.c

index adf69069cf5bfc9ac332bfb5348d2adb3953ac0f..10edac17a5887dd1890106576f96e94bcfd0c7b5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@ All changes, unless specified otherwise, made by Jelmer Vernooij
 <jelmer@samba.org>.
 
 0.4.2:
+ * Add ptb2abc utility
  * Default to always printing errors
  * Allow overriding or disabling of printing function used for errors
        and warnings
index 74fc6eb30a1f77a92db096aa1baf3710057882ac..a21b53bf6c2de1be29486990402bb8a93fd414f5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,9 @@ ptb2ptb$(EXEEXT): ptb2ptb.o ptb.o
 ptb2ly$(EXEEXT): ptb2ly.o ptb.o
        $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(POPT_LIBS)
 
+ptb2abc$(EXEEXT): ptb2abc.o ptb.o
+       $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(POPT_LIBS)
+
 gp2ly$(EXEEXT): gp2ly.o gp.o
        $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) $(POPT_LIBS)
 
index 4e46123a1cf1353a56520aa9ea745308b1b64030..f9db20a1f41a174e52e4917b4f80c401b7d0674a 100644 (file)
@@ -21,7 +21,7 @@ AC_SUBST(POPT_LIBS)
 # Checks for libraries.
 AC_CHECK_LIB([popt], [poptGetArg], [ 
          POPT_LIBS="-lpopt"
-         TARGET_BINS="$TARGET_BINS ptbdict$EXEEXT ptb2ly$EXEEXT ptb2ascii$EXEEXT ptbinfo$EXEEXT gp2ly$EXEEXT" 
+         TARGET_BINS="$TARGET_BINS ptbdict$EXEEXT ptb2ly$EXEEXT ptb2ascii$EXEEXT ptbinfo$EXEEXT gp2ly$EXEEXT ptb2abc$EXEEXT
          ] , AC_MSG_WARN([Popt is required for command-line utilities]))
 PKG_CHECK_MODULES(LIBXML, libxml-2.0, [
 if test $ac_cv_lib_popt_poptGetArg = yes; then  
diff --git a/ptb2abc.c b/ptb2abc.c
new file mode 100644 (file)
index 0000000..0bfc570
--- /dev/null
+++ b/ptb2abc.c
@@ -0,0 +1,177 @@
+/*
+       (c) 2005: Jelmer Vernooij <jelmer@samba.org>
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <stdio.h>
+#include <errno.h>
+#include <popt.h>
+#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#ifdef HAVE_STDINT_H
+#  include <stdint.h>
+#endif
+
+#include "ptb.h"
+
+void abc_write_header(FILE *out, struct ptbf *ret) 
+{
+       if(ret->hdr.classification == CLASSIFICATION_SONG) {
+               if(ret->hdr.class_info.song.title)      fprintf(out, "T: %s\n", ret->hdr.class_info.song.title);
+               if(ret->hdr.class_info.song.music_by) fprintf(out, "C: %s\n", ret->hdr.class_info.song.music_by);
+               if(ret->hdr.class_info.song.words_by) fprintf(out, "%%  Words By: %s\n", ret->hdr.class_info.song.words_by);
+               if(ret->hdr.class_info.song.copyright) fprintf(out, "%%  Copyright: %s\n", ret->hdr.class_info.song.copyright);
+               if(ret->hdr.class_info.song.guitar_transcribed_by) fprintf(out, "Z: %s\n", ret->hdr.class_info.song.guitar_transcribed_by);
+               if(ret->hdr.class_info.song.release_type == RELEASE_TYPE_PR_AUDIO &&
+                  ret->hdr.class_info.song.release_info.pr_audio.album_title) fprintf(out, "%%  Album Title: %s\n", ret->hdr.class_info.song.release_info.pr_audio.album_title);
+       } else if(ret->hdr.classification == CLASSIFICATION_LESSON) {
+               if(ret->hdr.class_info.lesson.title)    fprintf(out, "T:%s\n", ret->hdr.class_info.lesson.title);
+               if(ret->hdr.class_info.lesson.artist) fprintf(out, "C: %s\n", ret->hdr.class_info.lesson.artist);
+               if(ret->hdr.class_info.lesson.author) fprintf(out, "Z: %s\n", ret->hdr.class_info.lesson.author);
+               if(ret->hdr.class_info.lesson.copyright) fprintf(out, "%%  Copyright: %s\n", ret->hdr.class_info.lesson.copyright);
+       }
+       fprintf(out, "\n");
+}
+
+void abc_write_chordtext(FILE *out, struct ptb_chordtext *name) {
+       fprintf(out, "\"");
+
+       if(name->properties & CHORDTEXT_PROPERTY_NOCHORD) {
+               fprintf(out, "N.C.");
+       }
+
+       if(name->properties & CHORDTEXT_PROPERTY_PARENTHESES) {
+               fprintf(out, "(");
+       }
+
+       if(!(name->properties & CHORDTEXT_PROPERTY_NOCHORD) | 
+          (name->properties & CHORDTEXT_PROPERTY_PARENTHESES)) {
+               if(name->name[0] == name->name[1]) {
+                       fprintf(out, "%s", ptb_get_tone(name->name[0]));
+               } else { 
+                       fprintf(out, "%s/%s", ptb_get_tone(name->name[0]),
+                                               ptb_get_tone(name->name[1]));
+               }
+       }
+
+       if(name->properties & CHORDTEXT_PROPERTY_PARENTHESES) {
+               fprintf(out, ")");
+       }
+
+       fprintf(out, "\" ");
+}
+
+int abc_write_lyrics(FILE *out, struct ptbf *ret)
+{
+       if(ret->hdr.classification != CLASSIFICATION_SONG || !ret->hdr.class_info.song.lyrics) return 0;
+       fprintf(out, "W: %s\n\n", ret->hdr.class_info.song.lyrics);
+       return 1;
+}
+
+int main(int argc, const char **argv) 
+{
+       FILE *out;
+       struct ptbf *ret;
+       int debugging = 0;
+       struct ptb_section *section;
+       int instrument = 0;
+       int c;
+       int version = 0;
+       const char *input;
+       char *output = NULL;
+       poptContext pc;
+       struct poptOption options[] = {
+               POPT_AUTOHELP
+               {"debug", 'd', POPT_ARG_NONE, &debugging, 0, "Turn on debugging output" },
+               {"outputfile", 'o', POPT_ARG_STRING, &output, 0, "Write to specified file", "FILE" },
+               {"regular", 'r', POPT_ARG_NONE, &instrument, 0, "Write tabs for regular guitar" },
+               {"bass", 'b', POPT_ARG_NONE, &instrument, 1, "Write tabs for bass guitar"},
+               {"version", 'v', POPT_ARG_NONE, &version, 'v', "Show version information" },
+               POPT_TABLEEND
+       };
+
+       pc = poptGetContext(argv[0], argc, argv, options, 0);
+       poptSetOtherOptionHelp(pc, "file.ptb");
+       while((c = poptGetNextOpt(pc)) >= 0) {
+               switch(c) {
+               case 'v':
+                       printf("ptb2abc Version "PACKAGE_VERSION"\n");
+                       printf("(C) 2005 Jelmer Vernooij <jelmer@samba.org>\n");
+                       exit(0);
+                       break;
+               }
+       }
+                       
+       ptb_set_debug(debugging);
+       
+       if(!poptPeekArg(pc)) {
+               poptPrintUsage(pc, stderr, 0);
+               return -1;
+       }
+       input = poptGetArg(pc);
+       ret = ptb_read_file(input);
+       
+       if(!ret) {
+               perror("Read error: ");
+               return -1;
+       } 
+
+       if(!output) {
+               int baselength = strlen(input);
+               if (!strcmp(input + strlen(input) - 4, ".ptb")) {
+                       baselength -= 4;
+               }
+               output = malloc(baselength + 6);
+               strncpy(output, input, baselength);
+               strcpy(output + baselength, ".abc");
+       }
+
+       if(!strcmp(output, "-")) {
+               out = stdout;
+       } else {
+               out = fopen(output, "w+");
+               if(!out) {
+                       perror("open");
+                       return -1;
+               }
+       } 
+       
+       fprintf(out, "%% Generated by ptb2abc (C) 2005 Jelmer Vernooij <jelmer@samba.org>\n");
+       fprintf(out, "%% See http://jelmer.vernstok.nl/oss/ptabtools/ for more info\n\n");
+       fprintf(out, "X:1\n");
+               
+       abc_write_header(out, ret);
+       abc_write_lyrics(out, ret);
+
+       fprintf(out, "M: C\n");
+       fprintf(out, "K: Cm\n");
+       fprintf(out, "L: 1/4\n");
+
+       section = ret->instrument[instrument].sections;
+       while(section) {
+               /*abc_write_section(out, section);*/
+               fprintf(out, "\n\n");
+               section = section->next;
+       }
+
+       if(output)fclose(out);
+       
+       return (ret?0:1);
+}
index 200cb09bd442ae7a740442dfe370142086ae381c..4ca3c4929603cbc5d9582534a0b3543d32b4311e 100644 (file)
@@ -228,7 +228,7 @@ int main(int argc, const char **argv)
                }
        } 
        
-       fprintf(out, "Generated by ptb2ly (C) 2004 Jelmer Vernooij <jelmer@samba.org>\n");
+       fprintf(out, "Generated by ptb2ascii (C) 2004 Jelmer Vernooij <jelmer@samba.org>\n");
        fprintf(out, "See http://jelmer.vernstok.nl/oss/ptabtools/ for more info\n\n");
                
        ascii_write_header(out, ret);
index 5928db43b797f3bb0bddbcce84ba514213c51f20..c504ad73060a34ede8191360282eb72c2fa047ea 100644 (file)
--- a/ptb2ly.c
+++ b/ptb2ly.c
@@ -1,5 +1,5 @@
 /*
-       (c) 2004: Jelmer Vernooij <jelmer@samba.org>
+       (c) 2004-2005: Jelmer Vernooij <jelmer@samba.org>
 
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
@@ -623,7 +623,7 @@ int main(int argc, const char **argv)
                }
        }
        
-       fprintf(out, "%% Generated by ptb2ly (C) 2004 Jelmer Vernooij <jelmer@samba.org>\n");
+       fprintf(out, "%% Generated by ptb2ly (C) 2004-2005 Jelmer Vernooij <jelmer@samba.org>\n");
        fprintf(out, "%% See http://jelmer.vernstok.nl/oss/ptabtools/ for more info\n\n");
        fprintf(out, "\\version \""LILYPOND_VERSION"\"\n");
                
@@ -665,7 +665,6 @@ int main(int argc, const char **argv)
        }
 
        /* Do the main typesetting */
-
        if (!singlepiece) {
                /* typeset using \book */