<commit_before>#include "midi.h"

#define MAX_NUM_OF_TRKS 32
#define MAX_SYSEX_DATA_LEN 128

typedef struct midi_s {
	FILE *stream; /* Valid if loaded from file, otherwise NULL */
	char *mem; /* Valid if loaded from memory */
	size_t size; /* Valid if loaded from memory */
	long pos; /* Valid if loaded from memory */
	midi_hdr_t hdr;
	midi_trk_t trks[MAX_NUM_OF_TRKS];
} midi_t;

const char MTHD_MAGIC[] = {'M','T','h','d'};
const char MTRK_MAGIC[] = {'M','T','r','k'};

#define malloc_t_zero(x, t) (((x) = ((t*)malloc(sizeof(t)))) != NULL && memset((x), 0, sizeof(t)) != NULL)

#define msb32(b) ((b)[0] & 0xff) << 24 | ((b)[1] & 0xff) << 16 | ((b)[2] & 0xff) << 8 | ((b)[3] & 0xff)
#define msb24(b) ((b)[0] & 0xff) << 16 | ((b)[1] & 0xff) << 8 | ((b)[2] & 0xff)
#define msb16(b) ((b)[0] & 0xff) << 8 | ((b)[1] & 0xff)

static int midi_eof(midi_t *midi);
static int midi_getc(midi_t *midi);
static int midi_getnc(midi_t *midi, char* buf, int nbytes);
static int midi_seek(midi_t *midi, long pos, int whence);
static long midi_tell(midi_t *midi);
static int midi_readvarlen(midi_t *midi);
static int midi_parse_hdr(midi_t *midi);
static int midi_parse_trk(midi_t *midi, int trkno);

static int midi_eof(midi_t *midi) {
	if (midi->stream) {
		return feof(midi->stream);
	}
	else {
		if (midi->pos >= midi->size) return 1;
		return 0;
	}
}

/* Returns -1 on error */
static int midi_getc(midi_t *midi) {
	if (midi->stream) {
		return fgetc(midi->stream);
	}
	else {
		if (midi->pos >= midi->size) return -1;
		return *(midi->mem + midi->pos++);
	}
}

/* Returns num of read bytes */
static int midi_getnc(midi_t *midi, char* buf, int nbytes) {<commit_msg>Fix a bug when using midi_open_mem<commit_after>#include "midi.h"

#define MAX_NUM_OF_TRKS 32
#define MAX_SYSEX_DATA_LEN 128

typedef struct midi_s {
	FILE *stream; /* Valid if loaded from file, otherwise NULL */
	char *mem; /* Valid if loaded from memory */
	size_t size; /* Valid if loaded from memory */
	long pos; /* Valid if loaded from memory */
	midi_hdr_t hdr;
	midi_trk_t trks[MAX_NUM_OF_TRKS];
} midi_t;

const char MTHD_MAGIC[] = {'M','T','h','d'};
const char MTRK_MAGIC[] = {'M','T','r','k'};

#define malloc_t_zero(x, t) (((x) = ((t*)malloc(sizeof(t)))) != NULL && memset((x), 0, sizeof(t)) != NULL)

#define msb32(b) ((b)[0] & 0xff) << 24 | ((b)[1] & 0xff) << 16 | ((b)[2] & 0xff) << 8 | ((b)[3] & 0xff)
#define msb24(b) ((b)[0] & 0xff) << 16 | ((b)[1] & 0xff) << 8 | ((b)[2] & 0xff)
#define msb16(b) ((b)[0] & 0xff) << 8 | ((b)[1] & 0xff)

static int midi_eof(midi_t *midi);
static int midi_getc(midi_t *midi);
static int midi_getnc(midi_t *midi, char* buf, int nbytes);
static int midi_seek(midi_t *midi, long pos, int whence);
static long midi_tell(midi_t *midi);
static int midi_readvarlen(midi_t *midi);
static int midi_parse_hdr(midi_t *midi);
static int midi_parse_trk(midi_t *midi, int trkno);

static int midi_eof(midi_t *midi) {
	if (midi->stream) {
		return feof(midi->stream);
	}
	else {
		if (midi->pos >= midi->size) return 1;
		return 0;
	}
}

/* Returns -1 on error */
static int midi_getc(midi_t *midi) {
	if (midi->stream) {
		return fgetc(midi->stream);
	}
	else {
		if (midi->pos >= midi->size) return -1;
		return *(midi->mem + midi->pos++) & 0xff;
	}
}

/* Returns num of read bytes */
static int midi_getnc(midi_t *midi, char* buf, int nbytes) {
{'fraction_non_alphanumeric': 0.19094724220623502, 'fraction_numerical': 0.026079136690647483, 'mean_word_length': 4.4526143790849675, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1926088', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1453, 'n_words': 378}