int record_loop(char *filename) { Parts p; File f; if(parts_init(&p) != 0) { puts("Error initializing Particles"); return 1; } if(file_open_write(&f, filename, p.N) != 0) { printf("Error opening '%s': %s\n", filename, strerror(errno)); return 1; } int frames = 0; srandom(time(0)); while(1) { parts_step(&p); file_write_frame(&f, &p); frames++; if(frames % 10 == 0) { printf("%d frames\n", frames); } } parts_deinit(&p); file_close(&f); return 0; }
// Creating an HTML header void log_write_header() { // Open "LOG_FILE" (see log.h) to write vLogFileHandle = file_open_write(strLogFile); file_str_write(vLogFileHandle, "<html>\n\t<head>\n\t\t<title>Log file</title>\n\t</head>\n\t<body>\n\t\t<h1>Log file</h1>\n\t\t"); char cbuffer[128]; // Log date and time sprintf(cbuffer, "%d.%d.%d %d:%d:%d\n\t\t<table width=\"600\" style=\"border-style: solid; border-width: 1px; \">", (long)sys_day, (long)sys_month, (long)sys_year, (long)sys_hours, (long)sys_minutes, (long)sys_seconds); file_str_write(vLogFileHandle, cbuffer); // Write table header file_str_write(vLogFileHandle, "\n\t\t\t<tr><td><b>Frame</b></td><td><b>Title</b></td><td><b>Message</b></td></tr>"); file_close(vLogFileHandle); nLogHeaderWritten = 1; }
// write date from ringbuffer into destination file // void writer(char* destpath) { int destfd = file_open_write(destpath); // open destination file for writing int bufpos = 0; // next unread space in ringbuffer int wcount; // number of bytes written on last iteration do { sem_wait(&buf_empty); // wait until buffer is not empty // write contents of buffer into destination file wcount = file_write(destfd, buffer[bufpos], buffill[bufpos]); // advance read head bufpos = (bufpos + 1) % BUFSIZE; sem_post(&buf_full); // mark buffer as not full } while(wcount > 0); // abort on EOF (zero bytes in current buffer cell) file_close(destfd); }
JSValueRef function_file_output_stream_open(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeBoolean) { char *path = value_to_c_string(ctx, args[0]); bool append = JSValueToBoolean(ctx, args[1]); uint64_t descriptor = file_open_write(path, append); 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); }
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; }