Example #1
0
void snarf2(unsigned char* bytes, int size)
{
	int i=0;
	while (i < size)
	{
		while (i < size)
		{
			put_byte(bytes[i]);
			i++;
			if (cline_valid(0))
			{
//				printf("was valid\n");
				break;
			}
		}

		if(i >= size)
			break;

//		printf("Cline went valid\n");
		consume_line();
	}

	if(cline_valid(0))
	{
		consume_line();
//		printf("was valid2\n");
	}
}
Example #2
0
obj_t::obj_t( const char * objname ) 
{
    // long name
    fullpath = realname( objname ); 

    // model handle
    const char * shortname = strip_path_and_extension( objname );
    name = copy_string( shortname );

    // memory map file
    mmfile_t mm( fullpath ); 
    index = 0;
    size = mm.size;
    data = mm.data;
    
    
    while ( index < size ) 
    {
        strip_white_space();

        switch( data[i] ) 
        {
        case 'm':
            if ( check_name( "mtllib" ) )
                consume_line();
            break;
        case 'u':
            if ( check_name( "usemtl" ) )
                consume_line();
            break;
        case 'g':
            consume_line();
            break;
        case '#':
            strip_white_space();
            break;
        case 'v':
            if ( check_name( "vn" ) )
                add_normal();
            else if ( check_name( "vt" ) )
                add_texcoord();
            else if ( data[index+1] == ' ' )
                add_vertex();
            else
                core.warn( "obj_t: straggling v in objfile: %s", name );
            break;
        case 'f':
            if ( data[index+1] == ' ' ) 
                add_face();
            break;
        case 's':
        default:
            ++index;
            break;
        }
    }
}
Example #3
0
File: reader.c Project: qyqx/wisp
/* Print an error message. */
static void read_error (reader_t * r, char *str)
{
  fprintf (stderr, "%s:%d: %s\n", r->name, r->linecnt, str);
  consume_line (r);
  reset (r);
  r->error = 1;
}
Example #4
0
int turn(void)
{
    char numbers[4], ok;
    int good = 0, i;
    double result;
    struct eval_ctx ctx;
    ctx.stream = stdin;

    for(i = 0; i < 4; i++)
        numbers[i] = rand() % 9 + 1;

    do
    {
        printf("\nYour numbers are %hhd, %hhd, %hhd and %hhd.\nMake 24 [expr/s/q]: ",
            numbers[0], numbers[1], numbers[2], numbers[3]);

        if (eval_consume(&ctx, "Ss"))
        {
            consume_line(stdin);
            return 1;
        }
        if (eval_consume(&ctx, "Qq"))
        {
            consume_line(stdin);
            return 0;
        }

        result = eval(stdin, numbers, &ok);

        if(!ok) continue;

        printf("Result is %f. ", result);

        if((good = validate_answer(result)))
            puts("Correct!");
        else
            puts("Wrong!");

    } while (!good);

    return 1;
}
Example #5
0
double eval(FILE* stream, char allowed_nums[4], char* ok)
{
    int i, c;
    double result;
    struct eval_ctx ctx;
    ctx.stream = stream;
    ctx.err = NULL;

    /* we make a copy instead of pointing to the original array so that the can
       remove the numbers when we see them in the expression */
    memcpy(ctx.allowed_nums, allowed_nums, sizeof allowed_nums);

    result = eval_expr(&ctx);

    if((c = getc(stream)) != '\n' && c != EOF)
    {   /* still got characters in the stream */
        if(!ctx.err)
            ctx.err = "Unexpected character";

        consume_line(stream);
    }

    for (i = 0; !ctx.err && i < 4; i++)
        if(ctx.allowed_nums[i] != -1)
            ctx.err = "Not all numbers were used";

    if (ctx.err)
    {
        puts(ctx.err);
        *ok = 0;
        return 0.;
    }

    *ok = 1;
    return result;
}
Example #6
0
File: reader.c Project: qyqx/wisp
/* Read a single sexp from the reader. */
object_t *read_sexp (reader_t * r)
{
  /* Check for a shebang line. */
  if (r->shebang == -1)
    {
      char str[2];
      str[0] = reader_getc (r);
      str[1] = reader_getc (r);
      if (str[0] == '#' && str[1] == '!')
	{
	  /* Looks like a she-bang line. */
	  r->shebang = 1;
	  consume_line (r);
	}
      else
	{
	  r->shebang = 0;
	  reader_putc (r, str[1]);
	  reader_putc (r, str[0]);
	}
    }

  r->done = 0;
  r->error = 0;
  push (r);
  print_prompt (r);
  while (!r->eof && !r->error && (list_empty (r) || stack_height (r) > 1))
    {
      int nc, c = reader_getc (r);
      switch (c)
	{
	case EOF:
	  r->eof = 1;
	  break;

	  /* Comments */
	case ';':
	  consume_line (r);
	  break;

	  /* Dotted pair */
	case '.':
	  nc = reader_getc (r);
	  if (strchr (" \t\r\n()", nc) != NULL)
	    {
	      if (r->state->dotpair_mode > 0)
		read_error (r, "invalid dotted pair syntax");
	      else if (r->state->vector_mode > 0)
		read_error (r, "dotted pair not allowed in vector");
	      else
		{
		  r->state->dotpair_mode = 1;
		  reader_putc (r, nc);
		}
	    }
	  else
	    {
	      /* Turn it into a decimal point. */
	      reader_putc (r, nc);
	      reader_putc (r, '.');
	      reader_putc (r, '0');
	    }
	  break;

	  /* Whitespace */
	case '\n':
	  r->linecnt++;
	  print_prompt (r);
	case ' ':
	case '\t':
	case '\r':
	  break;

	  /* Parenthesis */
	case '(':
	  push (r);
	  break;
	case ')':
	  if (r->state->quote_mode)
	    read_error (r, "unbalanced parenthesis");
	  else if (r->state->vector_mode)
	    read_error (r, "unbalanced brackets");
	  else
	    addpop (r);
	  break;

	  /* Vectors */
	case '[':
	  push (r);
	  r->state->vector_mode = 1;
	  break;
	case ']':
	  if (r->state->quote_mode)
	    read_error (r, "unbalanced parenthesis");
	  else if (!r->state->vector_mode)
	    read_error (r, "unbalanced brackets");
	  else
	    addpop (r);
	  break;

	  /* Quoting */
	case '\'':
	  push (r);
	  add (r, quote);
	  if (!r->error)
	    r->state->quote_mode = 1;
	  break;

	  /* strings */
	case '"':
	  buf_read (r, "\"");
	  add (r, parse_str (r));
	  reader_getc (r);	/* Throw away other quote. */
	  break;

	  /* numbers and symbols */
	default:
	  buf_append (r, c);
	  buf_read (r, " \t\r\n()[];");
	  object_t *o = parse_atom (r);
	  if (!r->error)
	    add (r, o);
	  break;
	}
    }
  if (!r->eof && !r->error)
    consume_whitespace (r);
  if (r->error)
    return err_symbol;

  /* Check state */
  r->done = 1;
  if (stack_height (r) > 1 || r->state->quote_mode
      || r->state->dotpair_mode == 1)
    {
      read_error (r, "premature end of file");
      return err_symbol;
    }
  if (list_empty (r))
    {
      obj_destroy (pop (r));
      return NIL;
    }

  object_t *wrap = pop (r);
  object_t *sexp = UPREF (CAR (wrap));
  obj_destroy (wrap);
  return sexp;
}
Example #7
0
static int
parse_file (FILE * in, FILE * out, IspellMode_t mode, int countLines, gchar *dictionary)
{
	EnchantBroker * broker;
	EnchantDict * dict;
	
	GString * str, * word = NULL;
	GSList * tokens, *token_ptr;
	gchar * lang;
	size_t pos, lineCount = 0;

	gboolean was_last_line = FALSE, corrected_something = FALSE, terse_mode = FALSE;

	if (mode == MODE_A)
		print_version (out);

	if (dictionary) {
		lang = convert_language_code (dictionary);
	}
	else {
	        lang = enchant_get_user_language();
		if(!lang)
			return 1;
 	}

	/* Enchant will get rid of useless trailing garbage like de_DE@euro or de_DE.ISO-8859-15 */
	
	broker = enchant_broker_init ();
	dict = enchant_broker_request_dict (broker, lang);

	if (!dict) {
		fprintf (stderr, "Couldn't create a dictionary for %s\n", lang);
		g_free (lang);
		enchant_broker_free (broker);
		return 1;
	}

	g_free (lang);

	str = g_string_new (NULL);
	
	while (!was_last_line) {
		gboolean mode_A_no_command = FALSE;
		was_last_line = consume_line (in, str);

		if (countLines)
			lineCount++;

		if (str->len) {
			corrected_something = FALSE;

			if (mode == MODE_A) {
				switch (*str->str) {
				case '&': /* Insert uncapitalised in personal word list */
					if (str->len > 1) {
						gunichar c = g_utf8_get_char_validated(str->str + 1, str->len);
						if (c > 0) {
							str = g_string_erase(str, 1, g_utf8_next_char(str->str + 1) - (str->str + 1));
							g_string_insert_unichar(str, 1, g_unichar_tolower(c));
						}
					}
					/* FALLTHROUGH */
				case '*': /* Insert in personal word list */
					if (str->len == 1)
						goto empty_word;
					enchant_dict_add(dict, str->str + 1, -1);
					break;
				case '@': /* Accept for this session */
					if (str->len == 1)
						goto empty_word;
					enchant_dict_add_to_session(dict, str->str + 1, -1);
					break;

				case '%': /* Exit terse mode */
					terse_mode = FALSE;
					break;
				case '!': /* Enter terse mode */
					terse_mode = TRUE;
					break;

				/* Ignore these commands */
				case '#': /* Save personal word list (enchant does this automatically) */
				case '+': /* LaTeX mode */
				case '-': /* nroff mode [default] */
				case '~': /* change string character type (enchant is fixed to UTF-8) */
				case '`': /* Enter verbose-correction mode */
					break;

				case '$': /* Save correction for rest of session [aspell extension] */
					{ /* Syntax: $$ra <MISSPELLED>,<REPLACEMENT> */
						gchar *prefix = "$$ra ";
						if (g_str_has_prefix(str->str, prefix)) {
							gchar *comma = g_utf8_strchr(str->str, -1, (gunichar)',');
							char *mis = str->str + strlen(prefix);
							char *cor = comma + 1;
							ssize_t mis_len = comma - mis;
							ssize_t cor_len = strlen(str->str) - (cor - str->str);
							enchant_dict_store_replacement(dict, mis, mis_len, cor, cor_len);
						}
					}
					break;

				case '^': /* ^ is used as prefix to prevent interpretation of original
					     first character as a command */
					/* FALLTHROUGH */
				default: /* A word or words to check */
					mode_A_no_command = TRUE;
					break;

				empty_word:
					fprintf (out, "Error: The word \"\" is invalid. Empty string.\n");
				}
			}

			if (mode != MODE_A || mode_A_no_command) {
				token_ptr = tokens = tokenize_line (str);
				if (tokens == NULL)
					putc('\n', out);
				while (tokens != NULL) {
					corrected_something = TRUE;

					word = (GString *)tokens->data;
					tokens = tokens->next;
					pos = GPOINTER_TO_INT(tokens->data);
					tokens = tokens->next;

					if (mode == MODE_A)
						do_mode_a (out, dict, word, pos, lineCount, terse_mode);
					else if (mode == MODE_L)
						do_mode_l (out, dict, word, lineCount);

					g_string_free(word, TRUE);
				}
				if (token_ptr)
					g_slist_free (token_ptr);
			}
		} 
		
		if (mode == MODE_A && corrected_something) {
			fwrite ("\n", 1, 1, out);
		}
		g_string_truncate (str, 0);
		fflush (out);
	}

	enchant_broker_free_dict (broker, dict);
	enchant_broker_free (broker);

	g_string_free (str, TRUE);

	return 0;
}