Example #1
0
/*
 * look for name=value as in $Version="1";foo=bar  and 
 * add name and value to the cookie .
 * Returns 1 on success, else a http error do
 */
static int parse_new_cookie_name(cookie c, const char* input, meta_error e)
{
	const char *s, *s2;
	cstring str;

	if( (s = strchr(input, ';')) == NULL) 
		return set_http_error(e, HTTP_400_BAD_REQUEST);

	/* skip ; and white space (if any) */
	if( (s = find_first_non_space(++s)) == NULL) {
		/* If all we had was ws */
		return set_http_error(e, HTTP_400_BAD_REQUEST);
	}

	if( (s2 = strchr(s, '=')) == NULL) {
		/* Missing = in Name= */
		return set_http_error(e, HTTP_400_BAD_REQUEST);
	}

	if( (str = cstring_new()) == NULL)  
		return set_os_error(e, errno);

	if(!cstring_ncopy(str, s, (size_t) (s2 - s + 1))) {
		cstring_free(str);
		return set_os_error(e, errno);
	}
		
	if(!cookie_set_name(c, c_str(str))) {
		cstring_free(str);
		return set_os_error(e, errno);
	}
		
	s = s2 + 1;
	cstring_recycle(str);
	if( (s2 = strchr(s, ';')) == NULL) {
		/* Missing ; in Name=value; */
		cstring_free(str);
		return set_http_error(e, HTTP_400_BAD_REQUEST);
	}

	if(!cstring_ncopy(str, s, (size_t)(s2 - s + 1))) {
		cstring_free(str);
		return set_os_error(e, errno);
	}

	if(!cookie_set_value(c, c_str(str))) {
		cstring_free(str);
		return set_os_error(e, errno);
	}

	cstring_free(str);
	return 1;
}
Example #2
0
struct info parse_csv_line(char* line) {
  struct info info_ins;
  char* p;
  // id
  p = strtok(line, ",\n");
  strncpy(info_ins.id, find_first_non_space(p), 10);
  info_ins.id[10] = '\0';

  // name
  p = strtok(NULL, ",\n");
  strncpy(info_ins.name, find_first_non_space(p), 127);
  info_ins.name[127] = '\0';

  // department
  p = strtok(NULL, ",\n");
  strncpy(info_ins.department, find_first_non_space(p), 4);
  info_ins.department[4] = '\0';

  // age
  p = strtok(NULL, ",\n");
  uint16_t num = atoi(find_first_non_space(p));
  info_ins.age = htobe16(num);
  return info_ins;
}