All pastes #571575 Raw Edit

oggz-comment.c

public c v1 · immutable
·published 2007-06-17 06:13 UTC
rendered paste body
#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <oggz/oggz.h>int get_stream_types(OGGZ *oggz, ogg_packet *op,		     long serialno, void* user_data) {  OggzTable *table = (OggzTable *)user_data;  char *content;  if(!oggz_table_lookup(table, serialno)) {    content = oggz_stream_get_content_type(oggz, serialno);    oggz_table_insert(table, serialno, content);  }  return 0;}int copy_replace_comments(OGGZ *oggz, ogg_packet *op,			  long serialno, void *user_data) {  OGGZ *oggz_write = (OGGZ *)user_data;  ogg_packet *comment_packet;  OggzStreamContent content = oggz_stream_get_content(oggz, serialno);  int flush;  if (op->granulepos == -1) {    flush = 0;  } else {    flush = OGGZ_FLUSH_AFTER;  }  if(op->packetno == 1) {    oggz_comment_set_vendor(oggz_write, serialno,			    oggz_comment_get_vendor(oggz, serialno));    comment_packet = oggz_comment_generate(oggz_write, serialno, content, 0);    oggz_write_feed(oggz_write, comment_packet, serialno, flush, NULL);  } else {    oggz_write_feed(oggz_write, op, serialno, flush, NULL);  }  return 0;}void list_comments(OGGZ *oggz, long serialno) {  OggzComment *comment;  printf("\tVendor: ");  printf(oggz_comment_get_vendor(oggz, serialno));  printf("\n");  comment = oggz_comment_first(oggz, serialno);  while(comment) {    printf("\t");    printf(comment->name);    printf(": ");    printf(comment->value);    printf("\n");    comment = oggz_comment_next(oggz, serialno, comment);  }}void list_comments_all(OGGZ *oggz) {  int i;  long serialno;  OggzTable *table = oggz_table_new();  oggz_set_read_callback(oggz, -1, get_stream_types, table);  oggz_run(oggz);  for(i = 0; i < oggz_table_size(table); i++) {    printf(oggz_table_nth(table, i, &serialno));    printf(" (serialno = %ld):\n", serialno);    list_comments(oggz, serialno);  }}void edit_comments(OGGZ *oggz, long serialno, OggzComment *comments) {  int i;  for(i = 0; strcmp(comments[i].name, "0") != 0; i++) {    printf(comments[i].name);    oggz_comment_remove_byname(oggz, serialno, comments[i].name);    printf(": ");    printf(comments[i].value);    oggz_comment_add(oggz, serialno, &comments[i]);  }}OggzComment parse_comment_field(char *arg) {  int i;  char *c;  OggzComment comment;  comment.name = strcpy(calloc(strlen(arg) + 1, sizeof(char)), arg);  c = strchr(comment.name, '=');  *c = '\0';  for(i = 0; comment.name[i]; i++) {    if(islower(arg[i]))      comment.name[i] = toupper(arg[i]);  }  comment.value = c + 1;  return comment;}void help() {  printf("OH HI, LOL\n");}void version() {  printf("I DUNNO, LOL\n");}int main(int argc, char *argv[]) {  int i, index;  long n, serialno = 0;  unsigned char buffer[1024];  char *out_filename;  FILE *out_file;  OGGZ *oggz_in;  OGGZ *oggz_out;  OggzComment *comments;  OggzTable *table = oggz_table_new();  if(argc == 1) {    help();    return 0;  }  if(strcmp(argv[1], "--version") == 0     || strcmp(argv[1], "-v") == 0) {    version();    return 0;  } else if(strcmp(argv[1], "--help") == 0	    || strcmp(argv[1], "-h") == 0) {    help();    return 0;  } else {    oggz_in = oggz_open(argv[1], OGGZ_READ);    oggz_out = oggz_new(OGGZ_WRITE);    out_filename = argv[1];  }  comments = calloc(argc - 1, sizeof(OggzComment));  comments[index = 0].name = "0";  for(i = 2; i < argc; i++) {    if(strcmp(argv[i], "-l") == 0	      || strcmp(argv[i], "--list") == 0)      list_comments_all(oggz_in);    else if(strcmp(argv[i], "-o") == 0)      out_filename = argv[++i];    else if(strchr(argv[i], '=') != NULL) {      comments[index] = parse_comment_field(argv[i]);      comments[++index].name = "0";    } else if(strncmp(argv[i], "-s", 2) == 0) {      if(strcmp(comments[0].name, "0") != 0) {	oggz_table_insert(table, serialno, comments);	comments = calloc(argc - 2, sizeof(OggzComment));	comments[index = 0].name = "0";      }      serialno = strtol(&argv[i][2], NULL, 10);    } else {      printf("Error: option or field \"");      printf(argv[i]);      printf("\" unrecognized.\n");      return 0;    }  }  if(strcmp(comments[0].name, "0") != 0)    oggz_table_insert(table, serialno, comments);  if(oggz_table_size(table) != 0) {    for(i = 0; i < oggz_table_size(table); i++) {      comments = oggz_table_nth(table, i, &serialno);      edit_comments(oggz_out, serialno, comments);    }    oggz_set_read_callback(oggz_in, -1, copy_replace_comments, oggz_out);    out_file = fopen(out_filename, "w");    while((n = oggz_read(oggz_in, 1024)) > 0) {      while(oggz_write_output(oggz_out, buffer, n) > 0) {	fwrite(buffer, 1, n, out_file);      }    }    fclose(out_file);  } else if(oggz_in)    list_comments_all(oggz_in);  else {    printf("Error: file \"");    printf(argv[1]);    printf("\" could not be opened.\n");  }  oggz_close(oggz_in);  oggz_close(oggz_out);  return 0;}