Ejemplo n.º 1
0
  //
  // trim_string
  //
  // The following function removes trailing white space or comments
  // from the given string. There is no error checking on the string
  // handling.
  //
  static void trim_string(string &the_string)
  {
    const char *start = the_string.c_str();
    const char *p;

    // First let's see if there is a comment in this string. If so, kill it.
    if ((p = strchr(start, comment_char)) != 0) {
      the_string = the_string.substr(0, p - start);
      start = the_string.c_str();
        // Refresh 'start' since the trim operation invalidated it.
    }
    
    // If the string has zero size we are done.
    if (the_string.size() == 0) return;

    // Next let's jump to the end and back up over white space.
    p = strchr(start, '\0');
    --p;
    while (is_white(*p) && p != start) p--;

    // If the loop above broke because of a non-white space, retract one step.
    if (!is_white(*p)) p++;

    // Chop, hack, and slash.
    the_string = the_string.substr(0, p - start);
  }
Ejemplo n.º 2
0
// -------------------------------------------------------------------
// Determine pawn mobility
//  - returns a list of all squares where the pawn may move
// -------------------------------------------------------------------
move_list ChessBoard::mobility_pawn(int from) const {
    chessmove m;
    move_list ml;
    m.promotion=Empty;
    m.from=from;
    const int ahead=is_white(square[from])?1:-1;
    const int capture_destination[]=
        {m.from+ahead*Rank+File, m.from+ahead*Rank-File};
    int i;

    // Check for captures (normal or en-passant)
    for (i=0;i<2;i++) {
        m.to=capture_destination[i];
        if (abs(which_file(m.from)-which_file(m.to))==1 &&
            (is_white(square[m.from]) && is_black(square[m.to]) ||
             is_black(square[m.from]) && is_white(square[m.to]) ||
             en_passant==m.to) && !causes_check(m)) {
            if (is_edge(m.to)) {
                m.promotion=Bishop; ml.push_front(m);
                m.promotion=Rook;   ml.push_front(m);
                m.promotion=Knight; ml.push_front(m);
                m.promotion=Queen;  ml.push_front(m);
                m.promotion=Empty;
            }
            else
                ml.push_front(m);
        }
    }

    // Check two squares ahead
    m.to=m.from+ahead*2*Rank;
    if (which_rank(from)==1 && is_white(square[from]) &&
        square[m.to]==Empty && square[m.from+ahead*Rank]==Empty &&
        !causes_check(m))
        ml.push_front(m);
    else if (which_rank(from)==6 && is_black(square[from]) &&
        square[m.to]==Empty && square[m.from+ahead*Rank]==Empty &&
        !causes_check(m))
        ml.push_front(m);

    // Check one square ahead
    m.to=m.from+ahead*Rank;
    if (!is_edge(m.to) && square[m.to]==Empty && !causes_check(m))
        ml.push_front(m);
    else if (is_edge(m.to) && square[m.to]==Empty && !causes_check(m)) {
        m.promotion=Bishop; ml.push_front(m);
        m.promotion=Rook;   ml.push_front(m);
        m.promotion=Knight; ml.push_front(m);
        m.promotion=Queen;  ml.push_front(m);
    }

    return ml;
}
Ejemplo n.º 3
0
  //
  // analyze_line
  //
  // Note that this function does not do any error checking regarding
  // the handling of string objects or the dictionary. Note also that it
  // allows for a null name. A line in the form: "=VALUE" assigns the
  // string "VALUE" to the name "". This might be useful for some
  // programs. This behavior is currently undocumented.
  //
  static void analyze_line(const string &the_line, bool personalized)
  {
    const char *p = the_line.c_str();
    bool            happy = false;  // =true if this line has the right syntax.
    dictionary_entry temp;

    temp.personalized = personalized;

    if (blank_line(the_line)) return;
    while (*p && is_white(*p)) p++;

    // Copy the first word into the temporary dictionary entry.
    while (*p && *p != '=' && !is_white(*p)) {
      temp.name.append(1, *p++);
    }

    while (*p && is_white(*p)) p++;

    if (*p++ == '=') {

      // Ok, the syntax is good enough.
      happy = true;

      while (*p && is_white(*p)) p++;

      // Copy to the end of the line into the temporary dictionary
      // entry. Note that this behavior includes embeded white spaces in
      // the value, and it also includes trailing white spaces.
      // 
      while (*p) temp.value.append(1, *p++);
      trim_string(temp.value);
    }

    // If the syntax looks good, then lets add this information to the
    // dictionary. Otherwise we'll just ignore this line.
    // 
    if (happy) {
      list<dictionary_entry>::iterator stepper = the_dictionary.begin();

      while (stepper != the_dictionary.end()) {
        if (stepper->name == temp.name) {
          *stepper = temp;
          break;
        }
        stepper++;
      }

      if (stepper == the_dictionary.end()) {
        the_dictionary.push_back(temp);
      }
    }
  }
Ejemplo n.º 4
0
  inline bool thin_hs_hit_and_miss(const T& in, T& H_M, 
				   const size_t& j, const size_t& k) {
    bool flag;

    /* HIT operation */
    flag = false;
    for (size_t r = 1; r < in.nrows() - 1; ++r) {
      for (size_t c = 1; c < in.ncols() - 1; ++c) {
	for (size_t l = 0; l < 3; ++l) {
	  for (size_t m = 0; m < 3; ++m) {
	    if (is_white(in.get(Point(c + m - 1, r + l - 1)))) {
	      if (thin_hs_elements[j][l] & (1 << m))
		goto remove;
	    } else {
	      if (thin_hs_elements[k][l] & (1 << m))
		goto remove;
	    }
	  }
	}

	H_M.set(Point(c, r), black(H_M));
	flag = true; 
	continue;
      remove:
	H_M.set(Point(c, r), white(H_M));
      }
    }

    return flag;
  }
Ejemplo n.º 5
0
/* get the next token out of a scanner */
token_t * lexer_nextitem(scanner_t *s) {
  /* try to match longest tokens first */
  static lexcomp_t (*tokenizers[])(scanner_t*) = {
    tokenize_text,
    tokenize_identifier,
    tokenize_number,
    tokenize_bitops,
    tokenize_relops,
    tokenize_mathops,
    tokenize_miscops,
  };

  lexcomp_t lc;
  size_t i;

  /* consume all whitespace */
  while (is_white(scanner_advance(s)));
  scanner_backup(s);
  scanner_ignore(s);

  if (scanner_peek(s) == 0)
    return token_init(tokStackEmpty, "");

  for (i = 0; i < sizeof(tokenizers)/sizeof(tokenizers[0]); i++) {
    if ((lc = tokenizers[i](s)) != tokNoMatch) {
      token_t *t = (token_t*)scanner_accept(s, (acceptfn)tok_maker);
      t->lexcomp = lc;
      return t;
    }
  }

  return token_init(tokNoMatch, "");
}
Ejemplo n.º 6
0
// -------------------------------------------------------------------
// Determine whether the given move puts one's own king in check
// -------------------------------------------------------------------
bool ChessBoard::causes_check(const chessmove& m) const {
    bool w_turn=is_white(square[m.from]);
    ChessBoard c=*this;
    piece_type promotion=m.promotion;

    if (c.square[m.from]==w_Pawn && c.square[m.to]==Empty && 
        m.to!=m.from+Rank) {
        c.square[m.to]=w_Pawn;                      // White captures en-passant
        c.square[m.from]=c.square[m.to-Rank]=Empty;
    }
    else if (c.square[m.from]==b_Pawn && c.square[m.to]==Empty &&
        m.to!=m.from-Rank) {
        c.square[m.to]=b_Pawn;                      // Black captures en-passant
        c.square[m.from]=c.square[m.to+Rank]=Empty;
    }
    else {
        promotion=make_colour(promotion, w_turn);   // Normal move
        c.square[m.to]=(promotion!=Empty)?promotion:c.square[m.from];
        c.square[m.from]=Empty;
    }

    // Update king position
    if (c.square[m.to]==w_King)
        c.w_king_pos=m.to;
    else if (c.square[m.to]==b_King)
        c.b_king_pos=m.to;

    return w_turn?
        c.is_attacked(!w_turn, c.w_king_pos):
        c.is_attacked(!w_turn, c.b_king_pos);
}
Ejemplo n.º 7
0
/* Raw parsing */
static uint16_t hb_parse_character( hb_csv_file_t * file )
{
    int byte;
    uint16_t c = 0;
    int need_char = 1;

    if( file == NULL )
    {
        return CSV_CHAR_ERROR;
    }

    while( need_char )
    {
        byte = fgetc( file->fileref );
        if( feof( file->fileref ) )
        {
            return CSV_CHAR_EOF;
        }
        if( ferror( file->fileref ) )
        {
            return CSV_CHAR_ERROR;
        }

        if( file->parse_state == CSV_PARSE_SEEK && is_white(byte) )
        {
            continue;
        }
        else if( file->parse_state != CSV_PARSE_ESC && is_esc(byte) )
        {
            file->parse_state = CSV_PARSE_ESC;
            continue;
        }
        else if( file->parse_state != CSV_PARSE_ESC && is_sep(byte) )
        {
            file->parse_state = CSV_PARSE_SEEK;
            need_char = 0;
            c = CSV_CHAR_COLSEP;
        }
        else if( file->parse_state == CSV_PARSE_ESC )
        {
            file->parse_state = CSV_PARSE_NORMAL;
            need_char = 0;
            c = (uint16_t)byte;
        }
        else if( is_newline(byte) )
        {
            file->parse_state = CSV_PARSE_SEEK;
            need_char = 0;
            c = CSV_CHAR_ROWSEP;
        }
        else
        {
            file->parse_state = CSV_PARSE_NORMAL;
            need_char = 0;
            c = (uint16_t)byte;
        }
    }

    return c;
}
Ejemplo n.º 8
0
SCALAR CCheckerMaterial::getReflectance(const VECTOR &loc) const{
	if(is_white(loc)){
		return tile_white->getReflectance(loc);
	}else{
		return tile_black->getReflectance(loc);
	}
}
Ejemplo n.º 9
0
/* Entered after the "<!DOCTYPE" sequence. Ready to read the rest.
 */
static char
read_doctype(SaxDrive dr) {
    int		line = dr->buf.line;
    int		col = dr->buf.col - 10;
    char	*s;

    buf_backup(&dr->buf); /* back up to the start in case the cdata is empty */
    buf_protect(&dr->buf);
    read_delimited(dr, '>');
    if (dr->options.smart && 0 == dr->hints) {
	for (s = dr->buf.str; is_white(*s); s++) { }
	if (0 == strncasecmp("HTML", s, 4)) {
	    dr->hints = ox_hints_html();
	}
    }
    *(dr->buf.tail - 1) = '\0';
    if (dr->has.doctype) {
        VALUE       args[1];

	if (dr->has.line) {
	    rb_ivar_set(dr->handler, ox_at_line_id, LONG2NUM(line));
	}
	if (dr->has.column) {
	    rb_ivar_set(dr->handler, ox_at_column_id, LONG2NUM(col));
	}
        args[0] = rb_str_new2(dr->buf.str);
        rb_funcall2(dr->handler, ox_doctype_id, 1, args);
    }
    dr->buf.str = 0;

    return buf_get(&dr->buf);
}
Ejemplo n.º 10
0
/* The character after the character after the word is returned. dr->buf.tail is one past that. dr->buf.str will point to the
 * token which will be '\0' terminated.
 */
static char
read_name_token(SaxDrive dr) {
    char        c;

    dr->buf.str = dr->buf.tail;
    c = buf_get(&dr->buf);
    if (is_white(c)) {
        c = buf_next_non_white(&dr->buf);
        dr->buf.str = dr->buf.tail - 1;
    }
    while (1) {
	switch (c) {
	case ' ':
	case '\t':
	case '\f':
	case '?':
	case '=':
	case '/':
	case '>':
	case '<':
	case '\n':
	case '\r':
            *(dr->buf.tail - 1) = '\0';
	    return c;
	case '\0':
            /* documents never terminate after a name token */
            ox_sax_drive_error(dr, NO_TERM "document not terminated");
            return '\0';
	default:
	    break;
	}
        c = buf_get(&dr->buf);
    }
    return '\0';
}
Ejemplo n.º 11
0
COLOR  CCheckerMaterial::getDiffuseColor(const VECTOR &loc) const{
	if(is_white(loc)){
		return tile_white->getDiffuseColor(loc);
	}else{
		return tile_black->getDiffuseColor(loc);
	}
}
Ejemplo n.º 12
0
/*************************************************************************
 *
 *N  return_token
 *
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Purpose:
 *P
 *     This function returns the first token string found in the
 *     expression string.
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Parameters:
 *A
 *    expr   <input>==(char *) selection expression string.
 *    token <output>==(char *) first token in the string.
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   History:
 *H
 *    Barry Michaels    May 1991                          DOS Turbo C
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   External Variables:
 *X
 *    None
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Functions Called:
 *F
 *    None
 *E
*************************************************************************/
static void return_token( char *expr, char *token )
{
   register int i,j,n,found=0,stopflag;

   n = 0;
   stopflag=0;
   while (expr[0] == ' ') {
      for (i=0;i<ndelim;i++)
	 if (rspf_strncasecmp(expr,delimstr[i],(unsigned int)strlen(delimstr[i])) == 0) {
	    stopflag=1;
	    break;
	 }
      if (stopflag) break;
      expr++;
   }
   strcpy(token,expr);
   for (i=0;(unsigned int)i<strlen(token);i++) {
      for (j=0;j<ndelim;j++) {
	 if (rspf_strncasecmp(expr,delimstr[j],(unsigned int)strlen(delimstr[j]))==0) {
	    if (n>0)
	       token[i] = '\0';
	    else
	       token[strlen(delimstr[j])] = '\0';
	    found = 1;
	    break;
	 }
      }
      if ((found) || (!is_white(*expr))) n++;
      if ((!found)&&(*expr)) expr++;
/*      if (!is_white(*expr)) n++;  */
/*      if ((found) || (!is_white(*expr))) n++; */
      if (found) break;
   }
}
Ejemplo n.º 13
0
static gchar* get_prefix(glong index)
{
    gchar* prefix;
    gchar space = ' ';
    if (view.current == index && (view.top_x > 0 || is_white(index)) )
        space = '>';
    prefix = g_strnfill(1, space);

    if (conf.numbers)
    {
        gchar* tmp = prefix;
        glong max_n = num_digits(SL);
        glong n = num_digits(index);
        gchar* str_index = g_strdup_printf("%ld", index);
        gchar* num_prefix = g_strnfill(max_n - n, ' ');
        prefix = g_strconcat(num_prefix, str_index, tmp, NULL);
        g_free(tmp);
        g_free(num_prefix);
        g_free(str_index);
    }

    if (conf.checkbox)
    {
        gchar* symbol = " ";
        if (is_checked(index))
        {
            symbol = "x";
            if (conf.radiobox) symbol = "o";
        }
        gchar* tmp = prefix;
        prefix = g_strconcat(tmp, "[", symbol, "]", NULL);
        g_free(tmp);
    }
    return prefix;
}
Ejemplo n.º 14
0
/* function
 *
 *     find first tab or space from end of buffer
 */
int find_first(int from)
{
    int i;
    for (i = from; i >= 0; --i)
        if (is_white(buffer[i]))
            return i;
    return -1;
}
Ejemplo n.º 15
0
Archivo: gc.c Proyecto: anehing/mruby
void
mrb_gc_mark(mrb_state *mrb, struct RBasic *obj)
{
  if (obj == 0) return;
  if (!is_white(obj)) return;
  mrb_assert((obj)->tt != MRB_TT_FREE);
  add_gray_list(mrb, obj);
}
Ejemplo n.º 16
0
void
mrb_gc_mark(mrb_state *mrb, struct RBasic *obj)
{
  if (obj == 0) return;
  if (!is_white(obj)) return;
  gc_assert(!is_dead(mrb, obj));
  add_gray_list(mrb, obj);
}
Ejemplo n.º 17
0
int main()
{
    int c;                 /* character */
    int len;               /* current line length */
    int tos;               /* index to last tab or space */

    tos = -1;              /* no tabs or spaces read so far */
    len = 0;               /* set index to first character */

    while ((c = getchar()) != EOF)
    {
        if (c == '\n')              /* end of line */
        {
            print_line(len);
            len = 0;
            tos = -1;
        }
        else if (len == FOLD)        /* buffer full and next char not a newline */
        {
            if (tos < 0)             /* no tabs or spaces in line */
            {
                print_line(FOLD);
                buffer[0] = c;                 /* add char to next line */
                len = 1;                       /* set for next char */
                tos = (is_white(c)) ? 0 : -1;  /* is it a white space char */
            }
            else
            {
                print_line(tos);
                shift_left(tos + 1);   /* from first non white space */
                len = FOLD - tos -1;
                buffer[len] = c;
                tos = find_first(len);
                ++len;
            }
        }
        else
        {
            buffer[len] = c;
            if (is_white(c))
                tos = len;
            ++len;
        }
    }
}
Ejemplo n.º 18
0
static size_t		rm_debut(char const *s)
{
	size_t	i;

	i = 0;
	while (is_white(s[i]))
		i++;
	return (i);
}
Ejemplo n.º 19
0
static size_t		rm_fin(char const *s)
{
	size_t len;

	len = ft_strlen(s);
	len--;
	while (is_white(s[len]))
		len--;
	return (len);
}
Ejemplo n.º 20
0
int get_token(void)
    {
    int i;
    while (is_white(*cur_pos) && *cur_pos) cur_pos++;

    if (*cur_pos == NULL)
        {
        cur_token[0] = NULL;
        token_type = Unknown;
        return 0;   /* end of line */
        }

    /* check relation operator */
    if (strchr("!<>=", *cur_pos))
        {
        cur_token[0] = *cur_pos++;  /* get first char */
        cur_token[1] = NULL;
        if (*cur_pos == '=')   /*  ==, !=, >=, <=  */
            {
            cur_token[1] = *cur_pos++;
            cur_token[2] = NULL;
            }
        if (strcmp(cur_token, "=") == 0) token_type = Operator;
        else token_type = Rel_Op;
        return 1;
        }
    if (is_delim(*cur_pos))
        {
        cur_token[0] = *cur_pos++;
        cur_token[1] = NULL;
        token_type = Unknown;
        return 1;
        }
    if (is_alpha(*cur_pos))
        {
        i = 0;
        while (!is_delim(*cur_pos))
            cur_token[i++] = *cur_pos++;
        cur_token[i] = NULL;
        if (lookup_var(cur_token) != -1) token_type = Variable;
        else if (lookup_func(cur_token) != -1) token_type = Function;
        else token_type = Unknown;
        return 1;
        }
    if (is_digit(*cur_pos))
        {
        i = 0;
        while (is_digit(*cur_pos))
            cur_token[i++] = *cur_pos++;
        cur_token[i] = NULL;
        token_type = Number;
        return 1;
        }
    return 0;
    }
Ejemplo n.º 21
0
  //
  // blank_line
  //
  // The following function returns true if the given string is blank. A
  // blank string is one that has nothing in it, has only white space in
  // it, or contains only a possibly indented comment.
  //
  static bool blank_line(const string &the_line)
  {
    const char *p = the_line.c_str();

    while (*p) {
      if (*p == comment_char) return true;
      if ( !is_white(*p) ) return false;
      p++;
    }
    return true;
  }
Ejemplo n.º 22
0
static  char  *
trim_line(
     char *ptr
)
{
    register char	*lastp;

    skip_white(ptr);
    for (lastp = ptr + strlen(ptr) - 1;
	lastp >= ptr && (is_white(*lastp) || *lastp == '\n'); lastp--) ;
    *(lastp + 1) = 0;
    return ptr;		/* return lastp > ptr ? ptr : NULL; */
}
Ejemplo n.º 23
0
// -------------------------------------------------------------------
// Determine king mobility
//  - returns a list of all squares where the king may move
// -------------------------------------------------------------------
move_list ChessBoard::mobility_king(int from) const {
    chessmove m;
    move_list ml;
    m.promotion=Empty;
    m.from=from;

    // Potential destinations relative to king's position
    static const int relative_destination[] = {-Rank-File, -Rank, -Rank+File,
        -File, File, Rank-File, Rank, Rank+File};

    for (int i=0;i<8;i++) {
        m.to=from+relative_destination[i];
        if (is_dest(m.from,m.to) && abs(which_file(from)-which_file(m.to))<=1 &&
            !causes_check(m))
            ml.push_front(m);
    }

    // Can king castle kingside?
    register bool white=is_white(square[from]);
    if (// 1) Castling flag is set (i.e. king/rook not moved/captured)
        (white ? w_castle_k : b_castle_k) == true  &&
        // 2) King is not castling to escape check
        (white ? w_check    : b_check)    == false &&
        // 3) King is not castling through check
        is_attacked(!white, from+File)    == false &&
        // 4) King is not castling into check
        is_attacked(!white, from+2*File)  == false &&
        // 5) Squares between king and rook are empty
        square[from+File]==Empty && square[from+2*File]==Empty ) {
            m.to=from+2*File;
            ml.push_front(m);
    }

    // Can king castle queenside?
    if (// 1) Castling flag is set (i.e. king/rook not moved/captured)
        (white ? w_castle_q : b_castle_q) == true  &&
        // 2) King is not castling to escape check
        (white ? w_check    : b_check)    == false &&
        // 3) King is not castling through check
        is_attacked(!white, from-File)    == false &&
        // 4) King is not castling into check
        is_attacked(!white, from-2*File)  == false &&
        // 5) Squares between king and rook are empty
        square[from-File]==Empty && square[from-2*File]==Empty &&
        square[from-3*File]==Empty) {
            m.to=from-2*File;
            ml.push_front(m);
    }

    return ml;
}
Ejemplo n.º 24
0
static void hb_trim_end( char *text )
{
    if( text == NULL )
    {
        return;
    }

    int i;

    for( i = strlen(text) - 1; i >= 0 && is_white(text[i]) ; i-- )
    {
        text[i] = '\0';
    }
}
Ejemplo n.º 25
0
Archivo: gc.c Proyecto: anehing/mruby
void
mrb_field_write_barrier(mrb_state *mrb, struct RBasic *obj, struct RBasic *value)
{
  if (!is_black(obj)) return;
  if (!is_white(value)) return;

  mrb_assert(!is_dead(mrb, value) && !is_dead(mrb, obj));
  mrb_assert(is_generational(mrb) || mrb->gc_state != GC_STATE_NONE);

  if (is_generational(mrb) || mrb->gc_state == GC_STATE_MARK) {
    add_gray_list(mrb, value);
  }
  else {
    mrb_assert(mrb->gc_state == GC_STATE_SWEEP);
    paint_partial_white(mrb, obj); /* for never write barriers */
  }
}
Ejemplo n.º 26
0
// Indent a region n tab stops.
int indentRegion(Value *rp,int n) {
	Line *lnp;
	int count;
	Dot *dotp;

	// Validate n and determine number of tab stops.
	if(n == INT_MIN)
		count = 1;
	else if(n < 0)
		return rcset(FAILURE,0,text39,text137,n,0);
			// "%s (%d) must be %d or greater","Repeat count"
	else
		count = n;

	// Get number of lines.
	if(reglines(&n,NULL) != SUCCESS)
		return rc.status;
	dotp = &curwp->w_face.wf_dot;

	// Loop through lines in block.
	kentry.lastflag &= ~CFVMOV;
	do {
		dotp->off = 0;				// Start at the beginning.
		lnp = dotp->lnp;

		// Shift current line using tabs.
		if(lnp->l_used > 0 && !is_white(lnp,lnp->l_used) && !((curbp->b_modes & MDC) && lgetc(lnp,dotp->off) == '#')) {
			if(stabsize == 0)
				(void) linsert(count,'\t');
			else {
				begintxt();
				(void) instab(count);
				}
			if(rc.status != SUCCESS)
				return rc.status;
			}

		// Move to the next line.
		(void) forwln(1);			// Can't fail.
		} while(--n > 0);

	dotp->off = 0;
	kentry.thisflag &= ~CFVMOV;			// Flag that this resets the goal column.
	lchange(curbp,WFEDIT);
	return rc.status;
	}
Ejemplo n.º 27
0
/* Prend une ligne, entrée par l'utilisateur, se composant d'une
   commande et de ses éventuels arguments, et l'exécute */
void execute_line(char* line) {
  line = stripwhite(line);

  char** args;
  int args_nb = 0;
  unsigned int i, toggle;
  size_t len = strlen(line);
  for(i=0, toggle=0; line[i] != '\0'; i++) {
    if(is_white(line[i])) {
      if(toggle) toggle = 0;
      line[i] = '\0';
    } else {
      if(!toggle) toggle = 1;
      args_nb++;
    }
  }
  
  args = malloc((args_nb+1) * sizeof(char*));
  
  int a = 0;
  for(i=0, toggle=0; i < len; i++) {
    if(line[i] == '\0') {
      if(toggle) toggle = 0;
    } else {
      if(!toggle) {
	toggle = 1;
	args[a++] = &line[i];
      }
    }
  }
  args[a] = NULL;

  /* Comparaisons pour trouver de quelle commande il s'agit */

  for(i=0; env.commands[i].name != NULL; i++) {
    if(!strcmp(args[0], env.commands[i].name)) {
      env.commands[i].f(args, &env);
      free(args);
      return;
    }
  }

  printf("Command not found : %s\n", args[0]);
  free(args);
}
Ejemplo n.º 28
0
int	get_token( FILE * h, char * token, int tlen )
{
	int	c;
	int	i=0;
	int	quoting=0;
	while ( i < tlen )
	{
		if (!(c = getch(h)))
			break;
		else if ( c == '"' )
		{
			quoting = c;
			continue;
		}
		else if ( c == quoting ) 
		{
			quoting = 0;
			break;
		}
		else if ( quoting )
		{
			*(token+i) = c;
			i++;
			continue;
		}
		else if ( is_white( c ) )
		{
			ungetch( c );
			break;
		}
		else if ( is_punctuation( c ) )
		{
			ungetch( c );
			break;
		}
		else
		{
			*(token+i) = c;
			i++;
			continue;
		}
	}
	*(token+i) = 0;
	return(i);
}
Ejemplo n.º 29
0
    void operator()(const Mat& mat, char* data) {
      char* i = data;
      typename Mat::const_row_iterator row = mat.row_begin();
      typename Mat::const_col_iterator col;
      ImageAccessor<OneBitPixel> acc;
      unsigned char tmp;
      for (; row != mat.row_end(); ++row) {
	for (col = row.begin(); col != row.end(); ++col) {
	  if (is_white(acc(col)))
 	    tmp = 255;
 	  else
 	    tmp = 0;
	  *(i++) = tmp;
	  *(i++) = tmp;
	  *(i++) = tmp;
	}
      }
    }
Ejemplo n.º 30
0
/* The character after the quote or if there is no quote, the character after the word is returned. dr->buf.tail is one past
 * that. dr->buf.str will point to the token which will be '\0' terminated.
 */
static char
read_quoted_value(SaxDrive dr) {
    char	c;

    c = buf_get(&dr->buf);
    if (is_white(c)) {
        c = buf_next_non_white(&dr->buf);
    }
    if ('"' == c || '\'' == c) {
	char	term = c;

        dr->buf.str = dr->buf.tail;
        while (term != (c = buf_get(&dr->buf))) {
            if ('\0' == c) {
                ox_sax_drive_error(dr, NO_TERM "quoted value not terminated");
                return '\0';
            }
        }
	// dr->buf.tail is one past quote char
	*(dr->buf.tail - 1) = '\0'; /* terminate value */
	c = buf_get(&dr->buf);
	return c;
    }
    // not quoted, look for something that terminates the string
    dr->buf.str = dr->buf.tail - 1;
    ox_sax_drive_error(dr, WRONG_CHAR "attribute value not in quotes");
    while ('\0' != (c = buf_get(&dr->buf))) {
	switch (c) {
	case ' ':
	case '/':
	case '>':
	case '?': // for instructions
	case '\t':
	case '\n':
	case '\r':
	    *(dr->buf.tail - 1) = '\0'; /* terminate value */
	    // dr->buf.tail is in the correct position, one after the word terminator
	    return c;
	default:
	    break;
	}
    }
    return '\0'; // should never get here
}