foxygit / doom Log in
commit b58976aa244ae7bc681a2c254a9a117df0a82ed8
Author:     Alex Mayfield <alexmax2742@gmail.com>
AuthorDate: Tue Mar 14 21:19:46 2017 -0400
Commit:     Alex Mayfield <alexmax2742@gmail.com>
CommitDate: Tue Mar 14 21:19:46 2017 -0400

    Put our winged Int16 writing into function
---
 midiproc/main.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/midiproc/main.c b/midiproc/main.c
index 7e792388..391220aa 100644
--- a/midiproc/main.c
+++ b/midiproc/main.c
@@ -54,6 +54,22 @@ static Mix_Music *music  = NULL;
 // Private functions
 //

+//
+// Write an unsigned integer into a simple CHAR buffer.
+//
+static boolean WriteInt16(CHAR *out, size_t osize, unsigned int in)
+{
+    if (osize < 2)
+    {
+        return false;
+    }
+
+    out[0] = (in >> 8) & 0xff;
+    out[1] = in & 0xff;
+
+    return true;
+}
+
 //
 // Cleanly close our in-use pipes.
 //
@@ -145,7 +161,6 @@ static void StopSong()

 static boolean MidiPipe_RegisterSong(buffer_reader_t *reader)
 {
-    unsigned int i;
     CHAR buffer[2];
     DWORD bytes_written;

@@ -157,11 +172,11 @@ static boolean MidiPipe_RegisterSong(buffer_reader_t *reader)

     RegisterSong(filename);

-    // FIXME: We should probably have a function for writing Int16's into
-    //        buffers, as opposed to simply winging it.
-    i = MIDIPIPE_PACKET_TYPE_REGISTER_SONG_ACK;
-    buffer[0] = (i >> 8) & 0xff;
-    buffer[1] = i & 0xff;
+    if (!WriteInt16(buffer, sizeof(buffer),
+                    MIDIPIPE_PACKET_TYPE_REGISTER_SONG_ACK))
+    {
+        return false;
+    }

     WriteFile(midi_process_out, buffer, sizeof(buffer),
               &bytes_written, NULL);