int file_open_readlines(const char* filename, c_array* lines, c_array* file_contents) { int i, pos, len; char** char_ptr = NULL; char* nl = NULL; lines->data = NULL; lines->len = 0; lines->elem_size = 1; if (!file_open_read(filename, "r", file_contents)) { return 0; } len = file_contents->len / 60 + 1; /* start with conservative estimate if # of lines */ lines->data = malloc(len * sizeof(char*) + 1); if (!lines->data) return 0; char_ptr = (char**)lines->data; i = 0, pos = 0; while (1) { char_ptr[i] = (char*)&file_contents->data[pos]; nl = strchr((char*)&file_contents->data[pos], '\n'); if (nl) { *nl = '\0'; pos = nl - (char*)file_contents->data + 1; i++; if (i == len) { len *= 2; if (!(char_ptr = realloc(lines->data, len * sizeof(char*) + 1))) { free(lines->data); lines->len = 0; return 0; } lines->data = (byte*)char_ptr; } } else { break; } } lines->data = realloc(char_ptr, i*sizeof(char*)+1); lines->len = i; lines->elem_size = sizeof(char*); return 1; }
int main(int argc, char *argv[]) { int retval = EXIT_SUCCESS; char *f; if (!(f = *++argv)) { fprintf(stderr, "Usage: vorbistest oggfile\n"); return 1; } ogg_init(); vorbis_stream_t vorbis; vorbis_stream_init(&vorbis); buf_t packet; packet.len = 0; packet.size = 0; packet.data = NULL; buf_alloc(&packet, 512); if (!file_open_read(&vorbis.file, f)) { fprintf(stderr, "Could not open ogg file: %s\n", f); retval = EXIT_FAILURE; goto exit; } if (!vorbis_stream_read_hdrs(&vorbis)) { fprintf(stderr, "Stream is not a OGG encapsulated Vorbis stream\n"); retval = EXIT_FAILURE; goto exit; } while (vorbis_next_packet(&vorbis, &packet) > 0) { fprintf(stderr, "Packet length: %lu, size %lu\n", packet.len, packet.size); fgetc(stdin); } file_close(&vorbis.file); exit: vorbis_stream_destroy(&vorbis); buf_free(&packet); return retval; }
int show_loop(char *filename) { UI ui; File f; if(file_open_read(&f, filename) != 0) { printf("Error opening '%s': %s\n", filename, strerror(errno)); return 1; } if(ui_init(&ui,f.N) != 0) { puts("Error initializing UI."); return 1; } mkdir("frames",0751); int frames = 0; while(!ui_should_quit(&ui)) { if(file_read_frame(&f) != 0) break; if(file_read_frame(&f) != 0) break; ui_draw_file(&ui, &f); ui_poll_events(&ui); frames++; if(frames % 10 == 0) { printf("%d frames\n", frames); } /*char filename[32]; snprintf(filename, sizeof(filename), "frames/%05d.webp", frames); int rc = ui_save_frame(&ui, filename); if(rc != 0) { printf("failed to save frame: %s\n", strerror(errno)); break; }*/ } file_close(&f); ui_deinit(&ui); return 0; }
JSValueRef function_file_input_stream_open(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); uint64_t descriptor = file_open_read(path); free(path); char *descriptor_str = descriptor_int_to_str(descriptor); JSValueRef rv = c_string_to_value(ctx, descriptor_str); free(descriptor_str); return rv; } return JSValueMakeNull(ctx); }
// read data from source file into ringbuffer // void reader(char* srcpath) { int srcfd = file_open_read(srcpath); // open source file for reading int bufpos = 0; // next free space in ringbuffer int rcount; // number of bytes read on last iteration do { sem_wait(&buf_full); // wait until buffer is not full // read from sourcefile into buffer rcount = file_read(srcfd, buffer[bufpos], sizeof(buffer[bufpos])); // store number of bytes read and advance write head buffill[bufpos] = rcount; bufpos = (bufpos + 1) % BUFSIZE; sem_post(&buf_empty); // increase number of cells written to buffer (unblock writer) } while(rcount > 0); // abort on EOF (zero bytes read from source) file_close(srcfd); }
int main(int argc, char *argv[]) { char *cuefilename = NULL, *mp3filename = NULL; int retval = EXIT_SUCCESS; FILE *cuein = NULL; mp3cue_track_t *cuetracks = NULL; int c; while ((c = getopt(argc, argv, "c:C:")) >= 0) { switch (c) { case 'c': if (cuefilename != NULL) free(cuefilename); cuefilename = strdup(optarg); break; case 'C': break; default: usage(); goto exit; } } if (optind == argc) { usage(); goto exit; } mp3filename = argv[optind]; if ((cuefilename == NULL) || (mp3filename == NULL)) { usage(); retval = EXIT_FAILURE; goto exit; } /*M Initialize the mp3 cue structure. **/ mp3cue_file_t cuefile; cuetracks = malloc(sizeof(mp3cue_track_t) * MP3CUE_DEFAULT_TRACK_NUMBER); if (cuetracks == NULL) { fprintf(stderr, "Could not allocate memory for tracks\n"); retval = EXIT_FAILURE; goto exit; } cuefile.tracks = cuetracks; cuefile.track_number = 0; cuefile.max_track_number = MP3CUE_DEFAULT_TRACK_NUMBER; yymp3_cue_file = &cuefile; strncpy(cuefile.title, cuefilename, MP3CUE_MAX_STRING_LENGTH); /*M Open the input file. **/ cuein = fopen(cuefilename, "r"); if (cuein == NULL) { fprintf(stderr, "Could not open cuefile %s\n", cuefilename); retval = EXIT_FAILURE; goto exit; } /*M Parse the input file. **/ extern FILE* yyin; yyin = cuein; if (yyparse() != 0) { retval = EXIT_FAILURE; goto exit; } /*M Open the MP3 file. **/ file_t mp3file; if (!file_open_read(&mp3file, mp3filename)) { fprintf(stderr, "Could not open mp3 file: %s\n", mp3filename); retval = EXIT_FAILURE; return 0; } aq_t qin; aq_init(&qin); unsigned long current = 0; /*M For each track, cut out the relevant part and save it. **/ unsigned int i; for (i = 0; i < cuefile.track_number; i++) { char outfilename[MP3CUE_MAX_STRING_LENGTH * 3 + 1]; if (strlen(cuefile.tracks[i].performer) > 0 && strlen(cuefile.tracks[i].title) > 0) { snprintf(outfilename, MP3CUE_MAX_STRING_LENGTH * 3, "%02d. %s - %s.mp3", cuefile.tracks[i].number, cuefile.tracks[i].performer, cuefile.tracks[i].title); } else { snprintf(outfilename, MP3CUE_MAX_STRING_LENGTH * 3, "%02d. %s.mp3", cuefile.tracks[i].number, cuefile.title); } aq_t qout; aq_init(&qout); /*M Open the output MP3 file. **/ file_t outfile; if (!file_open_write(&outfile, outfilename)) { fprintf(stderr, "Could not open mp3 file: %s\n", outfilename); file_close(&mp3file); retval = EXIT_FAILURE; goto exit; } /* end time in msecs */ unsigned long end = (((cuefile.tracks[i].index.minutes * 60) + cuefile.tracks[i].index.seconds) * 100 + cuefile.tracks[i].index.centiseconds) * 10; char from_buf[256], to_buf[256]; format_time(current, from_buf, sizeof(from_buf)); format_time(end, to_buf, sizeof(to_buf)); printf("Extracting track %d (%s): %s - %s...\n", i, outfilename, from_buf, end ? to_buf : "end"); /* write id3 tags */ if (!mp3cue_write_id3(&outfile, &cuefile, &cuefile.tracks[i])) { fprintf(stderr, "Could not write id3 tags to file: %s\n", outfilename); file_close(&outfile); file_close(&mp3file); retval = EXIT_FAILURE; goto exit; } /*M Read in the input file Read while current < end or till the end of the file if it's the last track. **/ while ((current < end) || (i == (cuefile.track_number - 1))) { mp3_frame_t frame; if (mp3_next_frame(&mp3file, &frame) > 0) { if (aq_add_frame(&qin, &frame)) { adu_t *adu = aq_get_adu(&qin); assert(adu != NULL); if (aq_add_adu(&qout, adu)) { mp3_frame_t *frame_out = aq_get_frame(&qout); assert(frame_out != NULL); memset(frame_out->raw, 0, 4 + frame_out->si_size); if (!mp3_fill_hdr(frame_out) || !mp3_fill_si(frame_out) || (mp3_write_frame(&outfile, frame_out) <= 0)) { fprintf(stderr, "Could not write frame\n"); file_close(&mp3file); file_close(&outfile); retval = 1; goto exit; } free(frame_out); } free(adu); } current += frame.usec / 1000; } else { if (i != (cuefile.track_number - 1)) fprintf(stderr, "Could not read the next frame from the mp3 file...\n"); break; } } /*M Close the output file. **/ file_close(&outfile); aq_destroy(&qout); fprintf(stderr, "%s written\n", outfilename); } /*M Close the input file. **/ file_close(&mp3file); aq_destroy(&qin); /*M Cleanup the data structures. **/ exit: if (cuein != NULL) fclose(cuein); if (cuetracks != NULL) free(cuetracks); if (cuefilename != NULL) free(cuefilename); return retval; }
/*M \emph{Simple HTTP streaming server main loop.} The mainloop opens the MPEG Audio file \verb|filename|, reads each frame into an rtp packet and sends it out using HTTP. After sending a packet, the mainloop sleeps for the duration of the packet, synchronizing itself when the sleep is not accurate enough. If the sleep desynchronizes itself from the stream more than \verb|MAX_WAIT_TIME|, the synchronization is reset. **/ int poc_mainloop(http_server_t *server, char *filename, int quiet) { /*M Open file for reading. **/ file_t mp3_file; if (!file_open_read(&mp3_file, filename)) { fprintf(stderr, "Could not open mp3 file: %s\n", filename); return 0; } if (!quiet) fprintf(stderr, "\rStreaming %s...\n", filename); static long wait_time = 0; unsigned long frame_time = 0; mp3_frame_t frame; /*M Cycle through the frames and send them using HTTP. **/ while ((mp3_next_frame(&mp3_file, &frame) >= 0) && !finished) { /*M Get start time for this frame iteration. **/ struct timeval tv; gettimeofday(&tv, NULL); unsigned long start_sec, start_usec; start_sec = tv.tv_sec; start_usec = tv.tv_usec; /*M Go through HTTP main routine and check for timeouts, received data, etc... **/ if (!http_server_main(server, NULL)) { fprintf(stderr, "Http main error\n"); return 0; } /*M Write frame to HTTP clients. **/ int i; for (i = 0; i < server->num_clients; i++) { if ((server->clients[i].fd != -1) && (server->clients[i].found >= 2)) { int ret; ret = unix_write(server->clients[i].fd, frame.raw, frame.frame_size); if (ret != frame.frame_size) { fprintf(stderr, "Error writing to client %d: %d\n", i, ret); http_client_close(server, server->clients + i); } } } frame_time += frame.usec; wait_time += frame.usec; /*M Sleep for duration of frame. **/ if (wait_time > 1000) usleep(wait_time); /*M Print information. **/ if (!quiet) { static int count = 0; if ((count++) % 10 == 0) { if (mp3_file.size > 0) { fprintf(stderr, "\r%02ld:%02ld/%02ld:%02ld %7ld/%7ld (%3ld%%) %3ldkbit/s %4ldb ", (frame_time/1000000) / 60, (frame_time/1000000) % 60, (long)((float)(frame_time/1000) / ((float)mp3_file.offset+1) * (float)mp3_file.size) / 60000, (long)((float)(frame_time/1000) / ((float)mp3_file.offset+1) * (float)mp3_file.size) / 1000 % 60, mp3_file.offset, mp3_file.size, (long)(100*(float)mp3_file.offset/(float)mp3_file.size), frame.bitrate, frame.frame_size); } else { fprintf(stderr, "\r%02ld:%02ld %ld %3ldkbit/s %4ldb ", (frame_time/1000000) / 60, (frame_time/1000000) % 60, mp3_file.offset, frame.bitrate, frame.frame_size); } } fflush(stderr); } /*M Get length of iteration. **/ gettimeofday(&tv, NULL); unsigned long len = (tv.tv_sec - start_sec) * 1000000 + (tv.tv_usec - start_usec); wait_time -= len; if (abs(wait_time) > MAX_WAIT_TIME) wait_time = 0; } if (!file_close(&mp3_file)) { fprintf(stderr, "Could not close mp3 file %s\n", filename); return 0; } return 1; }