Exemplo n.º 1
0
static int find_word_combinations(const char *word, const char *dict_name, FILE *dict_fp)
{
	wchar_t word_wc[MAX_WORD_SIZE];
	wchar_t str_wc[MAX_WORD_SIZE];

	wchar_t *str_token_wc;
	wchar_t *str_token_wc_state;

	char *str = NULL;
	size_t str_len = 0;

	int translate_count = 0;


	if ( strlen(word) < 1 )
		return 0;

	if ( strncpy_lower_wc(word_wc, word, MAX_WORD_SIZE - 1) == NULL ) {
		fprintf(stderr, "Cannot convert \"%s\" to (wchar_t *): %s\n", word, strerror(errno));
		return -1;
	}

	while ( getline(&str, &str_len, dict_fp) != -1 ) {
		if ( str[0] == '#' || str[0] == '\n' )
			continue;

		if ( strncpy_lower_filter_wc(str_wc, str, MAX_WORD_SIZE - 1) == NULL )
			continue;

		for (str_token_wc = wcstok(str_wc, L" -./\\", &str_token_wc_state); str_token_wc;
			str_token_wc = wcstok(NULL, L" -./\\", &str_token_wc_state)) {
			if ( word_wc[0] != str_token_wc[0] )
				continue;

			if ( !strcmp_full_wc(str_token_wc, word_wc) ) {
				++translate_count;

				//if ( translate_count == 1 )
				//	print_header(dict_name, word_wc);
				print_translate(str, translate_count);
				//print_separator();

				if ( translate_count >= settings.max_translate_count )
					goto external_loop_break_label;

				break;
			}
		}
	}

	external_loop_break_label :

	free(str);

	return translate_count;
}
Exemplo n.º 2
0
static int find_word_unified(const char *word, const regimen_t regimen, const char *dict_name, FILE *dict_fp)
{
	wchar_t word_wc[MAX_WORD_SIZE];
	wchar_t str_wc[MAX_WORD_SIZE];

	char *str = NULL;
	size_t str_len = 0;

	long index_pos = -1;
	int translate_count = 0;

	bool break_end_flag = false;


	if ( strlen(word) < 1 )
		return 0;

	if ( strncpy_lower_wc(word_wc, word, MAX_WORD_SIZE - 1) == NULL ) {
		fprintf(stderr, "Cannot convert \"%s\" to (wchar_t *): %s\n", word, strerror(errno));
		return -1;
	}

	if ( regimen == usually_regimen || regimen == first_concurrence_regimen || list_regimen ) {
		index_pos = linear_index_pos(word_wc[0], dict_fp);
		if ( index_pos == 0 ) {
			return 0;
		}
		else if ( index_pos > 0 ) {
			if ( fseek(dict_fp, index_pos, SEEK_SET) != 0 )
				fprintf(stderr, "Seek fail on index \"%lc %ld\": %s: ignored\n", word_wc[0], index_pos, strerror(errno));
		}
		else {
			rewind(dict_fp);
		}
	}

	while ( getline(&str, &str_len, dict_fp) != -1 ) {
		if ( str[0] == '#' || str[0] == '\n' )
			continue;

		if ( (str_wc[0] = first_lower_wc(str)) == L'\0' )
			continue;

		if ( regimen == usually_regimen || regimen == first_concurrence_regimen || list_regimen ) {
			if ( word_wc[0] != str_wc[0] && break_end_flag )
				break;
			if ( word_wc[0] != str_wc[0] )
				continue;
			else
				break_end_flag = true;
		}

		if ( strncpy_lower_filter_wc(str_wc, str, MAX_WORD_SIZE - 1) == NULL ) {
			break_end_flag = false; // ill_defined_regimen not required this
			continue;
		}

		if ( regimen == usually_regimen ) {
			if ( !strcmp_full_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_translate(str, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
		else if ( regimen == first_concurrence_regimen ) {
			if ( !strcmp_noend_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_translate(str, translate_count);

				break;
			}
		}
		else if ( regimen == list_regimen ) {
			if ( !strcmp_noend_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_list_item(str_wc, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
		else if ( regimen == ill_defined_regimen ) {
			if ( !strcmp_jump_wc(str_wc, word_wc, settings.ill_defined_search_percent) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_list_item(str_wc, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
	}

	free(str);

	return translate_count;
}
Exemplo n.º 3
0
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);
}