Changeset 4835:002e0a1371ed in roaraudio


Ignore:
Timestamp:
04/03/11 12:36:29 (13 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

support reading and writing to a file via VIO

File:
1 edited

Legend:

Unmodified
Added
Removed
  • roarclients/roarvio.c

    r4834 r4835  
    2525 
    2626#include <roaraudio.h> 
     27#include <errno.h> 
     28#include <string.h> 
     29 
     30enum action { 
     31 READ, 
     32 WRITE 
     33}; 
     34 
     35void usage (const char * progname) { 
     36} 
    2737 
    2838int main (int argc, char * argv[]) { 
     39 struct roar_vio_defaults def; 
     40 struct roar_vio_calls vio; 
     41 enum action action = READ; 
     42 int i; 
     43 char * k; 
     44 char * file = NULL; 
     45 int o_flags = -1; 
     46 
     47 for (i = 1; i < argc; i++) { 
     48  k = argv[i]; 
     49 
     50  if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) { 
     51   usage(argv[0]); 
     52   return 0; 
     53  } else if ( !strcmp(k, "--read") ) { 
     54   action = READ; 
     55  } else if ( !strcmp(k, "--write") ) { 
     56   action = WRITE; 
     57  } else if ( file == NULL ) { 
     58   file = k; 
     59  } else { 
     60   ROAR_ERR("Too many parameters or unknown parameter: %s", k); 
     61   usage(argv[0]); 
     62   return 1; 
     63  } 
     64 } 
     65 
     66 if ( file == NULL ) { 
     67  usage(argv[0]); 
     68  return 1; 
     69 } 
     70 
     71 switch (action) { 
     72  case READ: 
     73    o_flags = O_RDONLY; 
     74   break; 
     75  case WRITE: 
     76    o_flags = O_WRONLY|O_CREAT|O_TRUNC; 
     77   break; 
     78 } 
     79 
     80 if ( o_flags == -1 ) { 
     81  ROAR_ERR("o_flags unset, very bad. This should never happen."); 
     82  return 1; 
     83 } 
     84 
     85 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, o_flags, 0644) == -1 ) { 
     86  ROAR_ERR("Can not init DSTR defaults. Bad."); 
     87  return 1; 
     88 } 
     89 
     90 if ( roar_vio_open_dstr(&vio, file, &def, 1) == -1 ) { 
     91  ROAR_ERR("Can not open file: %s: %s", file, strerror(errno)); 
     92  return 1; 
     93 } 
     94 
     95 switch (action) { 
     96  case READ: 
     97    roar_vio_copy_data(roar_stdout, &vio); 
     98   break; 
     99  case WRITE: 
     100    roar_vio_copy_data(&vio, roar_stdin); 
     101   break; 
     102 } 
     103 
     104 roar_vio_close(&vio); 
     105 
    29106 return 0; 
    30107} 
Note: See TracChangeset for help on using the changeset viewer.