pike.git / lib / modules / Protocols.pmod / IMAP.pmod / types.pmod

version» Context lines:

pike.git/lib/modules/Protocols.pmod/IMAP.pmod/types.pmod:60:   }      class imap_number   {    int value;       void create(int n) { value = n; }       string format() { return sprintf("%d", value); }   } +  + /* Input types */ +  + // Returns -1 on error. + int string_to_number(string s) + { +  if (!strlen(s) || (strlen(s) > 9)) +  return -1; +  if (sizeof(values(s) - ({ '0', '1', '2', '3', '4', +  '5', '6', '7', '8', '9' }))) +  return -1; +  return array_sscanf(s, "%d")[0]; + } +  +  + class imap_set + { +  /* Each element is either an integer, the string "*", or an +  * array ({ start, end }) */ +  array items; +  +  void create(array|void a) +  { +  items = a; +  } +  +  int|string string_to_number_star(string s) +  { +  return (s == "*") ? s : string_to_number(s); +  } +  +  int|string|array string_to_subset(string s) +  { +  int i = search(s, ":"); +  +  if (i<0) +  return string_to_number_star(s); +  +  int|string start = string_to_number_star(s[..i-1]); +  if (start < 0) +  return -1; +  +  int end = string_to_number_star(s[i+1..]); +  if (intp(end) && (end < 0)) +  return -1; +  if (end == start) +  return start; +  +  return ({ start, end }); +  } +  +  object init(string s) +  { +  items = s/","; +  +  for(int i = 0; i<sizeof(items); i++) +  { +  items[i] = string_to_subset(items[i]); +  +  if (intp(items[i]) && (items[i]<0)) +  return 0; +  } +  return this_object(); +  } +  +  int replace_number(int|string i, int largest) +  { return (i == "*") ? largest : i; } +  +  array(int) expand_item(int|string|array item, int largest) +  { +  if (arrayp(item)) +  { +  int start = replace_number(item[0], largest); +  int end = replace_number(item[1], largest); +  +  if (end > largest) +  end = largest; +  +  if (end < start) +  return ({ }); +  +  array res = allocate(end - start + 1); +  for(int i = start; i<= end; i++) +  res[i-start] = i; +  +  return res; +  } +  int i = replace_number(item, largest); +  return (i > largest) ? ({ }) : ({ i }); +  } +  +  /* Return a plain array of integers. LARGEST is the number to be +  * substituted for "*". Handles empty intervals, and cuts away +  * elements larger than LARGEST. Does not attempt to handle +  * overlapping intervals intelligently. */ +  array(int) expand(int largest) +  { +  return `+( ({}), @Array.map(items, expand_item, largest)); +  } + } +