Update of /cvsroot/audacity/lib-src/portsmf
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv3308

Modified Files:
        allegro.cpp allegro.h allegrord.cpp allegrosmfrd.cpp 
        allegrosmfwr.cpp mfmidi.cpp trace.cpp 
Log Message:
1)  Updated to latest upstream SVN trunk
2)  Fixed memleak in Alg_atoms::~Alg_atoms()
3)  Fixed memleak in Serial_buffer::~Serial_buffer()
4)  Added a couple more #pragmas to quiet MSVC a bit
5)  Changed Serial_buffer::init_for_read() to allocate a new buffer and copy 
the original into it.  I did this because Serial_buffer::check_buffer() can 
reallocate the buffer to make it bigger.  However, the caller of 
init_for_read() doesn't know this happened and when the original buffer passed 
in is no longer valid and causes a crash when freed by the caller.
6)  Fixed memleak in Alg_track::~Alg_track()
7)  Fixed memleak in Alg_seq::~Alg_seq()  (the Alg_track destructor wasn't 
being called)
8)  Changed stream parameter to Alg_seq::smf_write() and Alg_smf_write::write() 
from ofstream to ostream.  This should allow writing to any of the various 
streams.
9)  Changed a few casts to bool to generate proper value instead.
10) Fixed a bug in Alq_track::unserialize_track() that only appeared in release 
builds (when assert wasn't defined).  Two problems existed with the first being 
that the ALGT header was only bypassed in debug builds since the get_char()s 
were done in the assertions.  The second problem was the the header needed to 
be skipped in Alg_seq::unserialize_seq() since there's two paths into 
unserialize_track() and one path already stripped the header, while the other 
did not.
11) Fixed memleak in Alg_tracks::~Alg_tracks
12) Reverted one of the recent changes to allegrord.cpp that used a Windows 
specific function
13) Got rid of a couple more MSVC warnings 

Patch sent to Roger...


Index: allegro.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/allegro.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- allegro.cpp 6 Aug 2008 17:57:54 -0000       1.4
+++ allegro.cpp 22 Jun 2009 08:48:07 -0000      1.5
@@ -1459,18 +1459,20 @@
 }
 
 
+#pragma warning(disable: 4800) // long to bool performance warning
+
 void Alg_seq::unserialize_seq()
 {
     ser_buf.check_input_buffer(28);
     long len = ser_buf.get_int32();
     assert(ser_buf.get_len() >= len);
     channel_offset_per_track = ser_buf.get_int32();
-    units_are_seconds = (bool) ser_buf.get_int32();
+    units_are_seconds = ser_buf.get_int32() != 0;
     beat_dur = ser_buf.get_double();
     real_dur = ser_buf.get_double();
-    time_map = new Alg_time_map();
+    // no need to allocate an Alg_time_map since its done during instantiation
     time_map->last_tempo = ser_buf.get_double();
-    time_map->last_tempo_flag = (bool) ser_buf.get_int32();
+    time_map->last_tempo_flag = ser_buf.get_int32() != 0;
     long beats = ser_buf.get_int32();
     ser_buf.check_input_buffer(beats * 16 + 4);
     int i;
@@ -1493,6 +1495,15 @@
     ser_buf.get_pad();
     add_track(tracks_num - 1); // create tracks_num tracks
     for (i = 0; i < tracks_num; i++) {
+        bool algt = ser_buf.get_char() == 'A' &&
+                    ser_buf.get_char() == 'L' &&
+                    ser_buf.get_char() == 'G' &&
+                    ser_buf.get_char() == 'T';
+        if (!algt) {
+            assert(algt);
+            // bug or bad data
+            break;
+        }
         track(i)->unserialize_track();
     }
     // assume seq started at beginning of buffer. len measures
@@ -1504,15 +1515,11 @@
 
 void Alg_track::unserialize_track()
 {
-    ser_buf.check_input_buffer(32);
-    assert(ser_buf.get_char() == 'A');
-    assert(ser_buf.get_char() == 'L');
-    assert(ser_buf.get_char() == 'G');
-    assert(ser_buf.get_char() == 'T');
+    ser_buf.check_input_buffer(24);
     long offset = ser_buf.get_posn(); // stored length does not include 'ALGT'
     long bytes = ser_buf.get_int32();
     assert(bytes <= ser_buf.get_len() - offset);
-    units_are_seconds = (bool) ser_buf.get_int32();
+    units_are_seconds = ser_buf.get_int32() != 0;
     beat_dur = ser_buf.get_double();
     real_dur = ser_buf.get_double();
     int event_count = ser_buf.get_int32();
@@ -1530,7 +1537,7 @@
             double dur = ser_buf.get_double();
             Alg_note *note = 
                     create_note(time, channel, key, pitch, loud, dur);
-            note->set_selected(selected);
+            note->set_selected(selected != 0);
             long param_num = ser_buf.get_int32();
             int j;
             // this builds a list of parameters in the correct order
@@ -1545,7 +1552,7 @@
         } else {
             assert(type == 'u');
             Alg_update *update = create_update(time, channel, key);
-            update->set_selected(selected);
+            update->set_selected(selected != 0);
             unserialize_parameter(&(update->parameter));
             append(update);
         }
@@ -1573,7 +1580,7 @@
         break;
     case 'l':
         ser_buf.check_input_buffer(4);
-        parm_ptr->l = (bool) ser_buf.get_int32();
+        parm_ptr->l = ser_buf.get_int32() != 0;
         break;
     case 'a':
         parm_ptr->a = symbol_table.insert_attribute(ser_buf.get_string());
@@ -1581,6 +1588,7 @@
     }
 }
 
+#pragma warning(default: 4800)
 
 void Alg_track::set_time_map(Alg_time_map *map)
 {
@@ -2131,10 +2139,7 @@
 
 Alg_tracks::~Alg_tracks()
 {
-    // Alg_events objects (track data) are not deleted, only the array
-    if (tracks) {
-        delete[] tracks;
-    }
+    reset();
 }
 
 
@@ -2292,6 +2297,7 @@
     return &(track_list[i]);
 }
 
+#pragma warning(disable: 4715) // ok not to return a value here
 
 Alg_event_ptr &Alg_seq::operator[](int i) 
 {
@@ -2308,6 +2314,7 @@
     }
     assert(false); // out of bounds
 }
+#pragma warning(default: 4715)
 
 
 void Alg_seq::convert_to_beats()

Index: allegro.h
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/allegro.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- allegro.h   6 Aug 2008 17:57:54 -0000       1.6
+++ allegro.h   22 Jun 2009 08:48:07 -0000      1.7
@@ -79,6 +79,14 @@
         maxlen = len = 0;
         atoms = NULL;
     }
+    virtual ~Alg_atoms() {
+        for (int i = 0; i < len; i++) {
+            if (atoms[i]) {
+                delete atoms[i];
+            }
+        }
+        if (atoms) delete[] atoms;
+    }
     // insert/lookup an atttribute
     Alg_attribute insert_attribute(Alg_attribute attr);
     // insert/lookup attribute by name (without prefixed type)
@@ -499,6 +507,9 @@
         ptr = NULL;
         len = 0;
     }
+    ~Serial_buffer() {
+        if (buffer) delete [] buffer;
+    }
     void init_for_write() { ptr = buffer; }
     long get_posn() { return (long) (ptr - buffer); }
     long get_len() { return len; }
@@ -515,13 +526,22 @@
         // two lots of brackets surpress a g++ warning, because this is an
         // assignment operator inside a test.
         while ((*ptr++ = *s++)) assert(ptr < fence);
+// 4311 is type cast pointer to long warning
+// 4312 is type cast loing to pointer warning
+#pragma warning(disable: 4311 4312)
         assert((char *)(((long) (ptr + 7)) & ~7) <= fence);
+#pragma warning(default: 4311 4312)
         pad(); }
     void set_int32(long v) { *((long *) ptr) = v; ptr += 4; }
     void set_double(double v) { *((double *) ptr) = v; ptr += 8; }
     void set_float(float v) { *((float *) ptr) = v; ptr += 4; }
     void set_char(char v) { *ptr++ = v; }
+#pragma warning(disable: 546) // cast to int is OK, we only want low 7 bits
+#pragma warning(disable: 4311) // type cast pointer to long warning
     void pad() { while (((long) ptr) & 7) set_char(0); }
+    void get_pad() { while (((long) ptr) & 7) ptr++; }
+#pragma warning(default: 4311)
+#pragma warning(default: 546)
     void *to_heap(long *len) {
         *len = get_posn();
         char *newbuf = new char[*len];
@@ -529,8 +549,12 @@
         return newbuf;
     }
     void init_for_read(void *buf, long n) {
-        buffer = (char *) buf;
-        ptr = (char *) buf;
+        if (buffer) {
+            delete [] buffer;
+        }
+        buffer = new char[n];
+        memcpy(buffer, buf, n);
+        ptr = buffer;
         len = n;
     }
     char get_char() { return *ptr++; }
@@ -543,7 +567,6 @@
                          while (*ptr++) assert(ptr < fence);
                          get_pad();
                          return s; }
-    void get_pad() { while (((long) ptr) & 7) ptr++; }
     void check_input_buffer(long needed) {
         assert(get_posn() + needed <= len); }
 } *Serial_buffer_ptr;
@@ -581,7 +604,7 @@
     // copy constructor: event_list is copied, map is installed and referenced
     Alg_track(Alg_event_list_ref event_list, Alg_time_map_ptr map, 
               bool units_are_seconds);
-    virtual ~Alg_track() { set_time_map(NULL); }
+    virtual ~Alg_track() { if (time_map) time_map->dereference(); time_map = 
NULL; }
 
     // Returns a buffer containing a serialization of the
     // file.  It will be an ASCII representation unless text is true.
@@ -856,7 +879,7 @@
     void seq_from_track(Alg_track_ref tr);
     Alg_seq(std::istream &file, bool smf); // create from file
     Alg_seq(const char *filename, bool smf); // create from filename
-    ~Alg_seq();
+    virtual ~Alg_seq();
     int get_read_error() { return error; }
     void serialize(void **buffer, long *bytes);
     void copy_time_sigs_to(Alg_seq *dest); // a utility function
@@ -871,7 +894,7 @@
     void write(std::ostream &file, bool in_secs);
     // returns true on success
     bool write(const char *filename);
-    void smf_write(std::ofstream &file);
+    void smf_write(std::ostream &file);
     bool smf_write(const char *filename);
 
     // Returns the number of tracks

Index: allegrosmfrd.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/allegrosmfrd.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- allegrosmfrd.cpp    6 Aug 2008 17:57:54 -0000       1.2
+++ allegrosmfrd.cpp    22 Jun 2009 08:48:07 -0000      1.3
@@ -179,7 +179,9 @@
 {
     if (format > 1) {
         char msg[80];
+#pragma warning(disable: 4996) // msg is long enough
         sprintf(msg, "file format %d not implemented", format);
+#pragma warning(default: 4996)
         Mf_error(msg);
     }
     divisions = division;
@@ -266,7 +268,9 @@
 {
     Alg_parameter parameter;
     char name[32];
+#pragma warning(disable: 4996) // name is long enough
     sprintf(name, "control%dr", control);
+#pragma warning(default: 4996)
     parameter.set_attr(symbol_table.insert_string(name));
     parameter.r = val / 127.0;
     update(chan, -1, &parameter);
@@ -310,7 +314,9 @@
     Alg_parameter parameter;
     char *hexstr = new char[len * 2 + 1];
     for (int i = 0; i < len; i++) {
+#pragma warning(disable: 4996) // hexstr is long enough
         sprintf(hexstr + 2 * i, "%02x", (0xFF & msg[i]));
+#pragma warning(default: 4996)
     }
     parameter.s = hexstr;
     parameter.set_attr(symbol_table.insert_string(attr_string));
@@ -334,7 +340,9 @@
 void Alg_midifile_reader::Mf_metamisc(int type, int len, char *msg)
 {
     char text[128];
+#pragma warning(disable: 4996) // text is long enough
     sprintf(text, "metamsic data, type 0x%x, ignored", type);
+#pragma warning(default: 4996)
     Mf_error(text);
 }
 
@@ -355,8 +363,10 @@
     char text[32];
     int fps = (hours >> 6) & 3;
     hours &= 0x1F;
+#pragma warning(disable: 4996) // text is long enough
     sprintf(text, "%sfps:%02dh:%02dm:%02ds:%02d.%02df", 
             fpsstr[fps], hours, mins, secs, frames, subframes);
+#pragma warning(default: 4996)
     Alg_parameter smpteoffset;
     smpteoffset.s = heapify(text);
     smpteoffset.set_attr(symbol_table.insert_string("smpteoffsets"));

Index: mfmidi.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/mfmidi.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- mfmidi.cpp  6 Aug 2008 17:57:54 -0000       1.2
+++ mfmidi.cpp  22 Jun 2009 08:48:07 -0000      1.3
@@ -13,6 +13,7 @@
 #include "stdio.h"
 #include "mfmidi.h"
 #include "string.h"
+#include "assert.h"
 
 #define MIDIFILE_ERROR -1
 
@@ -38,6 +39,7 @@
     /* read through the "MThd" or "MTrk" header string */
     /* if skip == 1, we attempt to skip initial garbage. */
 {
+    assert(strlen(s) == 4); // must be "MThd" or "MTrk"
     int nread = 0;
     char b[4];
     char buff[32];
@@ -66,8 +68,10 @@
         goto retry;
     }
     err:
+#pragma warning(disable: 4996) // strcpy is safe since strings have known 
lengths
     (void) strcpy(buff,errmsg);
     (void) strcat(buff,s);
+#pragma warning(default: 4996) // turn it back on
     mferror(buff);
     return(0);
 }
@@ -253,8 +257,9 @@
 void Midifile_reader::badbyte(int c)
 {
     char buff[32];
-
+#pragma warning(disable: 4996) // safe in this case
     (void) sprintf(buff,"unexpected byte: 0x%02x",c);
+#pragma warning(default: 4996)
     mferror(buff);
 }
 

Index: trace.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/trace.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- trace.cpp   11 Jul 2008 15:39:25 -0000      1.1
+++ trace.cpp   22 Jun 2009 08:48:07 -0000      1.2
@@ -15,7 +15,7 @@
     char msg[256];
     va_list args;
     va_start(args, format);
-    _vsnprintf(msg, 256, format, args);
+    _vsnprintf_s(msg, 256, _TRUNCATE, format, args);
     va_end(args);
 #ifdef _DEBUG
     _CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, msg);

Index: allegrosmfwr.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/allegrosmfwr.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- allegrosmfwr.cpp    6 Aug 2008 17:57:54 -0000       1.2
+++ allegrosmfwr.cpp    22 Jun 2009 08:48:07 -0000      1.3
@@ -34,7 +34,7 @@
     // chan is actual_channel + channels_per_track * track_number
     // default is 100, set this to 0 to merge all tracks to 16 channels
 
-    void write(ofstream &file /* , midiFileFormat = 1 */);
+    void write(ostream &file /* , midiFileFormat = 1 */);
 
 private:
     long previous_divs; // time in ticks of most recently written event
@@ -162,7 +162,7 @@
 
     //printf("deltaDivisions: %d, beats elapsed: %g, on? %c\n", 
deltaDivisions, note->time, on);
 
-    char chan = (note->chan & 15);
+    char chan = char(note->chan & 15);
     int pitch = int(note->pitch + 0.5);
     if (pitch < 0) {
           pitch = pitch % 12;
@@ -184,8 +184,8 @@
 {
    if (update->chan >= 0) { // write MIDI Channel Prefix
         write_delta(update->time);
-        out_file->put(0xFF); // Meta Event
-        out_file->put(0x20); // Type code for MIDI Channel Prefix
+        out_file->put('\xFF'); // Meta Event
+        out_file->put('\x20'); // Type code for MIDI Channel Prefix
         out_file->put(1); // length
         out_file->put(to_midi_channel(update->chan));
         // one thing odd about the Std MIDI File spec is that once
@@ -201,7 +201,7 @@
 {
     write_midi_channel_prefix(update);
     write_delta(update->time);
-    out_file->put(0xFF);
+    out_file->put('\xFF');
     out_file->put(type);
     out_file->put((char) strlen(update->parameter.s));
     *out_file << update->parameter.s;
@@ -212,8 +212,8 @@
 {
     write_midi_channel_prefix(update);
     write_delta(update->time);
-    out_file->put(0xFF); // meta event
-    out_file->put(0x54); // smpte offset type code
+    out_file->put('\xFF'); // meta event
+    out_file->put('\x54'); // smpte offset type code
     out_file->put(5); // length
     for (int i = 0; i < 5; i++) *out_file << s[i];
 }
@@ -322,7 +322,7 @@
                update->parameter.attr_type() == 's') {
         char *s = update->parameter.s;
         write_delta(update->time);
-        out_file->put(0xFF);
+        out_file->put('\xFF');
         write_binary(0x7F, s);
 
     /****Text Events****/
@@ -390,8 +390,8 @@
     }
     if (keysig != -99 && keysig_mode) { // write when both are defined
         write_delta(keysig_when);
-        out_file->put(0xFF);
-        out_file->put(0x59);
+        out_file->put('\xFF');
+        out_file->put('\x59');
         out_file->put(2);
         // mask off high bits so that this value appears to be positive
         // i.e. -1 -> 0xFF (otherwise, write_data will clip -1 to 0)
@@ -482,9 +482,9 @@
     //    printf("Inserting tempo %f after %f clocks.\n", tempo, delta);
     write_varinum(divs - previous_divs);
     previous_divs = divs;
-    out_file->put(0xFF);
-    out_file->put(0x51);
-    out_file->put(0x03);
+    out_file->put('\xFF');
+    out_file->put('\x51');
+    out_file->put('\x03');
     write_24bit((int)tempo);
 }
 
@@ -515,9 +515,9 @@
     // write the time signature
     long divs = ROUND(ts[i].beat * division);
     write_varinum(divs - previous_divs);
-    out_file->put(0xFF);
-    out_file->put(0x58);  // time signature
-    out_file->put(4);     // length of message
+    out_file->put('\xFF');
+    out_file->put('\x58');  // time signature
+    out_file->put('\x04');  // length of message
     out_file->put(ROUND(ts[i].num));
     int den = ROUND(ts[i].den);
     int den_byte = 0;
@@ -532,7 +532,7 @@
 
 
 
-void Alg_smf_write::write(ofstream &file)
+void Alg_smf_write::write(ostream &file)
 {
     int track_len_offset;
     int track_end_offset;
@@ -564,9 +564,9 @@
 
         // End of track event
         write_varinum(0);           // delta time
-        out_file->put(0xFF);
-        out_file->put(0x2F);
-        out_file->put(0x00);
+        out_file->put('\xFF');
+        out_file->put('\x2F');
+        out_file->put('\x00');
 
         // Go back and write in the length of the track
         track_end_offset = out_file->tellp();
@@ -632,7 +632,7 @@
 }
 
 
-void Alg_seq::smf_write(ofstream &file)
+void Alg_seq::smf_write(ostream &file)
 {
     Alg_smf_write writer(this);
     writer.write(file);

Index: allegrord.cpp
===================================================================
RCS file: /cvsroot/audacity/lib-src/portsmf/allegrord.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- allegrord.cpp       27 Feb 2009 17:37:59 -0000      1.4
+++ allegrord.cpp       22 Jun 2009 08:48:07 -0000      1.5
@@ -346,8 +346,8 @@
                     note_ptr->time = time;
                     note_ptr->dur = dur;
                     note_ptr->set_identifier(key);
-                    note_ptr->pitch = pitch;
-                    note_ptr->loud = loud;
+                    note_ptr->pitch = (float) pitch;
+                    note_ptr->loud = (float) loud;
                     note_ptr->parameters = attributes;
                     seq->add_event(note_ptr, track_num); // sort later
                     if (seq->get_real_dur() < (time + dur)) 
seq->set_real_dur(time + dur);
@@ -678,6 +678,8 @@
         // so total memory to allocate is (len - i) - 1
         char *r = new char[(len - i) - 1];
         strncpy(r, s.c_str() + i + 1, (len - i) - 2);
+//      Windows only
+//      strncpy_s(r, (len - i) - 2, s.c_str() + i + 1, _TRUNCATE);
         r[(len - i) - 2] = 0; // terminate the string
         param->s = r;
     } else if (s[i] == '\'') {


------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
Audacity-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/audacity-cvs

Reply via email to