Exemple #1
0
static bool parse_listen_faces(char str[], ccarray_t * faces)
{
  static const char delims[] = " \t\n,;";
  char * tok;

  tok = strtok(str, delims);
  while ( tok && ccarray_size(faces) < ccarray_capacity(faces) ) {

    uint32_t address = 0;
    uint16_t port = 0;

    if ( getifaddr(tok, &address, &port) == -1 ) {
      fprintf(stderr, "FATAL: Can't get address for '%s': %s\n", tok, strerror(errno));
      fprintf(stderr, "Check if device name is valid and device is up\n");
      return false;
    }

    if ( !port ) {
      fprintf(stderr, "No port specified in '%s'\n", tok);
      return false;
    }

    ccarray_push_back(faces, &(struct sockaddr_in ) {
          .sin_family = AF_INET,
          .sin_addr.s_addr = htonl(address),
          .sin_port = htons(port),
          .sin_zero = { 0 }
        });

    tok = strtok(NULL, delims);
  }
/* load lines from input file */
static int load_objects(FILE * input, int rc, int dc, int ru, int du, char header[MAX_HEADER_LENGTH],
    ccarray_t * objects)
{
  char line[MAX_INPUT_LINE_LENGTH];
  int ic;
  char * pc;
  size_t size;
  size_t capacity;

  obj_t * obj;


  header[MAX_HEADER_LENGTH-1] = 0;

  if ( !fgets(header, MAX_HEADER_LENGTH, input) ) {
    fprintf(stderr,"fgets(header) fails: %d (%s)\n", errno, strerror(errno));
    return -1;
  }

  if ( header[MAX_HEADER_LENGTH - 1] != 0 ) {
    fprintf(stderr,"too long header line in this file\n");
    return -1;
  }

  /* remove trailing new line */
  header[strlen(header) - 1] = 0;


  size = ccarray_size(objects);
  capacity = ccarray_capacity(objects);

  while ( size < capacity && !feof(input) )
  {
    obj = ccarray_peek(objects, size);

    line[MAX_INPUT_LINE_LENGTH-1] = 0;
    if ( !fgets(line, MAX_INPUT_LINE_LENGTH, input) ) {
      break;
    }

    if ( line[MAX_INPUT_LINE_LENGTH - 1] != 0 ) {
      fprintf(stderr,"too long input line in this file\n");
      return -1;
    }

    /* remove trailing new line */
    line[strlen(line) - 1] = 0;



    for ( pc = line, ic = 1; ic < rc; ++ic ) {
      if ( !(pc = strchr(pc + 1, '\t')) ) {
        break;
      }
    }
    if ( ic != rc || sscanf(pc, " %lf", &obj->ra) != 1 ) {
      continue;
    }


    for ( pc = line, ic = 1; ic < dc; ++ic ) {
      if ( !(pc = strchr(pc + 1, '\t')) ) {
        break;
      }
    }
    if ( ic != dc || sscanf(pc, " %lf", &obj->dec) != 1 ) {
      continue;
    }

    if ( ru == degrees ) {
      obj->ra *= PI / 180;
    }

    if ( du == degrees ) {
      obj->dec *= PI / 180;
    }

    obj->line = strdup(line);
    ccarray_set_size(objects, ++size );
  }

  return 0;
}