www.pudn.com > eval-1.2.zip > read.c



#include 
#include 
#include 
#include 

#include "def.h"
#include "misc.h"
#include "read.h"

static const char FMT_T[] = "%*d %c %d%d%*[^\n]\n";

int ReadDump(char *fl[], data_t *D, MODE mode, int off)
{
	static dump_t null;
	FILE *f = 0;
	double t = 0, tmax;
	unsigned long i, ti, line, id = 0;
	char buf[0x1000], *p, *e, j, c;
	int l=0, h, m, lost;
	size_t num = 0;
	void *tmp;
	int nlf=0, nlfh=0;
	
	if ((f = fopen(fl[1], "r")) == 0) {
		fprintf(stderr, "error opening %s\n", fl[1]);
		return 0;
	}
	
	line = 0;
	while (c = 0, line++, !feof(f) && !ferror(f)) {
		if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) break;
		if (*buf == '\n') continue;
		
		if (!(t = strtod(buf, &e)) && e == buf) { c = 1; goto e1; }
		if (!(p = skips(buf, " id ", 1))) {
			if (!(p = skips(buf, "frag ", 1))) { c = 1; goto e1; }
		}
		if (!(id = strtoul(p, &e, 10)) && e == p) { c = 1; goto e1; }
		if (!(p = skips(buf, " udp ", 1)))
			if (!(p = skips(buf, "UDP, length:", 1)))
				if (!(p = skips(buf, "UDP, length", 1))) { c = 1; goto e1; }
				if (!(l = strtoul(p, &e, 10)) && e == p) c = 1;
				
e1: if (c) {
		fprintf(stderr, "malformed input (%s, %lu)\n", fl[1], line);
		continue;
    }
	
    if (++D->nP > num) {
		num += 16384;
		if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
			fprintf(stderr, "realloc error\n");
			return 0;
		}
		D->P = tmp;
    }
	
    D->P[D->nP-1] = null;
    D->P[D->nP-1].id = id;
    D->P[D->nP-1].t1 = t;
    D->P[D->nP-1].size = l - off;
    D->P[D->nP-1].lost = 1;
    D->P[D->nP-1].type = ' ';
	}
	fclose(f);
	
	if (mode != GEN) {
		if ((f = fopen(fl[2], "r")) == 0) {
			fprintf(stderr, "error opening %s\n", fl[2]);
			return 0;
		}
		
		line = m = 0;
		while (lost=0, c=0, line++, !feof(f) && !ferror(f)) {
			if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) 
				break;
			if (*buf == '\n') 
				continue;
			
			if (!(t = strtod(buf, &e)) && e == buf) { 
				c = 1; goto e2; 
			}
			if (!(p = skips(buf, " id ", 1))) { 
				c = 1; 
				goto e2; 
			}
			if (!(id = strtoul(p, &e, 10)) && e == p) { 
				c = 1; 
				goto e2; 
			}
			
e2:         if (c) {
				fprintf(stderr, "malformed input (%s, %lu)\n", fl[2], line);
				continue;
			}
			
			if (p = skips(buf, " lost ", 1)) 
				lost = strtoul(p, 0, 10); /* with lost marking 9999 the .lost char value is 15 (because 9999=0x270f)*/
			
			for (i=m; inP; i++) 
				if (id == D->P[i].id) {
					D->P[i].t2 = t;
					D->P[i].lost = lost; /* will be 0 (not lost) or 15=0x0f (lost) */
					if (i > 100) 
						m = i - 100;
					break;
				}
		} /* End while */		
		fclose(f);
	}
		
	if (mode != RAW) {
		if ((f = fopen(fl[3], "r")) == 0) {
			fprintf(stderr, "error opening %s\n", fl[3]);
			return 0;
		}
		
		i = 0;
		line = num = 0;
		while (c = 0, line++, !feof(f) && !ferror(f)) {
			if (3 != fscanf(f, FMT_T, &c, &m, &l)) {
				if (!feof(f)) {
					fprintf(stderr, "malformed input (%s, %lu)\n", fl[3], line);
					continue;
				} else break;
			}
			
			if (i >= D->nP) {
				fprintf(stderr, "%s incomplete\n", fl[1]);
				return 0;
			}
			
			ti = i;
			tmax = D->P[i].lost ? 0 : D->P[i].t2; /* tmax set to 0 only if packet was lost */
			for (j=0, h=0; hP[i].lost : D->P[i].lost && (h == 0 || c == 'B' || c == 'P');
				if (!D->P[i].lost && D->P[i].t2 > tmax) 
					tmax = D->P[i].t2; /* this is to find the arrival of the last packet in the frame */
				D->P[i++].type = c;
			}
			
			if (++D->nF > num) {
				num += 16384;
				if ((tmp = realloc(D->F, num * sizeof *D->F)) == 0) {
					fprintf(stderr, "realloc error\n");
					return 0;
				}
				D->F = tmp;
			}
			
			D->F[D->nF-1] = null;
			D->F[D->nF-1].size = m;
			D->F[D->nF-1].lost = j;
			if (j != 0) {
				nlf++;
				D->F[D->nF-1].lost = 1; /* Change as of 30.03.06 so that values > 1 can be used to signal delay drops */
			}
			D->F[D->nF-1].type = c;
			if (j!=0 && c == 'H')
				nlfh++;
				/* printf("Header Frame Detected at frame No. %d\n",D->nF-1); */
			
			D->F[D->nF-1].segm = l;
			D->F[D->nF-1].t1 = mode == FRAME ? D->P[ti].t1 : D->P[i-1].t1;
			D->F[D->nF-1].t2 = tmax;
			/*
			if (typ == 1) {
			if (!I)
			if (D->F[D->nF-1].type == 'I' || D->F[D->nF-1].type == 'P')
            if (!D->F[D->nF-1].lost)
			I = 1;
			if (!I && D->nF-1)
			D->F[D->nF-1].lost = 1;
			}
			*/
		}
		fclose(f);
		printf("#of lost frames: %d. Among them %d Header Frames\n", nlf, nlfh);
    
	}
	
	return 1;
}

void CalcSNR(dump_t *D, unsigned l)
{
  unsigned i;

  for (i=0; inF - 1) * sizeof *pF);
	
	if (pF == 0) return 0;
	i = 1; /* Skip frame 0 which is H-frame (start at j=1) */
	for (j=1; jnF; j++ )
		switch (D->F[j].type) {
      case 'I': 
		  if (j == 1) 
			  pF[0] = D->F[j];
      case 'P': 
		  if (i + nB-1 < D->nF - 1) 
			  pF[i + nB-1] = D->F[j];
		  else 
			  pF[D->nF - 2] = D->F[j];
		  i++;
		  break;
      case 'B': 
		  if (i >= 2) 
			  pF[i-2] = D->F[j];
		  i++;
      default :  
		  break;
    }
	
	free(D->F);
	D->F = pF;
	
	return (i-1);
}

unsigned long MaxPackSize(dump_t *P, unsigned l)
{
  unsigned i;
  unsigned long max = P[0].size;

  for (i=1; i max) max = P[i].size;

  return 8 * max;
}

int GetNumPackets(tcpdump_t *P, unsigned l, DIR dir)
{
  unsigned i = 0;

  while (l--) if (!P[l].lost[dir]) i++;

  return i;
}

int ReadIni(char *fn, param_t *par)
{
  FILE *f;
  char line[0x1000];

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  while (fgets(line, sizeof line, f) && !feof(f) && !ferror(f)) {
    if (getuint (line, "ofdmsystem.terminals",                             &par->wt)) ; else
    if (getuint (line, "ofdmsystem.subbands",                              &par->subs)) ; else
    if (getuint (line, "ofdmsystem.sbprocessingunit.scheduler.assign_alg", &par->alg)) ; else
    if (getstr  (line, "ofdmsystem.sbprocessingunit.subband.input_file",   &par->chn_file)) ; else
    if (getuint (line, "ofdmsystem.wt.wt_stat.sig_flag",                   &par->sig)) ; else
    if (getstr  (line, "ofdmsystem.sd1",                                   &par->sd[0])) ; else
    if (getstr  (line, "ofdmsystem.st1",                                   &par->st[0])) ; else
    if (getstr  (line, "ofdmsystem.sd2",                                   &par->sd[1])) ; else
    if (getstr  (line, "ofdmsystem.st2",                                   &par->st[1])) ; else
    if (getstr  (line, "ofdmsystem.rd1",                                   &par->res_path[0])) ; else
    if (getstr  (line, "ofdmsystem.rd2",                                   &par->res_path[1])) ; else
    if (getdbl  (line, "ofdmsystem.mix",                                   &par->mix)) ; else
    if (getstate(line, "ofdmsystem.vbr",                                   &par->vbr)) ; else
    if (getdbl  (line, "ofdmsystem.deadline",                              &par->deadline)) ; else
    if (getstate(line, "ofdmsystem.prio",                                  &par->prio)) ; else
    if (getstate(line, "ofdmsystem.vrth",                                  &par->vrth)) ; else
    if (getdbl  (line, "ofdmsystem.i_dl",                                  &par->I_dl)) ; else
    if (getdbl  (line, "ofdmsystem.p_dl",                                  &par->P_dl)) ; else
    if (getdbl  (line, "ofdmsystem.b_dl",                                  &par->B_dl)) ; else
    if (getdbl  (line, "ofdmsystem.i_wgt",                                 &par->I_wgt)) ; else
    if (getdbl  (line, "ofdmsystem.p_wgt",                                 &par->P_wgt)) ; else
    if (getdbl  (line, "ofdmsystem.b_wgt",                                 &par->B_wgt)) ;
  }

  fclose(f);

  return 1;
}

int ReadSD(char *fn, snr_t *D, DIR dir)
{
  FILE *f = 0;
  double t = 0;
  unsigned long i, j, line, id = 0;
  char buf[0x1000], *p, *e, c, *ip[] = {"> 192.168.0.153.", "> 192.168.0.154."};
  size_t num = 0;
  void *tmp;
  static tcpdump_t init; init.lost[0] = init.lost[1] = 1;

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  i = j = line = 0;
  while (c = 0, line++, !feof(f) && !ferror(f)) {
    if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) break;

    if (strstr(buf, "icmp:")) continue;   /* skip icmp packets */
    if (strstr(buf, " arp ")) continue;   /* skip arp packets */
    switch (dir) {
      case MATCH_IDS:
        if (!strstr(buf, ip[FROM_MOBILE])) continue;
        break;
      case TO_MOBILE:
      case CHECK_IDS:
        if (!strstr(buf, ip[TO_MOBILE])) continue;
    }
    if (!(t = strtod(buf, &e)) && e == buf) { c = 1; goto e1; }   /* time stamp */
    if (!(p = skips(buf, " id ", 1)) || !(id = strtoul(p, &e, 10)) && e == p) continue;   /* IP ID */

e1: if (c) {
      fprintf(stderr, "malformed input (%s, %lu)\n", fn, line);
      continue;
    }

    switch (dir) {
      case TO_MOBILE:   /* next sent packet at mobile */
        if (++D->nP > num) {
          num += 16384;
          if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
            fprintf(stderr, "realloc error\n");
            return 0;
          }
          D->P = tmp;
        }
        D->P[D->nP-1] = init;
        D->P[D->nP-1].id[dir] = id;
        D->P[D->nP-1].tr[0] = t;
        break;
      case FROM_MOBILE:
        break;
      case MATCH_IDS:   /* match id's in mobile sd */
        if (j > D->nP-1) {
          fprintf(stderr, "Hmmpf: More packets answered than received!\n");
          return 1;
        }
        D->P[j].id[FROM_MOBILE] = id;
        D->P[j].ts[1] = t;
        j++;
        break;
      case CHECK_IDS:   /* check base sender id's */
        for (i=j; inP; i++) {
          if (id == D->P[i].id[TO_MOBILE]) {
            D->P[i].ts[0] = t;
            D->P[i].insd[TO_MOBILE] = 1;
            if (i > 2500) j = i - 2500;
            break;
          }
        }
        break;
    }
  }

  fclose(f);

  return 1;
}

int ReadRD(char *fn, snr_t *D, DIR dir)
{
  FILE *f = 0;
  double t = 0;
  unsigned long line, i, m, id = 0;
  char buf[0x1000], *p, *e, c;
  int signal, noise;

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  m = line = 0;
  while (c = 0, line++, !feof(f) && !ferror(f)) {
    if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) break;

    if (!(t = strtod(buf, &e)) && e == buf) { c = 1; goto e1; }
    if (!(p = skips(buf, "s:", 1)) || !(signal = strtoul(p, &e, 10)) && e == p) { c = 1; goto e1; }
    if (!(p = skips(buf, "n:", 1)) || !(noise = strtoul(p, &e, 10)) && e == p) { c = 1; goto e1; }
    if (!(p = skips(buf, " id ", 1)) || !(id = strtoul(p, &e, 10)) && e == p) { c = 1; goto e1; }

e1: if (c) {
      fprintf(stderr, "malformed input (%s, %lu)\n", fn, line);
      continue;
    }

    for (i=m; inP; i++) {
      if (id == D->P[i].id[dir]) {
        switch (dir) {
          case TO_MOBILE:
            if (t != D->P[i].tr[0]) D->P[i].insd[1] = 1;
          case FROM_MOBILE:
            D->P[i].tr[1] = t;
            break;
        }
        D->P[i].noise[dir] = noise;
        D->P[i].signal[dir] = SCALEDB(signal);
        D->P[i].snr[dir] = FROMDB(D->P[i].signal[dir]);
        D->P[i].lost[dir] = 0;
        if (i > 2500) m = i - 2500;
        break;
      }
    }
  }

  fclose(f);

  return 1;
}

int ReadTCPDump(char *fn, cDump_t *D)
{
  FILE *f = 0;
  double t = 0;
  unsigned long line, l, id = 0;
  char buf[0x1000], *p, *e, c, dir = 0;
  size_t num = 0;
  void *tmp;
  static TCPdump_t null;

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  line = 0;
  while (c = 0, line++, !feof(f) && !ferror(f)) {
    if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) break;

    if (!(t = strtod(buf, &e)) && e == buf) { c = 1; goto e1; }
    if (!(p = skips(buf, " id ", 1)) || !(id = strtoul(p, &e, 10)) && e == p) { c = 1; goto e1; }
    if (!(p = skips(buf, " udp ", 1))) { c = 1; goto e1; }
    if (!(l = strtoul(p, &e, 10)) && e == p) c = 1;

e1: if (c) {
      fprintf(stderr, "malformed input (%s, %lu)\n", fn, line);
      continue;
    }

    if (!dir && ++D->nP > num) {
      num += 16384;
      if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
        fprintf(stderr, "realloc error\n");
        return 0;
      }
      D->P = tmp;
    }

    if (!dir) D->P[D->nP-1] = null;
    D->P[D->nP-1].size = l;
    D->P[D->nP-1].id[dir] = id;
    D->P[D->nP-1].t[dir] = t;
     dir = !dir;
  }

  fclose(f);

  return 1;
}

int ReadAVG(char *fn, avg_t *D)
{
  FILE *f = 0;
  double t1, t2, sig1, sig2, avg1, avg2;
  unsigned long line;
  char buf[0x1000];
  size_t num = 0;
  void *tmp;
  static AVG_t null;

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  line = 0;
  fgets(buf, sizeof buf, f);
  while (line++, !feof(f) && !ferror(f)) {
    if (!fgets(buf, sizeof buf, f) || feof(f) || ferror(f)) break;

    if (6 != sscanf(buf, "%lf %lf %lf %*d %lf %*d %lf %lf", &t1, &t2, &sig1, &sig2, &avg1, &avg2)) {
      fprintf(stderr, "malformed input (%s, %lu)\n", fn, line);
      continue;
    }

    if (++D->nP > num) {
      num += 16384;
      if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
        fprintf(stderr, "realloc error\n");
        return 0;
      }
      D->P = tmp;
    }

    D->P[D->nP-1] = null;
    D->P[D->nP-1].t[0] = t1;
    D->P[D->nP-1].t[1] = t2;
    D->P[D->nP-1].signal[0] = sig1;
    D->P[D->nP-1].signal[1] = sig2;
    D->P[D->nP-1].avg[0] = avg1;
    D->P[D->nP-1].avg[1] = avg2;
  }

  fclose(f);

  return 1;
}

int ReadLin(char *fn, avg_t *D)
{
  FILE *f = 0;
  double t, sig1, sig2;
  unsigned long line;
  char buf[0x1000];
  size_t num = 0;
  void *tmp;
  static AVG_t null;

  if ((f = fopen(fn, "r")) == 0) {
    fprintf(stderr, "error opening %s\n", fn);
    return 0;
  }

  line = 0;
  while (line++, fgets(buf, sizeof buf, f) && !feof(f) && !ferror(f)) {
    if (3 != sscanf(buf, "%lf,%lf,%lf", &t, &sig1, &sig2)) {
      fprintf(stderr, "malformed input (%s, %lu)\n", fn, line);
      continue;
    }

    if (++D->nP > num) {
      num += 16384;
      if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
        fprintf(stderr, "realloc error\n");
        return 0;
      }
      D->P = tmp;
    }

    D->P[D->nP-1] = null;
    D->P[D->nP-1].t[0] = t;
    D->P[D->nP-1].t[1] = t;
    D->P[D->nP-1].signal[0] = sig1;
    D->P[D->nP-1].signal[1] = sig2;
  }

  fclose(f);

  return 1;
}