示例#1
0
static ifo_handle_t *
ifoReadVGMI(int file, ifo_handle_t *ifofile)
{
	off_t	offset;
	Uint	counter;
	UInt32_t sector;
	UInt16_t titles;

	vmgi_mat_t *vmgi_mat;
	tt_srpt_t *tt_srpt;

	/* Make the VTS part null */
	ifofile->vtsi_mat = NULL;

	vmgi_mat = (vmgi_mat_t *)e_malloc(sizeof (vmgi_mat_t));
	if (!vmgi_mat) {
/*		fprintf(stderr, "Memmory allocation error\n");*/
		free(ifofile);
		return (0);
	}

	ifofile->vmgi_mat = vmgi_mat;

	/* Last sector of VMG i.e. last sector of BUP */

	offset = 12;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		ifoClose(ifofile);
		return (0);
	}

	if (read(file, &sector, sizeof (sector)) != sizeof (sector)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		ifoClose(ifofile);
		return (0);
	}

	B2N_32(sector);

	vmgi_mat->vmg_last_sector = sector;

	/* Last sector of IFO */

	offset = 28;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		ifoClose(ifofile);
		return (0);
	}


	if (read(file, &sector, sizeof (sector)) != sizeof (sector)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		ifoClose(ifofile);
		return (0);
	}

	B2N_32(sector);

	vmgi_mat->vmgi_last_sector = sector;


	/* Number of VTS i.e. title sets */

	offset = 62;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		ifoClose(ifofile);
		return (0);
	}


	if (read(file, &titles, sizeof (titles)) != sizeof (titles)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		ifoClose(ifofile);
		return (0);
	}

	B2N_16(titles);

	vmgi_mat->vmg_nr_of_title_sets = titles;


	/* Star sector of VMG Menu VOB */

	offset = 192;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		ifoClose(ifofile);
		return (0);
	}


	if (read(file, &sector, sizeof (sector)) != sizeof (sector)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		ifoClose(ifofile);
		return (0);
	}

	B2N_32(sector);

	vmgi_mat->vmgm_vobs = sector;


	/* Sector offset to TT_SRPT */

	offset = 196;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		ifoClose(ifofile);
		return (0);
	}


	if (read(file, &sector, sizeof (sector)) != sizeof (sector)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		ifoClose(ifofile);
		return (0);
	}

	B2N_32(sector);

	vmgi_mat->tt_srpt = sector;

	tt_srpt = (tt_srpt_t *)e_malloc(sizeof (tt_srpt_t));
	if (!tt_srpt) {
/*		fprintf(stderr, "Memmory allocation error\n");*/
		ifoClose(ifofile);
		return (0);
	}

	ifofile->tt_srpt = tt_srpt;


	/* Number of titles in TT_SRPT */

	offset = 2048 * vmgi_mat->tt_srpt;

	if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGESEEK);
#else
		printf(stderr, MSGESEEK);
#endif
		return (0);
	}

	if (read(file, &titles, sizeof (titles)) != sizeof (titles)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		return (0);
	}

	B2N_16(titles);

	tt_srpt->nr_of_srpts = titles;

	tt_srpt->title = (title_info_t *)e_malloc(sizeof (title_info_t) * tt_srpt->nr_of_srpts);
	if (!tt_srpt->title) {
/*		fprintf(stderr, "Memmory allocation error\n");*/
		ifoClose(ifofile);
		return (0);
	}

	/* Start sector of each title in TT_SRPT */

	for (counter = 0; counter < tt_srpt->nr_of_srpts; counter++) {
		offset = (2048 * vmgi_mat->tt_srpt) + 8 + (counter * 12) + 8;
		if (lseek(file, offset, SEEK_SET) != offset) {
#ifdef	USE_LIBSCHILY
			errmsg(MSGESEEK);
#else
			printf(stderr, MSGESEEK);
#endif
			ifoClose(ifofile);
			return (0);
		}

		if (read(file, &sector, sizeof (sector)) != sizeof (sector)) {
#ifdef	USE_LIBSCHILY
			errmsg(MSGEREAD);
#else
			printf(stderr, MSGEREAD);
#endif
			ifoClose(ifofile);
			return (0);
		}

		B2N_32(sector);

		tt_srpt->title[counter].title_set_sector = sector;

	}
	return (ifofile);
}
示例#2
0
ifo_handle_t *
ifoOpen(dvd_reader_t *dvd, int title)
{
	/* The main ifofile structure */
	ifo_handle_t *ifofile;

	/* File handles and offset */
	int file;
	off_t offset;
	char full_path[ PATH_MAX + 1 ];

	/* Identifier of the IFO */
	char identifier[13];

	identifier[0] = '\0';

	ifofile = (ifo_handle_t *)e_malloc(sizeof (ifo_handle_t));

	memset(ifofile, 0, sizeof (ifo_handle_t));

	if (title) {
		snprintf(full_path, sizeof (full_path),
				"%s/VIDEO_TS/VTS_%02d_0.IFO", dvd->path_root, title);
	} else {
		snprintf(full_path, sizeof (full_path),
				"%s/VIDEO_TS/VIDEO_TS.IFO", dvd->path_root);
	}

	if ((file = open(full_path, O_RDONLY | O_BINARY)) == -1) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEOPEN);
#else
		printf(stderr, MSGEOPEN);
#endif
		free(ifofile);
		return (0);
	}

	offset = 0;

	/* Determine if we have a VMGI or VTSI */

	if (read(file, identifier, sizeof (identifier)) != sizeof (identifier)) {
#ifdef	USE_LIBSCHILY
		errmsg(MSGEREAD);
#else
		printf(stderr, MSGEREAD);
#endif
		return (0);
	}

	if ((strstr("DVDVIDEO-VMG", identifier) != 0) && (title == 0)) {
		ifofile = ifoReadVGMI(file, ifofile);
		close(file);
		return (ifofile);
	} else if ((strstr("DVDVIDEO-VTS", identifier) != 0) && (title != 0)) {
		ifofile = ifoReadVTSI(file, ifofile);
		close(file);
		return (ifofile);
	} else {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "Giving up this is not a valid IFO file\n");
#else
		fprintf(stderr, "Giving up this is not a valid IFO file\n");
#endif
		close(file);
		free(ifofile);
		ifofile = 0;
		return (0);
	}
}
示例#3
0
文件: compiler.c 项目: vroman/edivc
void compila()
{
  struct objeto *i;
  FILE *f;
  int n, l;
  unsigned long m;
  byte *q, *p;
  int start_lin, start_dbg;
  byte *varptr; /* variables indexadas */
  FILE *fvar;

  ultima_linea = prog;
  acceso_remoto = 0;
  parametros = 0;
  linea = 1;
  linf = NULL;

  print_translate(27);

  /* inicio_textos debe ser como mínimo 256 */
  if (imem < 256)
    imem = 256;
  itxt = inicio_textos = imem;
  psintactico(); /* Para obtener "longitud_textos" */
  imem += longitud_textos;
#ifdef _DEBUG
  printf("dbg: longitud_textos: %d\n", longitud_textos);
#endif

  test_buffer(&mem, &imem_max, imem);

  if (n_errors > 0)
    return;

  num_obj_predefinidos = num_obj;

  ultima_linea = source;
  acceso_remoto = 0;
  parametros = 0;
  linea = 1;

  sintactico();

  i = obj;
  while (i < iobj)
  {
    if (i->usado)
    {
      linea = i->linea;
      ierror = i->ierror;
      error(0, 34, i->name); /* nombre desconocido */
    }
    i++;
  }

  if (n_errors > 0)
    return;

  /* Borra todo y comienza de nuevo :P */

  print_translate(28);

  if (frm != NULL)
  {
    free(frm);
    frm = NULL;
  }
  if (loc != NULL)
  {
    free(loc);
    loc = NULL;
  }
  if (mem != NULL)
  {
    free(mem);
    mem = mem_ory = NULL;
  }
  if (vnom != NULL)
  {
    free(vnom);
    vnom = NULL;
  }

  prepara_compilacion();
  source = prog;

  inicializa_index(); /* ahora toca construir el indice de variables */
  dll_func2();        /* recarga sólo las dlls necesarias */

  if (debug)
  {
    if ((linf = tmpfile()) == NULL)
    {
      print_translate(29);
      exit(1);
    }
  }

  /* inicio_textos debe ser como mínimo 256 */
  if (imem < 256)
    imem = 256;
  itxt = inicio_textos = imem;
  imem += longitud_textos;
  test_buffer(&mem, &imem_max, imem);
  num_obj_predefinidos = num_obj;
  acceso_remoto = 0;
  parametros = 0;
  linea = 1;

  sintactico();

  /* 
	 * Ahora que estamos en el final del bytecode, añadiremos la rutina que carga
	 * las DLLs. Lo primero es guardar las cadenas con los nombres:
	 */
  for (n = 0; n < numdlls; n++)
    if (dlls[n].usado || dlls[n].prioridad >= P_SIEMPRE)
    {
      dlls[n].mem_nombre = imem;
      l = strlen(dlls[n].nombre);
      memcpy(&mem[imem], dlls[n].nombre, l + 1);
      imem += (l + 4) / 4;
      test_buffer(&mem, &imem_max, imem);
    }

  /* Si estamos compilando en modo debug, añadimos tambien la debug.dll */
  if (debug)
  {
    dlls[numdlls].mem_nombre = imem;
    memcpy(&mem[imem], "debug", 6);
    imem += 2;
    test_buffer(&mem, &imem_max, imem);
  }

  /* 
	 * Ahora estamos en la posición donde comienza la rutina, por lo que tenemos
	 * que guardar la posición actual en el offset que guardamos en salto_import
	 */
  mem[salto_import] = imem;

  /* Escribimos la rutina de carga de DLLs */
  for (n = 0; n < numdlls; n++)
    if (dlls[n].usado || dlls[n].prioridad >= P_SIEMPRE)
      g2(limp, dlls[n].mem_nombre);

  if (debug)
    g2(limp, dlls[numdlls].mem_nombre);

  g2(ljmp, salto_import + 1);

  /* Ya está !! :) */

  /* Preparamos la cabecera del bytecode */
  mem[2] = imem;
  mem[3] = max_process; /* Antes long_header, ahora no se utiliza */
  mem[4] = 0;           /* Antes mem[1]-mem[3] (long datos globales), ahora no se utiliza */
  mem[5] = iloc_len - iloc;
  mem[6] = iloc;
  mem[7] = 0;           /* Antes imem+iloc (inicio textos), ahora no se utiliza */
  mem[8] = imem + iloc; /* Número de elementos ocupados en mem[] */

  /*
	 * mem[0] se usa para almacenar flags para el ejecutable. En el caso de DIV 2,
	 * éstas eran:
	 * +1	= El programa es un setup de sonido (setup_program)
	 * +128	= El programa invoca al trazador nada más ejecutarse (compilado con F12)
	 * +512	= Se ignoran los errores de ejecución (ignore_errors)
	 * +1024= Modo DEMO (mensaje en el centro de la pantalla parpadeando diciendo
	 *		  "VERSIÓN DE DEMOSTRACIÓN")
	 */

  /*
	 * nosotros usaremos las siguientes:
	 * +16	= El exe lleva incluidas las DLL's
	 *        A ver como lo hacemos en Linux.. 1) con temporales o 2) hurgando en el codigo
	 *        de dlopen y demas y haciendonos una pekeña lib (esto es mas aconsejable)
	 * +32	= El exe lleva incluido el PAK
	 * +64	= Compilado en modo debug
	 * +128 = Igual que en DIV2, el programa invoca al trazador nada más ejecutarse (se
	 *        ha usado la orden "trazar programa" en el IDE)
	 * +512	= ignore_errors, igual que en DIV2
	 */

  mem[0] = 0;
  if (debug)
    mem[0] += 64;
  if (ignore_errors)
    mem[0] += 512;

  /*
	 * Generamos los listados (debe hacerse ahora porque en el listado de objetos guardamos 
	 * los valores de mem[1..8])
	 */
  if (listados)
  {
    print_translate(30);
    if (listados & 1)
      listado_ensamblador();
    if (listados & 2)
      listado_objetos();
  }

  if (noexe)
    return;

  print_translate(31);

  /* descomprime la edivrun.lib y guarda una copia con el nombre temp.dj! */
  _encriptar(0, edivrun_lib, la_clave);
  _comprimir(0, "temp.dj!");

  /* si el archivo de salida ya existe, lo borra */
  if ((f = fopen(outfilename, "rb")) != NULL)
  {
    fclose(f);
    remove(outfilename);
  }

  /* renombra temp.dj! al nombre del exe */
  if (rename("temp.dj!", outfilename))
  {
    print_translate(33); /* error escribiendo ejecutable */
    remove("temp.dj!");
    exit(1);
  }

  /* ordenamos varindex */
  ordena_varindex();

  /* escribimos en un temporal todo el indice de variables */
  fvar = tmpfile();
  for (n = 0; n < num_indexed_vars; n++)
  {
    fputc(varindex[n].hash, fvar);
    fwrite(varindex[n].nombre, 1, strlen(varindex[n].nombre) + 1, fvar);
    fwrite(&varindex[n].offset, 1, 4, fvar);
    fputc(varindex[n].tipo, fvar);
  }

  /* liberamos varindex */
  for (n = 0; n < num_indexed_vars; n++)
    free(varindex[n].nombre);
  free(varindex);

  /* lo pasamos todo del temporal a la memoria */
  l = ftell(fvar);
  fseek(fvar, 0, SEEK_SET);
  varptr = e_malloc(l);
  fread(varptr, 1, l, fvar);
  fclose(fvar);
#ifdef _DEBUG
  if (fvar = fopen("varindex.out", "wb"))
  {
    fwrite(varptr, 1, l, fvar);
    fclose(fvar);
  }
#endif

  if ((f = fopen(outfilename, "ab")) != NULL)
  {
    fwrite(nombre_program, strlen((const char *)nombre_program) + 1, 1, f);
    p = e_malloc((imem + iloc) * 4);
    m = (imem + iloc) * 4 + 1024;
    q = e_malloc(m);
    if (p != NULL && q != NULL)
    {
      fwrite(mem, 4, 9, f); /* mem[0..8] */
      memcpy(p, &mem[9], (imem - 9) * 4);
      memcpy(p + (imem - 9) * 4, loc, iloc * 4);
      n = (imem - 9 + iloc) * 4;
      if (!compress(q, &m, p, n))
      {
        fwrite(&n, 1, 4, f); /* mem[0]..mem[8],longitud_datos_descomp,longitud_datos_comp,datos comp... */
        fwrite(&m, 1, sizeof(unsigned long), f);
        fwrite(q, 1, m, f);
        free(q);
        free(p);
        m = l * 2;
        q = e_malloc(m);
        if (!compress(q, &m, varptr, l))
        { /* nºvariables,longitud_datos_descomp,longitud_datos_comp,datos_comp... */
          fwrite(&num_indexed_vars, 1, 4, f);
          fwrite(&l, 1, 4, f);
          fwrite(&m, 1, sizeof(unsigned long), f);
          fwrite(q, 1, m, f);
          free(q);
          free(varptr);
          if (debug)
          { /* formato de ejecutable de debug */
            print_translate(32);
            start_lin = ftell(f);
            escribe_lin(f);
            start_dbg = ftell(f);
            escribe_dbg(f);
            fwrite(&start_lin, 1, 4, f);
            fwrite(&start_dbg, 1, 4, f);
          }
          fwrite(&stub_size, 1, 4, f); /* Ultimos 4 bytes siempre son el tamaño del stub */
          fclose(f);
        }
        else
        {
          free(q);
          free(varptr);
          fclose(f);
          errormem();
        }
      }
      else
      {
        free(q);
        free(p);
        free(varptr);
        fclose(f);
        errormem();
      }
    }
    else
    {
      if (p != NULL)
        free(p);
      if (q != NULL)
        free(q);
      free(varptr);
      fclose(f);
      errormem();
    }
  }
  else
  {
    free(varptr);
    print_translate(33);
    exit(1);
  }

#ifndef _WIN32
  chmod(outfilename, 493); /* -rwxr-xr-x */
#endif

  print_translate(34);
}
示例#4
0
文件: compiler.c 项目: vroman/edivc
void prepara_compilacion()
{
  int n;

  vnom = NULL;

  mem_ory = NULL;
  frm = NULL;
  mem = NULL;
  loc = NULL;

  itbreak = 0;
  itcont = 0;
  itelseif = 0;
  coment = 0;
  cero = 0;
  error_25 = 25;

  memset(vhash, 0, 256 * sizeof(byte *));

  inicializa_lower();

  /* Inicializamos la tabla de objetos */
  memset(obj, 0, sizeof(obj[0]) * max_obj);
  iobj = obj;
  num_obj = 0;

  /* Inicializamos la tabla de símbolos */
  memset(lex_simb, 0, sizeof(lex_simb));
  ilex_simb = lex_simb;
  num_nodos = 0;

  /* Inicializamos los caracteres básicos en lex_case */
  for (n = 0; n < 256; n++)
    if (lower[n])
    {
      if (n >= '0' && n <= '9')
        lex_case[n] = (struct lex_ele *)l_num;
      else
        lex_case[n] = (struct lex_ele *)l_id;
    }
    else
      lex_case[n] = (struct lex_ele *)l_err;

  /* Inicializamos el vector de nombres */
  vnom = e_malloc(max_obj * long_med_id + 1024);
  ivnom.b = vnom;

  /* Leemos los símbolos y palabras reservadas de ltlex.def */
  analiza_ltlex();

  /* Terminamos de inicializar lex_case */
  lex_case[' '] = (struct lex_ele *)l_spc;
  lex_case[tab] = (struct lex_ele *)l_spc;
  lex_case[cr] = (struct lex_ele *)l_cr;
  lex_case[lf] = (struct lex_ele *)l_cr;
  lex_case[0] = (struct lex_ele *)l_eof;

  /* Buffer para el bytecode */
  imem_max = default_buffer;
  imem = 0;
  mem_ory = mem = e_malloc(imem_max * sizeof(int));
  memset(mem, 0, imem_max * sizeof(int));

  /* Buffer para variables locales y privadas */
  iloc_max = default_buffer / 2;
  iloc = 0;
  iloc_len = 0;
  loc = e_malloc(iloc_max * sizeof(int));
  memset(loc, 0, iloc_max * sizeof(int));

  /* ¿Que es esto? */
  ifrm_max = default_buffer / 2;
  frm = e_malloc(ifrm_max * sizeof(int));
  memset(frm, 0, ifrm_max * sizeof(int));

  imem = long_header;

  *(prog + progsize) = 0;

  _source = NULL;

  linsize = 0;
}
示例#5
0
文件: ext_function.c 项目: xth1/lwr
void do_fn_external(int num_genes, int num_chromosomes, int num_genomes,
                    int circular,
                    int unsigned_dist,
                    int verbose,
                    struct genome_struct *genome_list,
                    char *ext_args)
{
    int i, j;
    int **distmatrix;
    /*  int dist_error; */
    distmem_t *distmem;

    int aborted=0;

    //fprintf(outfile,"\nExternal function %s\n", ext_args);

    if (verbose && !unsigned_dist) {
        /*
            do_fn_distmatrix_v(num_genes, num_chromosomes, num_genomes,
        		       circular, unsigned_dist,
        		       genome_list);
        */
        //fprintf(outfile,"verbose, signed distance.  Code deleted.\n");
        return;
    }


    //fprintf(outfile,"\nDistance Matrix:\n");

    /* allocate space for matrix */
    distmatrix = (int **) e_malloc((num_genomes)*sizeof(int *), "distmatrix");

    for (i=0; i < num_genomes; i++) {
        distmatrix[i] = (int *) e_malloc((num_genomes)*sizeof(int), "distmatrix");
    }

    /* allocate memory for distance computations */
    distmem = (distmem_t *) e_malloc(sizeof(distmem_t), "distmem");
    mcdist_allocmem(num_genes,num_chromosomes,distmem);


    /* compute the appropriate matrix */
    if (unsigned_dist) {
        //fprintf(outfile,"Unsigned distance.  Code deleted.\n");
        aborted++;

#if 0
        set_unsigneddist_matrix(distmatrix, genome_list,
                                num_genes, num_chromosomes, num_genomes,
                                circular,
                                distmem,
                                &dist_error);
        if (dist_error) {
            //fprintf(outfile,"WARNING: These unsigned permutations are too complex;\n");
            //fprintf(outfile,"some answers are approximated.\n");
        }
#endif /* 0 */

    } else if (num_chromosomes == 0) {
        /* unichromosomal data */
#if 0
        setinvmatrix(distmatrix,genome_list,
                     num_genes,num_genomes,
                     distmem,

                     /* circular already converted to equiv linear problem */
                     FALSE);
#endif /* 0 */
        /* inline the code to demonstrate explicitly how distmem is used */

        for (i=0 ; i<num_genomes ; i++) {
            distmatrix[i][i] = 0;
            for (j=i+1 ; j<num_genomes ; j++) {
                if (circular)
                    distmatrix[i][j] = distmatrix[j][i] =
                                           invdist_circular(genome_list+i,genome_list+j,
                                                            num_genes,distmem);
                else
                    distmatrix[i][j] = distmatrix[j][i] =
                                           invdist_noncircular(genome_list+i,genome_list+j,
                                                   0,num_genes,distmem);
            }
        }



    } else {
#if 0
        /* multichromosomal data */
        setmcdistmatrix(distmatrix,genome_list,
                        num_genes,num_chromosomes,num_genomes,
                        distmem);
#endif /* 0 */

        /* inline the code to demonstrate explicitly how distmem is used */

        for (i=0 ; i<num_genomes ; i++) {
            distmatrix[i][i] = 0;
            for (j=i+1 ; j<num_genomes ; j++) {
                distmatrix[i][j] = distmatrix[j][i] =
                                       mcdist_noncircular(genome_list+i,genome_list+j,
                                               num_genes,num_chromosomes,distmem,
                                               (graphstats_t *)NULL); /* not verbose */
            }
        }

    }

    if (!aborted) {
        /* display the matrix */
        print_full_distmatrix(distmatrix,num_genomes,genome_list);
    }

    /* free the memory for distance computations */
    mcdist_freemem(distmem);   /* frees memory pointed to by fields of distmem */
    free(distmem);             /* frees the distmem structure itself */

    /* free memory for the distance matrix */
    for (i=num_genomes-1; i >=0 ; i--)
        free(distmatrix[i]);
    free(distmatrix);
}
示例#6
0
title_set_info_t *
DVDGetFileSet(char *dvd)
{
	/*
	 * TODO  Fix close of files if
	 *	we error out
	 *	We also assume that all
	 *	DVD files are of valid
	 *	size i.e. file%2048 == 0
	 */

	/* title interation */
	int		title_sets;
	int		titles;
	int		counter;
	int		i;

	/* DVD file structures */
	dvd_reader_t *	_dvd = NULL;

	ifo_handle_t *	vmg_ifo = NULL;
	ifo_handle_t *	vts_ifo = NULL;

	dvd_file_t   *	vmg_vob_file = NULL;
	dvd_file_t   *	vmg_ifo_file = NULL;

	dvd_file_t   *	vts_ifo_file = NULL;
	dvd_file_t   *	vts_menu_file = NULL;
	dvd_file_t   * 	vts_title_file = NULL;

	/* The sizes it self of each file */
	int		ifo;
	int		bup;
	int		menu_vob;
	int		title_vob;

	/* Arrays keeping the title - filset relationship */
	int		* sector;
	int		* title;
	int		* title_sets_array;
	int		* sector_sets_array;

	/* DVD Video files */
	struct stat	fileinfo;
	char		temppoint[PATH_MAX + 1];

	/* The Title Set Info struct*/
	title_set_info_t * title_set_info;

	/* Temporary mount point - to be used later */
	char		mountpoint[PATH_MAX + 1];

	strncpy(mountpoint, dvd, sizeof (mountpoint));
	mountpoint[sizeof (mountpoint)-1] = '\0';


	_dvd = DVDOpen(dvd);
	if (!_dvd) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "Can't open device '%s'\n", dvd);
#else
		fprintf(stderr, "Can't open device\n");
#endif
		return (0);
	}
	vmg_ifo = ifoOpen(_dvd, 0);
	if (!vmg_ifo) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "Can't open VMG info for '%s'.\n", dvd);
#else
		fprintf(stderr, "Can't open VMG info.\n");
#endif
		return (0);
	}

	/* Check mount point */

	snprintf(temppoint, sizeof (temppoint),
				"%s/VIDEO_TS/VIDEO_TS.IFO", mountpoint);


	if (stat(temppoint, &fileinfo) < 0) {
		/* If we can't stat the file, give up */
#ifdef	USE_LIBSCHILY
		errmsg("Can't stat %s\n", temppoint);
#else
		fprintf(stderr, "Can't stat %s\n", temppoint);
		perror("");
#endif
		return (0);
	}



	title_sets = vmg_ifo->vmgi_mat->vmg_nr_of_title_sets;
	titles = vmg_ifo->tt_srpt->nr_of_srpts;

	sector = e_malloc(titles * sizeof (int));
	memset(sector, 0, titles * sizeof (int));
	title = e_malloc(titles * sizeof (int));
	title_sets_array = e_malloc(title_sets * sizeof (int));
	sector_sets_array = e_malloc(title_sets * sizeof (int));
	title_set_info = (title_set_info_t *)e_malloc(sizeof (title_set_info_t));
	title_set_info->title_set = (title_set_t *)e_malloc((title_sets + 1) *
							sizeof (title_set_t));

	title_set_info->num_titles = title_sets;


	/* Fill and sort the arrays for titles*/

	if (titles >= 1) {
		for (counter = 0; counter < titles; counter++) {
			sector[counter] = vmg_ifo->tt_srpt->title[counter].title_set_sector;
			title[counter]  = counter + 1;
		}
	}

	/* Yes, we should probably do a better sort than B - but what the heck*/
	bsort(sector, title, titles);


	/*
	 * Since title sets and titles are not the same we will need to sort
	 * out "bogus" titles
	 */

	uniq(sector, title, title_sets_array, sector_sets_array, titles);


	/* Open VIDEO_TS.VOB is present */

	vmg_vob_file = DVDOpenFile(_dvd, 0, DVD_READ_MENU_VOBS);

	/* Check VIDEO_TS title set */

	vmg_ifo_file = DVDOpenFile(_dvd, 0, DVD_READ_INFO_FILE);

	if ((vmg_vob_file == 0) && vmg_ifo->vmgi_mat->vmg_last_sector + 1
			< 2 * DVDFileSize(vmg_ifo_file)) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "IFO is not of correct size aborting\n");
#else
		fprintf(stderr, "IFO is not of correct size aborting\n");
#endif
		DVDFreeFileSetArrays(sector, title, title_sets_array,
					sector_sets_array);
		DVDFreeFileSet(title_set_info);
		return (0);
	} else if ((vmg_vob_file != 0) && (vmg_ifo->vmgi_mat->vmg_last_sector
		    + 1  < 2 * DVDFileSize(vmg_ifo_file) +
		    DVDFileSize(vmg_vob_file))) {
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size");
#else
		fprintf(stderr, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size");
#endif
		DVDFreeFileSetArrays(sector, title, title_sets_array,
					sector_sets_array);
		DVDFreeFileSet(title_set_info);
		return (0);
	}

	/* Find the actuall right size of VIDEO_TS.IFO */
	if (vmg_vob_file == 0) {
		if (vmg_ifo->vmgi_mat->vmg_last_sector + 1 > 2
				*  DVDFileSize(vmg_ifo_file)) {
			ifo = vmg_ifo->vmgi_mat->vmg_last_sector
				- DVDFileSize(vmg_ifo_file) + 1;
		} else {
			ifo = vmg_ifo->vmgi_mat->vmgi_last_sector + 1;
		}
	} else {
		if (vmg_ifo->vmgi_mat->vmgi_last_sector + 1
				< vmg_ifo->vmgi_mat->vmgm_vobs) {
			ifo = vmg_ifo->vmgi_mat->vmgm_vobs;
		} else {
			ifo = vmg_ifo->vmgi_mat->vmgi_last_sector + 1;
		}
	}

	title_set_info->title_set[0].size_ifo = ifo * 2048;
	title_set_info->title_set[0].realsize_ifo = fileinfo.st_size;
	title_set_info->title_set[0].pad_ifo = ifo - DVDFileSize(vmg_ifo_file);

	/* Find the actuall right size of VIDEO_TS.VOB */
	if (vmg_vob_file != 0) {
		if (ifo + DVDFileSize(vmg_ifo_file) +
		    DVDFileSize(vmg_vob_file) - 1 <
		    vmg_ifo->vmgi_mat->vmg_last_sector) {
				menu_vob = vmg_ifo->vmgi_mat->vmg_last_sector -
						ifo - DVDFileSize(vmg_ifo_file) + 1;
		} else {
			menu_vob = vmg_ifo->vmgi_mat->vmg_last_sector
			- ifo - DVDFileSize(vmg_ifo_file) + 1;
		}

		snprintf(temppoint, sizeof (temppoint),
				"%s/VIDEO_TS/VIDEO_TS.VOB", mountpoint);
		if (stat(temppoint, &fileinfo) < 0) {
#ifdef	USE_LIBSCHILY
			errmsg("calc: Can't stat %s\n", temppoint);
#else
			fprintf(stderr, "calc: Can't stat %s\n", temppoint);
			perror("");
#endif
			DVDFreeFileSetArrays(sector, title, title_sets_array,
						sector_sets_array);
			DVDFreeFileSet(title_set_info);
			return (0);
		}

		title_set_info->title_set[0].realsize_menu = fileinfo.st_size;
		title_set_info->title_set[0].pad_menu = menu_vob -
						DVDFileSize(vmg_vob_file);
		title_set_info->title_set[0].size_menu = menu_vob * 2048;
		DVDCloseFile(vmg_vob_file);
	} else {
		title_set_info->title_set[0].size_menu = 0;
		title_set_info->title_set[0].realsize_menu = 0;
		title_set_info->title_set[0].pad_menu = 0;
		menu_vob = 0;
	}


	/* Finding the actuall right size of VIDEO_TS.BUP */
	if (title_sets >= 1) {
		bup = sector_sets_array[0] - menu_vob - ifo;
	} else {
		/* Just in case we burn a DVD-Video without any title_sets */
		bup = vmg_ifo->vmgi_mat->vmg_last_sector + 1 - menu_vob - ifo;
	}

	/* Never trust the BUP file - use a copy of the IFO */
	snprintf(temppoint, sizeof (temppoint),
				"%s/VIDEO_TS/VIDEO_TS.IFO", mountpoint);

	if (stat(temppoint, &fileinfo) < 0) {
#ifdef	USE_LIBSCHILY
		errmsg("calc: Can't stat %s\n", temppoint);
#else
		fprintf(stderr, "calc: Can't stat %s\n", temppoint);
		perror("");
#endif
		DVDFreeFileSetArrays(sector, title, title_sets_array,
					sector_sets_array);
		DVDFreeFileSet(title_set_info);
		return (0);
	}

	title_set_info->title_set[0].realsize_bup = fileinfo.st_size;
	title_set_info->title_set[0].size_bup = bup * 2048;
	title_set_info->title_set[0].pad_bup = bup - DVDFileSize(vmg_ifo_file);

	/* Take care of the titles which we don't have in VMG */

	title_set_info->title_set[0].number_of_vob_files = 0;
	title_set_info->title_set[0].realsize_vob[0] = 0;
	title_set_info->title_set[0].pad_title = 0;

	DVDCloseFile(vmg_ifo_file);

	if (title_sets >= 1) {
		for (counter = 0; counter < title_sets; counter++) {

			vts_ifo = ifoOpen(_dvd, counter + 1);

			if (!vts_ifo) {
#ifdef	USE_LIBSCHILY
				errmsgno(EX_BAD, "Can't open VTS info.\n");
#else
				fprintf(stderr, "Can't open VTS info.\n");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			}

			snprintf(temppoint, sizeof (temppoint),
				"%s/VIDEO_TS/VTS_%02i_0.IFO",
				mountpoint, counter + 1);

			if (stat(temppoint, &fileinfo) < 0) {
#ifdef	USE_LIBSCHILY
				errmsg("calc: Can't stat %s\n", temppoint);
#else
				fprintf(stderr, "calc: Can't stat %s\n",
					temppoint);
				perror("");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			}


			/* Test if VTS_XX_0.VOB is present */

			vts_menu_file = DVDOpenFile(_dvd, counter + 1,
					DVD_READ_MENU_VOBS);

			/* Test if VTS_XX_X.VOB are present */

			vts_title_file = DVDOpenFile(_dvd, counter + 1,
						DVD_READ_TITLE_VOBS);

			/* Check VIDEO_TS.IFO */

			vts_ifo_file = DVDOpenFile(_dvd, counter + 1,
							DVD_READ_INFO_FILE);

			/*
			 * Checking that title will fit in the
			 * space given by the ifo file
			 */


			if (vts_ifo->vtsi_mat->vts_last_sector + 1
				< 2 * DVDFileSize(vts_ifo_file)) {
#ifdef	USE_LIBSCHILY
				errmsgno(EX_BAD, "IFO is not of correct size aborting.\n");
#else
				fprintf(stderr, "IFO is not of correct size aborting\n");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			} else if ((vts_title_file != 0) &&
				    (vts_menu_file != 0) &&
				    (vts_ifo->vtsi_mat->vts_last_sector + 1
				    < 2 * DVDFileSize(vts_ifo_file) +
				    DVDFileSize(vts_title_file) +
				    DVDFileSize(vts_menu_file))) {
#ifdef	USE_LIBSCHILY
				errmsgno(EX_BAD, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size.\n");
#else
				fprintf(stderr, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			} else if ((vts_title_file != 0) &&
				    (vts_menu_file == 0) &&
				    (vts_ifo->vtsi_mat->vts_last_sector + 1
				    < 2 * DVDFileSize(vts_ifo_file) +
				    DVDFileSize(vts_title_file))) {
#ifdef	USE_LIBSCHILY
				errmsgno(EX_BAD, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size.\n");
#else
				fprintf(stderr, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			} else if ((vts_menu_file != 0) &&
				    (vts_title_file == 0) &&
				    (vts_ifo->vtsi_mat->vts_last_sector + 1
				    < 2 * DVDFileSize(vts_ifo_file) +
				    DVDFileSize(vts_menu_file))) {
#ifdef	USE_LIBSCHILY
				errmsgno(EX_BAD, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size.\n");
#else
				fprintf(stderr, "Either VIDEO_TS.IFO or VIDEO_TS.VOB is not of correct size");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			}


			/* Find the actuall right size of VTS_XX_0.IFO */
			if ((vts_title_file == 0) && (vts_menu_file == 0)) {
				if (vts_ifo->vtsi_mat->vts_last_sector + 1 >
				    2 * DVDFileSize(vts_ifo_file)) {
					ifo = vts_ifo->vtsi_mat->vts_last_sector
						- DVDFileSize(vts_ifo_file) + 1;
				} else {
					ifo = vts_ifo->vtsi_mat->vts_last_sector
						- DVDFileSize(vts_ifo_file) + 1;
				}
			} else if (vts_title_file == 0) {
				if (vts_ifo->vtsi_mat->vtsi_last_sector + 1 <
				    vts_ifo->vtsi_mat->vtstt_vobs) {
					ifo = vmg_ifo->vtsi_mat->vtstt_vobs;
				} else {
					ifo = vmg_ifo->vtsi_mat->vtstt_vobs;
				}
			} else {
				if (vts_ifo->vtsi_mat->vtsi_last_sector + 1 <
				    vts_ifo->vtsi_mat->vtsm_vobs) {
					ifo = vts_ifo->vtsi_mat->vtsm_vobs;
				} else {
					ifo = vts_ifo->vtsi_mat->vtsi_last_sector + 1;
				}
			}
			title_set_info->title_set[counter + 1].size_ifo =
						ifo * 2048;
			title_set_info->title_set[counter + 1].realsize_ifo =
						fileinfo.st_size;
			title_set_info->title_set[counter + 1].pad_ifo =
						ifo - DVDFileSize(vts_ifo_file);


			/* Find the actuall right size of VTS_XX_0.VOB */
			if (vts_menu_file != 0) {
				if (vts_ifo->vtsi_mat->vtsm_vobs == 0)  {
					/*
					 * Apparently start sector 0 means that
					 * VTS_XX_0.VOB is empty after all...
					 */
					menu_vob = 0;
					if (DVDFileSize(vts_menu_file) != 0) {
						/*
						 * Paranoia: we most likely never
						 * come here...
						 */
#ifdef	USE_LIBSCHILY
						errmsgno(EX_BAD,
							"%s/VIDEO_TS/VTS_%02i_0.IFO appears to be corrupted.\n",
							mountpoint, counter+1);
#else
						fprintf(stderr,
							"%s/VIDEO_TS/VTS_%02i_0.IFO appears to be corrupted.\n",
							mountpoint, counter+1);
#endif
						return (0);
					}
				} else if ((vts_title_file != 0) &&
					(vts_ifo->vtsi_mat->vtstt_vobs -
					vts_ifo->vtsi_mat->vtsm_vobs >
						DVDFileSize(vts_menu_file))) {
					menu_vob = vts_ifo->vtsi_mat->vtstt_vobs -
							vts_ifo->vtsi_mat->vtsm_vobs;
				} else if ((vts_title_file == 0) &&
					    (vts_ifo->vtsi_mat->vtsm_vobs +
					    DVDFileSize(vts_menu_file) +
					    DVDFileSize(vts_ifo_file) - 1 <
					    vts_ifo->vtsi_mat->vts_last_sector)) {
					menu_vob = vts_ifo->vtsi_mat->vts_last_sector
						- DVDFileSize(vts_ifo_file)
						- vts_ifo->vtsi_mat->vtsm_vobs + 1;
				} else {
					menu_vob = vts_ifo->vtsi_mat->vtstt_vobs -
							vts_ifo->vtsi_mat->vtsm_vobs;
				}

				snprintf(temppoint, sizeof (temppoint),
					"%s/VIDEO_TS/VTS_%02i_0.VOB", mountpoint, counter + 1);

				if (stat(temppoint, &fileinfo)  < 0) {
#ifdef	USE_LIBSCHILY
					errmsg("calc: Can't stat %s\n", temppoint);
#else
					fprintf(stderr, "calc: Can't stat %s\n",
						temppoint);
					perror("");
#endif
					DVDFreeFileSetArrays(sector, title,
						title_sets_array, sector_sets_array);
					DVDFreeFileSet(title_set_info);
					return (0);
				}

				title_set_info->title_set[counter + 1].realsize_menu = fileinfo.st_size;
				title_set_info->title_set[counter + 1].size_menu = menu_vob * 2048;
				title_set_info->title_set[counter + 1].pad_menu = menu_vob - DVDFileSize(vts_menu_file);

			} else {
				title_set_info->title_set[counter + 1].size_menu = 0;
				title_set_info->title_set[counter + 1].realsize_menu = 0;
				title_set_info->title_set[counter + 1].pad_menu = 0;
				menu_vob = 0;
			}


			/* Find the actuall total size of VTS_XX_[1 to 9].VOB */

			if (vts_title_file != 0) {
				if (ifo + menu_vob + DVDFileSize(vts_ifo_file) -
				    1 < vts_ifo->vtsi_mat->vts_last_sector) {
				    title_vob = vts_ifo->vtsi_mat->vts_last_sector
						+ 1 - ifo - menu_vob -
						DVDFileSize(vts_ifo_file);
				} else {
					title_vob = vts_ifo->vtsi_mat->vts_last_sector +
						1 - ifo - menu_vob -
						DVDFileSize(vts_ifo_file);
				}
				/*
				 * Find out how many vob files
				 * and the size of them
				 */
				for (i = 0; i < 9; ++i) {
					snprintf(temppoint, sizeof (temppoint),
						"%s/VIDEO_TS/VTS_%02i_%i.VOB",
						mountpoint, counter + 1, i + 1);
					if (stat(temppoint, &fileinfo) < 0) {
						break;
					}
					title_set_info->title_set[counter + 1].realsize_vob[i] = fileinfo.st_size;
				}
				title_set_info->title_set[counter + 1].number_of_vob_files = i;
				title_set_info->title_set[counter + 1].size_title = title_vob * 2048;
				title_set_info->title_set[counter + 1].pad_title = title_vob - DVDFileSize(vts_title_file);
			} else {
				title_set_info->title_set[counter + 1].number_of_vob_files = 0;
				title_set_info->title_set[counter + 1].realsize_vob[0] = 0;
				title_set_info->title_set[counter + 1].size_title = 0;
				title_set_info->title_set[counter + 1].pad_title = 0;
				title_vob = 0;

			}


			/* Find the actuall total size of VTS_XX_0.BUP */
			if (title_sets - 1 > counter) {
				bup = sector_sets_array[counter+1]
					- sector_sets_array[counter]
					- title_vob - menu_vob - ifo;
			} else {
				bup = vts_ifo->vtsi_mat->vts_last_sector + 1
					- title_vob - menu_vob - ifo;
			}

			/* Never trust the BUP use a copy of the IFO */
			snprintf(temppoint, sizeof (temppoint),
				"%s/VIDEO_TS/VTS_%02i_0.IFO",
				mountpoint, counter + 1);

			if (stat(temppoint, &fileinfo) < 0) {
#ifdef	USE_LIBSCHILY
				errmsg("calc: Can't stat %s\n", temppoint);
#else
				fprintf(stderr, "calc: Can't stat %s\n",
					temppoint);
				perror("");
#endif
				DVDFreeFileSetArrays(sector, title,
					title_sets_array, sector_sets_array);
				DVDFreeFileSet(title_set_info);
				return (0);
			}

			title_set_info->title_set[counter + 1].size_bup =
						bup * 2048;
			title_set_info->title_set[counter + 1].realsize_bup =
						fileinfo.st_size;
			title_set_info->title_set[counter + 1].pad_bup =
						bup - DVDFileSize(vts_ifo_file);


			/* Closing files */

			if (vts_menu_file != 0) {
				DVDCloseFile(vts_menu_file);
			}

			if (vts_title_file != 0) {
				DVDCloseFile(vts_title_file);
			}


			if (vts_ifo_file != 0) {
				DVDCloseFile(vts_ifo_file);
			}

			ifoClose(vts_ifo);

		}

	}

	DVDFreeFileSetArrays(sector, title, title_sets_array, sector_sets_array);

	/* Close the VMG ifo file we got all the info we need */
	ifoClose(vmg_ifo);


	/* Close the DVD */
	DVDClose(_dvd);

	/* Return the actuall info*/
	return (title_set_info);


}
示例#7
0
文件: unsignedhc.c 项目: xth1/lwr
void uhc_summarize_results(
    int num_genes,
    int num_chromosomes,
    int num_genomes,
    int circular,
    struct genome_struct *genome_list_in,
    int nsegs,
    int *seg_list,

    /* output */
    struct uhc_mem *uhcmem,
    struct genome_struct *out_genome
)
{
    int d;

    int count;
    int s1, s2;
    int bestd, num_best;
    int k;

    int *curseg;
    int cur_len;
    int cur_genome_num;
    struct genome_struct *cur_genome = (struct genome_struct *) 0;

    int *sign_groups;

    int i1,i2,max_entry;
    int *dmat = uhcmem->dmat;

    /**********************************************************************
     * Calc sign groups
     **********************************************************************/
    //printf("uhc_summarize_results\n");
    sign_groups = (int *) e_malloc(nsegs * sizeof(int),
                                   "uhc_summarize_results: sign_groups");
    calc_sign_groups(sign_groups,
                     uhcmem);



    //printf("STOP_1\n");
    /**********************************************************************
     * # runs at each distance
     **********************************************************************/

    //f//printf(outfile, "\n# trials giving each score:\n");
    /*f//printf(outfile, "%*s  # times\n",
    uhcmem->width_score2, "score");*/
    for (d=0; d < uhcmem->max_dist_possible; d++) {
    if (uhcmem->dist_counts[d] > 0) {
            /*f//printf(outfile, "%*d  %d\n",
            uhcmem->width_score2, d,
                   uhcmem->dist_counts[d]);*/
        }
    }
    if (uhcmem->dist_counts[uhcmem->max_dist_possible] != 0) {
        /* TODO: improve handling of this for arbitrary weight matrices */
        /*f//printf(outfile, "%*s  %d\n",
        uhcmem->width_score2, "other",
        uhcmem->dist_counts[uhcmem->max_dist_possible]);*/
    }

    //printf("STOP_2\n");
    /**********************************************************************
     * at best dist: sign pattern
     **********************************************************************/

    bestd = uhcmem->best_dist;
    num_best = uhcmem->dist_counts[bestd];

    //f//printf(outfile, "\n");
    //printf("STOP_3\n");
    /**********************************************************************
     * at best dist: perfect correlation groups
     **********************************************************************/

    //f//printf(outfile, "\n");
    //printf("STOP_4\n");
    /**********************************************************************
     * at best dist: sign counts
     **********************************************************************/

    //f//printf(outfile,"\nSign counts and perfect correlations:\n");
    cur_genome_num = -1;
    for (s1 = 0; s1 < nsegs; s1++) {
    curseg = USEG(seg_list, s1);

        /* print genome name if switched genomes */
        if (curseg[0] != cur_genome_num) {
            cur_genome_num = curseg[0];
            cur_genome = &genome_list_in[cur_genome_num];
            if (cur_genome->gnamePtr != (char *) 0) {
                //f//printf(outfile, ">%s\n", cur_genome->gnamePtr);
            } else {
                //f//printf(outfile, ">genome%d\n", cur_genome_num + 1);
            }
        }

        cur_len = curseg[2];

        //f//printf(outfile, "S%-*d  [", uhcmem->width_segnum, s1);
        for (k=0; k<cur_len; k++) {
            //f//printf(outfile, " %d", cur_genome->genes[cur_start+k]);
        }
        //f//printf(outfile," ]   ");

        count = uhcmem->dist_corr[s1*nsegs+s1];
        /*f//printf(outfile,
        "%d+  %d-  "
        ,
        count, num_best-count);*/

        count = sign_groups[s1];
        if (count == -1-nsegs) {
        //f//printf(outfile, "sign -");
    } else if (count == nsegs) {
        //f//printf(outfile, "sign +");
    } else if (count == s1) {
    } else if (count >= 0) {
        //f//printf(outfile, "sign same as S%d", count);
    } else if (count < 0) {
        //f//printf(outfile, "sign same as -S%d", -1-count);
    }

    //f//printf(outfile, "\n");
}
//printf("STOP_5\n");
/**********************************************************************
 * at best dist: detailed correlations
 **********************************************************************/

//f//printf(outfile,"\nCorrelations: Number of times segments have same sign:\n");
for (s1=0; s1<nsegs; s1++) {
    //f//printf(outfile, "S%-*d  ", uhcmem->width_segnum, s1);
    for (s2=0; s2<nsegs; s2++) {
            if (s1 == s2) {
                count = num_best;
            } else if (s2<s1) {
                count = uhcmem->dist_corr[s2*nsegs+s1];
            } else {
                count = uhcmem->dist_corr[s1*nsegs+s2];
            }
            //f//printf(outfile, " %*d", uhcmem->width_run, count);
        }
        //f//printf(outfile, "\n");
    }
//printf("STOP_6\n");
    /**********************************************************************
     * at best dist: one particular signage
     **********************************************************************/

    //f//printf(outfile,"\nA best scoring solution:\n");

    uhc_setsigns(num_genes, num_chromosomes, num_genomes,
                 genome_list_in,
                 nsegs, seg_list, uhcmem->bestsigns,
                 uhcmem);

    /* Set the output genome */
    ////printf ("Genome list %u ", uhcmem);
    ////printf ("Genome list %d ", uhcmem->genome_list + 1);
    memcpy(out_genome, uhcmem->genome_list + 1, sizeof(struct genome_struct));
    out_genome->genes = malloc(sizeof(int) * num_genes);
    copy_genes((uhcmem->genome_list + 1)->genes, out_genome->genes, num_genes);

//printf("STOP_7\n");
    /**********************************************************************
     * and the pairwise matrix for that signage
     **********************************************************************/

    max_entry = 0;
    for (i1=0; i1<num_genomes; i1++) {
    dmat[num_genomes*i1 + i1] = 0;
        for (i2=0; i2<i1; i2++) {
            d =
                dmat[num_genomes*i1 + i2] =
                    dmat[num_genomes*i2 + i1] =
                        uhc_dist(uhcmem, i1, i2,
                                 num_genes, num_chromosomes, circular);
            if (d > max_entry) {
                max_entry = d;
            }
        }
    }
//printf("STOP_8\n");
    /* TODO: should integrate this with the other matrix printing routines */

    //DEBUG: deleted code here
}
示例#8
0
文件: unsignedhc.c 项目: xth1/lwr
/* Do the main algorithm */
int unsignedhc_mcdist_mat2(
    int num_genes,
    int num_chromosomes,
    int num_genomes,
    int circular,
    int verbose,
    struct genome_struct *genome_list_in,
    int nsegs,
    int *seg_list,
    int **weight_matrix,
    int num_iterations,
    struct genome_struct *out_genome
)
{

    distmem_t distmem_mem;
    struct uhc_mem uhcmem_mem, *uhcmem=&uhcmem_mem;
    struct genome_struct uhcmem_g1, uhcmem_g2;

    /* extreme upper bound on max distance possible */
    /* TODO: improve for use with arbitrary weight matrices */
    int max_dist_possible = num_genomes*(num_genomes-1)/2 * (num_genes+1);

    int i,k,s1;
    int cur_genome_num;
    struct genome_struct *cur_genome = (struct genome_struct *) 0;
    int *curseg;
    int cur_len;
    int num_options;

    /**********************************************************************
     * allocate memory
     **********************************************************************/

    uhcmem->max_dist_possible = max_dist_possible;
    uhcmem->best_dist = max_dist_possible;
    uhcmem->distmem = &distmem_mem;
    uhcmem->run_no = 0;

    uhcmem->genome_list =
        (struct genome_struct *) e_malloc(num_genomes*sizeof(struct genome_struct),

                                          "unsignedhc_mcdist_mat2: genome_list");

    uhcmem->g1 = &uhcmem_g1;
    alloc_simple_genome(num_genes, &uhcmem_g1);
    uhcmem->g2 = &uhcmem_g2;
    alloc_simple_genome(num_genes, &uhcmem_g2);

    /* allocate space to work on a copy of all the genomes;
     * keep the originals intact
     */
    for (i=0; i<num_genomes; i++) {
        uhcmem->genome_list[i].gnamePtr = genome_list_in[i].gnamePtr;
        uhcmem->genome_list[i].genome_num = genome_list_in[i].genome_num;
        uhcmem->genome_list[i].encoding = genome_list_in[i].encoding;
        uhcmem->genome_list[i].genes =
            (int *) e_malloc(num_genes * sizeof(int), "unsignedhc_mcdist_mat2: genes");
    }


    /* allocate memory for distance computations */
    mcdist_allocmem(num_genes, num_chromosomes, uhcmem->distmem);


    /* allocate memory for distance matrix */
    uhcmem->dmat = (int *) e_malloc(num_genomes*num_genomes*sizeof(int),
                                    "unsignedhc_mcdist_mat2: dmat");
    /* allocate memory for modified row of distance matrix */
    uhcmem->dmat_r = (int *) e_malloc(num_genomes*sizeof(int),
                                      "unsignedhc_mcdist_mat2: dmat_r");

    /* keep track of which flips have been tested and which have not */
    uhcmem->options = (int *) e_malloc(2 * nsegs * sizeof(int),
                                       "unsignedhc_mcdist_mat2: options");

    /* allocate memory for statistical summary of results */

    /* # runs with each score */
    uhcmem->dist_counts = (int *) e_calloc(max_dist_possible+1, sizeof(int),
                                           "unsignedhc_mcdist_mat2: dist_counts");

    /* correlations */
    uhcmem->dist_corr = (int *) e_calloc(nsegs * nsegs, sizeof(int),
                                         "unsignedhc_mcdist_mat2: dist_corr");
    uhcmem->nsegs = nsegs;

    //printf("nsegs at alloc: %d\n", nsegs);

    uhcmem->signs = (int *) e_malloc(nsegs * sizeof(int),
                                     "unsignedhc_mcdist_mat2: signs");
    uhcmem->bestsigns = (int *) e_malloc(nsegs * sizeof(int),
                                         "unsignedhc_mcdist_mat2: bestsigns");


    uhcmem->width_run = num_digits(num_iterations);
    uhcmem->width_run2 = max2(uhcmem->width_run, 5);
    uhcmem->width_score = num_digits(max_dist_possible);
    uhcmem->width_score2 = max2(uhcmem->width_score, 5);
    uhcmem->width_mat_entry = num_digits(num_genes+1);
    uhcmem->width_segnum = num_digits(nsegs);


    uhc_init_random_seed();

    /**********************************************************************
     * initialize list of allowed options
     * 1. all segments individually
     * 2. "antistrips" (just to increase the probability of finding them)
     **********************************************************************/

    /* individual segments */
    num_options = 0;
    for (s1=0; s1<nsegs; s1++) {
        uhcmem->options[num_options++] = s1;
    }

    /* antistrips */
    for (s1=0; s1<nsegs-1; s1++) {
        curseg = USEG(seg_list,s1);
        /* are segs s1 & s1+1 adjacent? skip if not. */
        if (curseg[0] != curseg[3]                /* genome numbers */
                || curseg[1]+curseg[2] != curseg[4]   /* adjacent positions */
           ) {
            /* not adjacent */
            continue;
        }
        /* are s1 & s1-1 adjacent? skip if so (bigger than 2-strip) */
        if (s1>0
                && curseg[0]==curseg[-3]
                && curseg[1]==curseg[-2]+curseg[-1]) {
            /* adjacent, 3+ strip, skip */
            continue;
        }
        uhcmem->options[num_options++] = s1 + nsegs;
    }
    uhcmem->num_options = num_options;

    /**********************************************************************
     * print list of segments
     **********************************************************************/

    if (verbose) {
        //f//printf(outfile,"\nSegments:\n");
        cur_genome_num = -1;
        for (s1 = 0; s1 < nsegs; s1++) {
            curseg = USEG(seg_list, s1);

            /* print genome name if switched genomes */
            if (curseg[0] != cur_genome_num) {
                cur_genome_num = curseg[0];
                cur_genome = &genome_list_in[cur_genome_num];
                if (cur_genome->gnamePtr != (char *) 0) {
                    //f//printf(outfile, ">%s\n", cur_genome->gnamePtr);
                } else {
                    //f//printf(outfile, ">genome%d\n", cur_genome_num + 1);
                }
            }


            cur_len = curseg[2];

            //f//printf(outfile, "S%-*d  [", uhcmem->width_segnum, s1);
            for (k=0; k<cur_len; k++) {
                //f//printf(outfile, " %d", cur_genome->genes[cur_start+k]);
            }
            //f//printf(outfile," ]\n");
        }
    }

    /**********************************************************************
     * do lots of iterations
     **********************************************************************/

   /* if (verbose) {
        //f//printf(outfile, "\nTrials:\n");
        //f//printf(outfile, "%-*s %-*s  matrix %*s signs\n",
        uhcmem->width_run2, "run",
        uhcmem->width_score2, "score",
        ((uhcmem->width_mat_entry+1)*num_genomes + 2)*num_genomes - 5, "");
    }
*/
    for (i=0; i<num_iterations; i++) {
        uhc_run(num_genes,num_chromosomes,num_genomes,
                circular,verbose,
                genome_list_in,
                nsegs, seg_list,
                weight_matrix,
                uhcmem);
    }

    /**********************************************************************
     * report on results
     **********************************************************************/

    //if (verbose) {
    uhc_summarize_results(num_genes, num_chromosomes, num_genomes,
                          circular,
                          genome_list_in,
                          nsegs, seg_list,
                          uhcmem, out_genome);
    //}

    /**********************************************************************
     * cleanup memory
     **********************************************************************/

    free((void *) uhcmem->bestsigns);
    free((void *) uhcmem->signs);
    free((void *) uhcmem->options);
    free((void *) uhcmem->dmat_r);
    free((void *) uhcmem->dmat);
    free((void *) uhcmem->dist_corr);
    free((void *) uhcmem->dist_counts);

    /* free memory for distance computations */
    mcdist_freemem(uhcmem->distmem);

    free_simple_genome((void *) uhcmem->g2);
    free_simple_genome((void *) uhcmem->g1);

    /* free memory for copy of genomes */
    for (i=0; i<num_genomes; i++) {
        free((void *) uhcmem->genome_list[i].genes);
    }
    free((void *) uhcmem->genome_list);

    return uhcmem->best_dist;
}
示例#9
0
文件: unsignedhc.c 项目: xth1/lwr
int unsignedhc_mcdist(
    int num_genes,
    int num_chromosomes,
    int circular,
    int verbose,
    struct genome_struct *genome_list,
    int gindex1, int gindex2,
    int nsegs,
    int *seg_list,
    int **weight_matrix,
    int num_iterations,
    struct genome_struct *out_genome
)
{
    int d;
    int num_genomes2 = 2;
    struct genome_struct *genome_list2
        = (struct genome_struct *) e_malloc(num_genomes2
                                            *sizeof(struct genome_struct),
                                            "unsignedhc_mcdist: genome_list");
    int **weight_matrix2 = (int **) NULL;

    /* TODO: nsegs is upper bound could compute exactly */
    int *seg_list2;

    int nsegs2 = 0;
    int i;
    int new_num;
    int *curseg, *curseg2;

    //printf("num_genes: %d\n", num_genes);
    //printf("num_chromosomes: %d\n", num_chromosomes);
    //printf("circular: %d\n", circular);
    //printf("verbose: %d\n", verbose);
    //printf("genome_list: %p\n", genome_list);
    //printf("gindex1: %d\n", gindex1);
    //printf("gindex2: %d\n", gindex2);
    //printf("nsegs: %d\n", nsegs);
    //printf("seg_list: %p\n", seg_list);
    //printf("weight_matrix: %p\n", weight_matrix);
    //printf("num_iterations: %d\n", num_iterations);

    /* make subsetted list of segments */
    if (nsegs >= 0) {
        seg_list2 =
            (int *) e_malloc(3 * nsegs * sizeof(int),
                             "unsignedhc_mcdist: seg_list2");
    } else {
        seg_list2 = (int *) 0;
    }


    /* make list of just 2 genomes */
    memcpy(genome_list2, genome_list+gindex1, sizeof(struct genome_struct));
    memcpy(genome_list2+1, genome_list+gindex2, sizeof(struct genome_struct));

    /* copy segments in these genomes */
    if (nsegs < 0) {
        nsegs2 = nsegs;
    } else {
        nsegs2 = 0;
        curseg2 = seg_list2;
        for (i=0; i<nsegs; i++) {
            curseg = USEG(seg_list, i);
            if (curseg[0] == gindex1) {
                new_num = 0;
            } else if (curseg[0] == gindex2) {
                new_num = 1;
            } else {
                continue;
            }

            *curseg2++ = new_num;     /* change genome number */
            *curseg2++ = curseg[1];   /* copy offset */
            *curseg2++ = curseg[2];   /* copy length */
            nsegs2++;
        }
    }


    /* subsetted weight matrix */
    weight_matrix2 = default_weight_mat(2);
    if (weight_matrix != (int **) NULL) {
        weight_matrix2[0][1] = weight_matrix2[1][0] =
                                   weight_matrix[gindex1][gindex2];
    }


    d = unsignedhc_mcdist_mat(num_genes, num_chromosomes, num_genomes2,
                                circular, verbose,
                                genome_list2,
                                nsegs2, seg_list2,
                                weight_matrix2,
                                num_iterations,
                                out_genome);

    destroy_mat_int2d(weight_matrix2,2);
    free((void *) genome_list2);
    free((void *) seg_list2);
    return d;
}