예제 #1
0
int Int_Look(var self, var input, int pos) {
  IntData* io = cast(self, Int);
  int off = 0;
  int err = format_from(input, pos, "%li%n", &io->value, &off);
  pos += off;
  return pos;
}
예제 #2
0
int scan_from_va(var input, int pos, const char* fmt, va_list va) {

  char fmt_buf[strlen(fmt)+1];
  
  while(true) {
    
    if (*fmt == '\0') { break; }
    
    const char* start = fmt;
    
    /* Match String */
    while(!strchr("%\0", *fmt)) { fmt++; }
    
    if (start != fmt) {
      
      strncpy(fmt_buf, start, (fmt - start));
      fmt_buf[(fmt - start)+0] = '\0';
      fmt_buf[(fmt - start)+1] = '%';
      fmt_buf[(fmt - start)+2] = 'n';
      
      int off = 0;
      int err = format_from(input, pos, fmt_buf, &off);
      if (err < 0) { throw(FormatError, "Unable to input format!"); }
      
      pos += off;
      continue;
    }
    
    /* Match %% */
    if (*fmt == '%' && *(fmt+1) == '%') {
      
      int err = format_from(input, pos, "%%");
      if (err < 0) { throw(FormatError, "Unable to input '%%%%'!"); }
      
      pos += 2;
      fmt += 2;
      continue;
    }
    
    /* Match Format Specifier */
    while(!strchr("diuoxXfFeEgGaAxcsp$\0", *fmt)) { fmt++; }
    
    if (start != fmt) {
    
      strncpy(fmt_buf, start, (fmt - start)+1);
      fmt_buf[(fmt - start)+1] = '\0';
      
      var a = va_arg(va, var);
      
      if (*fmt == '$') { pos = look_from(a, input, pos); }
      
      fmt_buf[(fmt - start)+1] = '%';
      fmt_buf[(fmt - start)+2] = 'n';
      fmt_buf[(fmt - start)+3] = '\0';
      
      int off = 0;
      
      if (*fmt == 's') {      
        int err = format_from(input, pos, fmt_buf, as_str(a), &off);
        if (err < 1) { throw(FormatError, "Unable to input String!"); }
        pos += off;
      }
      
      if (strchr("diouxX", *fmt)) {
        long tmp = 0;
        int err = format_from(input, pos, fmt_buf, &tmp, &off);
        if (err < 1) { throw(FormatError, "Unable to input Int!"); }
        pos += off;
        assign(a, $(Int, tmp));
      }
      
      if (strchr("fFeEgGaA", *fmt)) {
        if (strchr(fmt_buf, 'l')) {
          double tmp = 0;
          int err = format_from(input, pos, fmt_buf, &tmp, &off);
          if (err < 1) { throw(FormatError, "Unable to input Real!"); }
          pos += off;
          assign(a, $(Real, tmp));
        } else {
          float tmp = 0;
          int err = format_from(input, pos, fmt_buf, &tmp, &off);
          if (err < 1) { throw(FormatError, "Unable to input Real!"); }
          pos += off;
          assign(a, $(Real, tmp));
        }
      }
      
      if (*fmt == 'c') {
        char tmp = '\0';
        int err = format_from(input, pos, fmt_buf, &tmp, &off);
        if (err < 1) { throw(FormatError, "Unable to input Char!"); }
        pos += off;
        assign(a, $(Char, tmp));
      }
      
      if (*fmt == 'p') {
        void* tmp = NULL;
        int err = format_from(input, pos, fmt_buf, &tmp, &off);
        if (err < 1) { throw(FormatError, "Unable to input Reference!"); }
        pos += off;
        assign(a, $(Reference, tmp));
      }

      fmt++;
      continue;
    }
    
    throw(FormatError, "Invalid Format String!");
  }
  
  return pos;

}