int li_sem_game_spawn(t_env e[1]) { union semun_t su; e->semid_game = semget(e->keygame, 1, IPC_CREAT | IPC_EXCL | 0666); if (e->semid_game == -1) return (ERRORNO("semget(..., IPC_CREAT | IPC_EXCL, ...)")); su.val = 1; if (semctl(e->semid_game, 0, SETVAL, su) < 0) return (ERRORNO("semctl(..., SETVAL, ...)")); ft_printf("\t:yel:e->semid_game spawned and set:eoc:\n"); return (0); }
int li_sem_game_read(t_env e[1]) { e->semid_game = semget(e->keygame, 0, 0); if (e->semid_game == -1) return (ERRORNO("semget()")); ft_printf("\t:yel:e->semid_game read:eoc:\n"); return (0); }
static int read_over(FILE *stream) { if (feof(stream)) return (-1); if (ferror(stream)) { ERRORNO("fgets(...)"); return (-2); } ERROR("while reading"); return (-2); }
static int args( t_arg_parser p[1], unsigned int opt[1], t_ftvector paths[1]) { enum e_arg a; enum e_arg expect; expect = FTARG_NONE; while ((a = ft_arg_next(p, expect)) != FTARG_NONE) { if (a == FTARG_OPTION) { if (save_option(opt, p->c)) return (1); } else if (a == FTARG_STRING) { if (ftv_push_back(paths, &p->s)) return (ERRORNO("ftv_push_back")); expect = FTARG_STRING; } } return (0); }
int txtmidi_parse(const char * filename, struct smf_header * header, struct smf_track ** track) { char * eventspace = NULL; int lineno = 0; FILE * file = fopen(filename, "r"); if (file == NULL) { ERRORNO("Unable to open file"); goto fail; } char buffer[4096]; if (fgets(buffer, sizeof buffer, file) == NULL) { ERRORNO("Unable to fgets header"); goto fail; } unsigned int evcount, evlen, division; if (sscanf(buffer, "%u @ %u / %u", &evcount, &evlen, &division) != 3) { ERROR("Unable to parse header. Should be in form \"16 @ 1 / 4\". Had \"%s\"", buffer); goto fail; } header->header_ntracks = 1; header->header_division = division; #define MAX_EVENTS 1024 eventspace = calloc(MAX_EVENTS + 2, sizeof(struct smf_event) + 8); *track = (struct smf_track *) eventspace; char * evdata = eventspace + (MAX_EVENTS + 2) * sizeof(struct smf_event); struct smf_event * events = (*track)->track_events; size_t count = 0; while (fgets(buffer, sizeof buffer, file) != NULL) { int note = 0; char * notestr = strtok(buffer, ":"); //XXX if (notestr == NULL) { ERROR("Parse error"); goto fail; } note = atoi(notestr); if (note < 0 || note >= 128) { ERROR("Invalid note number; expected 0-127, got %d", note); goto fail; } char * evstr = strtok(NULL, ""); if (evstr == NULL) { ERROR("Parse error"); goto fail; } unsigned int time = 0; bool on = false; #define ADD_EVENT(status) ({ \ *events++ = (struct smf_event) { \ .event_deltatime = time * evlen, \ .event_length = 3, \ .event_data = evdata \ }; \ (*track)->track_nevents++; \ *evdata++ = (status); /* Note on/off */ \ *evdata++ = note; /* note */ \ *evdata++ = 127; /* velocity */ \ }) while (*evstr) { switch(*evstr) { case '#': // Note if (on) { ADD_EVENT(0x80); on = false; } ADD_EVENT(0x90); on = true; time++; break; case '>': // Sustain if (!on) { ERROR("'>' not preceeded by '#' or '>'"); goto fail; } time++; break; case '.': // Rest if (on) { ADD_EVENT(0x80); on = false; } time++; break; default: break; } evstr++; } if (on) { ADD_EVENT(0x80); on = false; } if (time != evcount) { ERROR("Invalid number of ticks; expected %d got %d", evcount, time); goto fail; } if ((*track)->track_nevents >= MAX_EVENTS) { ERROR("Too many events; MAX_EVENTS is %d", MAX_EVENTS); goto fail; } } qsort((*track)->track_events, (*track)->track_nevents, sizeof(struct smf_event), ev_compare); // Now convert all the event times into delta times for (size_t i = (*track)->track_nevents - 1; i >= 1; i--) { (*track)->track_events[i].event_deltatime -= (*track)->track_events[i-1].event_deltatime; } return 0; fail: fclose(file); free(eventspace); return -1; }