mercredi 17 juin 2020

C++ .exe with file as argument

I'm sorry for the poor title, i dunno how to explain it however.

I've recently found a code that i'm trying to analyze and understand in order to implement some part of it in a different project.

However i cannot understand how the file that it processes is managed.

The executable is launched through a command like this:

./leandvb --f32 -f 800000 --sr 50000 --derotate 50000 --cnr -d <DVBS_50Ksym_800Ksps_50KHz_IF.iq> output4.ts  

The code inside int main(int argc,const char *argv[]) however is the following:

int main(int argc, const char *argv[]) {
  config cfg;

  for (int i = 0; i<argc;i++)
  {
    std::cout<<"INDEX: "<<i<<" ARG: "<<argv[i]<<" \n";
    std::cout<<"INDEX: "<<i<<" ARGS: "<<argc<<" \n";
  }

  for ( int i=1; i<argc; ++i ) {   
    if      ( ! strcmp(argv[i], "-h") )
      usage(argv[0], stdout, 0);
    else if ( ! strcmp(argv[i], "-v") )
      cfg.verbose = true;
    else if ( ! strcmp(argv[i], "-d") ) {
      cfg.debug2 = cfg.debug;
      cfg.debug = true;
    }
    else if ( ! strcmp(argv[i], "--version") ) {
      printf("%s\n", VERSION);
      return 0;
    }
    else if ( ! strcmp(argv[i], "-f") && i+1<argc )
      cfg.Fs = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--sr") && i+1<argc )
      cfg.Fm = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--standard") && i+1<argc ) {
      ++i;
      if      ( ! strcmp(argv[i], "DVB-S" ) )
    cfg.standard = config::DVB_S;
      else if ( ! strcmp(argv[i], "DVB-S2" ) )
    cfg.standard = config::DVB_S2;
      else usage(argv[0], stderr, 1, argv[i]);
    }
    else if ( ! strcmp(argv[i], "--const") && i+1<argc ) {
      ++i;
      int c;
      for ( c=0; c<cstln_base::COUNT; ++c )
    if ( ! strcmp(argv[i], cstln_base::names[c]) ) {
      cfg.constellation = (cstln_base::predef)c;
      break;
    }
      if ( c == cstln_base::COUNT )
    usage(argv[0], stderr, 1, argv[i]);
    }
    else if ( ! strcmp(argv[i], "--strongpls") )
      cfg.strongpls = true;
    else if ( ! strcmp(argv[i], "--modcods") && i+1<argc )
      cfg.modcods = strtol(argv[++i], NULL, 0);
    else if ( ! strcmp(argv[i], "--framesizes") && i+1<argc )
      cfg.framesizes = strtol(argv[++i], NULL, 0);
    else if ( ! strcmp(argv[i], "--cr") && i+1<argc ) {
      ++i;
      int f;
      for ( f=0; f<FEC_COUNT; ++f )
    if ( ! strcmp(argv[i], fec_names[f]) ) {
      cfg.fec = (code_rate)f;
      break;
    }
      if ( f == FEC_COUNT )
    usage(argv[0], stderr, 1, argv[i]);
    }
    else if ( ! strcmp(argv[i], "--fastlock") )
      cfg.fastlock = true;
    else if ( ! strcmp(argv[i], "--fastdrift") )
      cfg.fastdrift = true;
    else if ( ! strcmp(argv[i], "--viterbi") )
      cfg.viterbi = true;
    else if ( ! strcmp(argv[i], "--ldpc-bf") && i+1<argc )
      cfg.ldpc_bf = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--ldpc-helper") && i+1<argc )
      cfg.ldpc_helper = argv[++i];
    else if ( ! strcmp(argv[i], "--nhelpers") && i+1<argc )
      cfg.nhelpers = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--hard-metric") )
      cfg.hard_metric = true;
    else if ( ! strcmp(argv[i], "--filter") ) {
      fprintf(stderr, "--filter is obsolete; use --resample.\n");
      cfg.resample = true;
    }
    else if ( ! strcmp(argv[i], "--resample") )
      cfg.resample = true;
    else if ( ! strcmp(argv[i], "--resample-rej") && i+1<argc )
      cfg.resample_rej = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--decim") && i+1<argc )
      cfg.decim = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--sampler") && i+1<argc ) {
      ++i;
      if      (!strcmp(argv[i],"nearest")) cfg.sampler = config::SAMP_NEAREST;
      else if (!strcmp(argv[i],"linear" )) cfg.sampler = config::SAMP_LINEAR;
      else if (!strcmp(argv[i],"rrc"    )) cfg.sampler = config::SAMP_RRC;
      else usage(argv[0], stderr, 1, argv[i]);
    }
    else if ( ! strcmp(argv[i], "--rrc-steps") && i+1<argc )
      cfg.rrc_steps = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--rrc-rej") && i+1<argc )
      cfg.rrc_rej = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--roll-off") && i+1<argc )
      cfg.rolloff = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--hq") ) {
      cfg.fastlock = true;
      cfg.viterbi = true;
      cfg.sampler = config::SAMP_RRC;
    }
    else if ( ! strcmp(argv[i], "--hs") )
      cfg.highspeed = true;
    else if ( ! strcmp(argv[i], "--anf") && i+1<argc )
      cfg.anf = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--cnr") )
      cfg.cnr = true;
    else if ( ! strcmp(argv[i], "--tune") && i+1<argc ) {
      fprintf(stderr, "\n** --tune is broken, use --derotate instead.\n\n");
      cfg.Ftune = atof(argv[++i]);
    } else if ( ! strcmp(argv[i], "--drift") )
      cfg.allow_drift = true;
    else if ( ! strcmp(argv[i], "--freq-tol") && i+1<argc )
      cfg.freq_tol = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--sr-tol") && i+1<argc )
      cfg.sr_tol = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--hdlc") )
      cfg.hdlc = true;
    else if ( ! strcmp(argv[i], "--packetized") )
      cfg.packetized = true;
#ifdef GUI
    else if  ( ! strcmp(argv[i], "--gui") )
      cfg.gui = true;
    else if ( ! strcmp(argv[i], "--duration") && i+1<argc )
      cfg.duration = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--linger") )
      cfg.linger = true;
#endif
    else if ( ! strcmp(argv[i], "--u8") )
      cfg.input_format = config::INPUT_U8;
    else if ( ! strcmp(argv[i], "--s8") )
      cfg.input_format = config::INPUT_S8;
    else if ( ! strcmp(argv[i], "--s12") )
      cfg.input_format = config::INPUT_S12;
    else if ( ! strcmp(argv[i], "--s16") )
      cfg.input_format = config::INPUT_S16;
    else if ( ! strcmp(argv[i], "--f32") )
      cfg.input_format = config::INPUT_F32;
    else if ( ! strcmp(argv[i], "--float-scale") && i+1<argc )
      cfg.float_scale = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--loop") )
      cfg.loop_input = true;
    else if ( ! strcmp(argv[i], "--inpipe")  && i+1<argc )
      cfg.input_pipe = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--inbuf")  && i+1<argc )
      cfg.input_buffer = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--buf-factor")  && i+1<argc )
      cfg.buf_factor = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--derotate") && i+1<argc )
      cfg.Fderot = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-pp") && i+1<argc )
      cfg.fd_pp = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-gse") && i+1<argc )
      cfg.fd_gse = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--awgn") && i+1<argc )
      cfg.awgn = atof(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-info") && i+1<argc )
      cfg.fd_info = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-iqsymbols") && i+1<argc )
      cfg.fd_iqsymbols = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-const") && i+1<argc )
      cfg.fd_const = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--fd-spectrum") && i+1<argc )
      cfg.fd_spectrum = atoi(argv[++i]);
    else if ( ! strcmp(argv[i], "--json") )
      cfg.json = true;
    else
      usage(argv[0], stderr, 1, argv[i]);
  }

  if ( cfg.highspeed ) {
    fail("--hs mode is broken");
#if 0
    switch ( cfg.standard ) {
    case config::DVB_S:  return run_highspeed(cfg);
    case config::DVB_S2: return run_highspeed_s2(cfg);
    }
#endif
  }
  switch ( cfg.standard ) {
  case config::DVB_S:  return run_dvbs(cfg);
  case config::DVB_S2: return run_dvbs2(cfg);
  }
}

As you can see it manages all the possible flags that you can give as parameters to the command line, hoever i cannot understand how the file DVBS_50Ksym_800Ksps_50KHz_IF.iq is processed since it seems it is not managed, read or processed anywhere.

I'm sorry for my poor explanations, i'll try to give more informations if needed.

EDIT:

As suggested by Nate i've found that probably the file is managed through stdin, however there are a lot of nested functions, classes and types such as:

case config::INPUT_U8: {
      pipebuf<cu8> *p_stdin =
    new pipebuf<cu8>(sch, "stdin", BUF_BASEBAND+cfg.input_buffer);
      file_reader<cu8> *r_stdin =
    new file_reader<cu8>(sch, 0, *p_stdin);
      r_stdin->loop = cfg.loop_input;
      cconverter<u8,128, f32,0, 1,1> *r_convert =
    new cconverter<u8,128, f32,0, 1,1>(sch, *p_stdin, *p_rawiq);
      amp = 128;
      break;
    }

Currently i'm passing a binary(.IQ) file to the executable, however in order to implement this code where needed i'll be in need to avoid using files and use a buffer instead, it can be a buffer of char or float or any kind, how can i force the stdin to get my char* buffer that i am managing inside C++ as the item, so that the following code will access it from stdin instead of the file given with the shell redirection? is it possible from within C++ code?

Aucun commentaire:

Enregistrer un commentaire