Exemple #1
0
void		print_s(t_arg *arg, void *s)
{
	int		size;
	int		len;
	int		print;

	len = INT_MAX;
	if (arg->f_prec && arg->v_prec == 0)
		return (print_s_noprec(arg));
	useless_func(arg, (&s), 0, 0);
	len = arg->conv == 's' ? ft_strlen(s) : wcharlen(s);
	size = arg->v_prec && arg->v_prec < len ? arg->v_prec - (arg->conv == 'S') :
		ft_strlen(s);
	if (!arg->f_prec && (arg->conv == 'S'))
		size = wcharlen(s);
	print = size;
	if (arg->conv == 'S' && arg->v_prec == 1)
		++print;
	while (!arg->f_minus && arg->v_len >= ++print)
		arg->nb_char += arg->f_zero ? add_char_to_buffer(arg, '0') :
			add_char_to_buffer(arg, ' ');
	if (arg->conv == 'S' && arg->f_prec && arg->v_prec >= arg->v_len)
		useless_func(arg, (&s), arg->v_prec, 1);
	else
		useless_func(arg, (&s), size, 1);
}
Exemple #2
0
void	handle_c(t_arg *arg, va_list ap)
{
	int		i;

	i = arg->v_len;
	while (i > arg->v_prec && !arg->f_minus && i-- > 1)
		arg->nb_char += arg->f_zero ? add_char_to_buffer(arg, '0') :
			add_char_to_buffer(arg, ' ');
	while (!arg->f_minus && i-- > 1)
		arg->nb_char += add_char_to_buffer(arg, '0');
	if (arg->mod == NULL && arg->conv == 'c')
		arg->nb_char += add_char_to_buffer(arg, (char)va_arg(ap, int));
	else if ((arg->mod && ft_strcmp(arg->mod, "l") == 0) || arg->conv == 'C')
Exemple #3
0
static void	print_s_noprec(t_arg *arg)
{
	int		i;

	i = 0;
	while (i < arg->v_len)
	{
		if (!arg->f_zero)
			arg->nb_char += add_char_to_buffer(arg, ' ');
		else
			arg->nb_char += add_char_to_buffer(arg, '0');
		i++;
	}
}
Exemple #4
0
void parse_char(struct ptf_parser_state *state, int c)
{
  int is_not_quoted;
  int is_no_space;
  enum { item_parsed, section_opened, section_closed, none } parser_event;

  switch(c)
  {
    case '\\':
    {
      if(state->flag.escaped == 0)
      {
        state->flag.escaped = 1;
        return;
      };
      break;
    };
    case '"':
    {
      if(!state->flag.escaped && !state->flag.single_quoted)
      {
        state->flag.double_quoted = 1 - state->flag.double_quoted;
        return;
      }
      break;
    };
    case '\'':
    {
      if(!state->flag.escaped && !state->flag.double_quoted)
      {
        state->flag.single_quoted = 1 - state->flag.single_quoted;
        return;
      }
      break;
    };
    case '\n':
    {
      state->line++;
      break;
    };
    default:
      break;
  };

  parser_event = none;

  is_not_quoted = !(state->flag.escaped ||
    state->flag.single_quoted || state->flag.double_quoted);
  is_no_space = (!is_not_quoted || !isspace(c));
  state->flag.escaped = 0;

  switch(state->expectation)
  {
    case section_or_item_specification:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: section_or_item_specification\n");
#endif

      if(is_not_quoted && c == '}')
      {
        parser_event = section_closed;
      }
      else if(is_no_space)
      {
        state->name_length = 1;
        state->name_buffer[0] = c;
        state->expectation = more_section_type_or_item_name_chars;
      };
      break;
    };

    case more_section_type_or_item_name_chars:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: more_section_type_or_item_name_chars\n");
#endif

      /* Item name is stored in name_buffer */
      /* Section type is stored in name_buffer */
      if(is_no_space)
      {
        if(!add_char_to_buffer(&state->name_length, state->name_buffer, PTFPARSER_NAMEBUFSIZE, c))
          parse_error(state, "First word is too long; I expected a shorter section type or item name");
      }
      else
      {
        state->expectation = whitespace_after_section_or_item_name;
      }
      break;
    };

    case whitespace_after_section_specification:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: whitespace_after_section_specification\n");
#endif

      if(c == '{')
        parser_event = section_opened;
      else if(is_no_space)
        parse_error(state, "Expected section content within brackets {...}");
      break;
    };

    case whitespace_after_section_or_item_name:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: whitespace_after_section_or_item_name\n");
#endif

      if(c == '{')
      {
        state->value_length = 0;
        parser_event = section_opened;
      }
      else if(c == '=')
        state->expectation = whitespace_before_item_value;
      else if(is_no_space)
      {
        state->value_length = 1;
        state->value_buffer[0] = c;
        state->expectation = more_section_name_chars;
      };
      break;
    };

    case more_section_name_chars:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: more_section_name_chars\n");
#endif

      /* Section name is stored in value_buffer */
      if(is_no_space)
      {
        if(!add_char_to_buffer(&state->value_length, state->value_buffer, PTFPARSER_VALUEBUFSIZE, c))
          parse_error(state, "Section name is too long");
      }
      else
        state->expectation = whitespace_after_section_specification;
      break;
    }

    case whitespace_before_item_value:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: whitespace_before_item_value\n");
#endif

      if(is_not_quoted && c == ';')
      {
        state->value_length = 0;
        parser_event = item_parsed;
      }
      else if(is_no_space)
      {
        state->value_length = 1;
        state->value_buffer[0] = c;
        state->expectation = more_item_value_chars;
      };
      break;
    };

    case more_item_value_chars:
    {
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: more_item_value_chars\n");
#endif

      /* Item value is stored in value_buffer */
      if(is_not_quoted && c == ';')
        parser_event = item_parsed;
      else if(is_no_space)
      {
        if(!add_char_to_buffer(&state->value_length, state->value_buffer, PTFPARSER_VALUEBUFSIZE, c))
          parse_error(state, "Item value is too long");
      }
      else
        parser_event = item_parsed;
      break;
    }

    default:
#if DEBUG&DEBUG_EXPECTATIONS
      printf("Expectation: %d (?)\n", state->expectation);
#endif

      parse_error(state, "Internal error: Unhandled state of expectation");
  };

  switch(parser_event)
  {
    /* TODO: pointer tuff */

    case item_parsed:
    {
      struct ptf *new_item;
      state->name_buffer[state->name_length] = 0;
      state->value_buffer[state->value_length] = 0;
#if DEBUG&DEBUG_READVALUES
      printf("== Item %s is '%s' ==\n", state->name_buffer, state->value_buffer);
#endif

      new_item = ptf_alloc_item(item, state->name_buffer, state->value_buffer);
      if(new_item == NULL)
      {
        parse_error(state, "Internal error: "
            "Could not allocate memory for new item");
        return;
      };

      add_ptf_item(state, new_item);
      state->current_item = new_item;
      state->expectation = section_or_item_specification;

      break;
    };
    case section_opened:
    {
      struct ptf *new_section;
      state->name_buffer[state->name_length] = 0;
      state->value_buffer[state->value_length] = 0;
#if DEBUG&DEBUG_READVALUES
      printf("== New %s section '%s' opened ==\n", state->name_buffer, state->value_buffer);
#endif

      if(state->section_level >= PTFPARSER_MAXDEPTH-1)
      {
        parse_error(state, "Internal error: "
             "cannot handle sections nested as deep as here.");
        return;
      };

      new_section = ptf_alloc_item(section, state->name_buffer, state->value_buffer);
      if(new_section == NULL)
      {
        parse_error(state, "Internal error: "
            "Could not allocate memory for new section");
        return;
      };

      add_ptf_item(state, new_section);
      state->current_item = NULL;
      state->current_in[state->section_level] = new_section;
      state->section_level++;

      state->expectation = section_or_item_specification;
      break;
    };
    case section_closed:
    {
      if(state->section_level < 1)
      {
        parse_error(state, "Found closing '}' without opening '{' before");
        return;
      };

      state->section_level--;
      state->current_item = state->current_in[state->section_level];
      state->expectation = section_or_item_specification;
#if DEBUG&DEBUG_READVALUES
      printf("-- Closed section --\n");
#endif
      break;
    };
    default:
      break;
  };
}