int set_glb_default_ttl(void *zone_info, char *val) { assert(zone_info); assert(val); /* val值示例: "\t 86400 ;24 hours could have been written as 24h or 1d" */ char token[TOKEN_NAME_LEN_MAX]; int tmp_ret; tmp_ret = get_a_token_str(&val, token, NULL); if (tmp_ret != RET_OK) { SDNS_LOG_ERR("$TTL format err! [%s]", val); return RET_ERR; } if (!is_digit(token)) { SDNS_LOG_ERR("(%s) NOT int!", token); return RET_ERR; } ((ZONE_INFO *)zone_info)->default_ttl = atoi(token); return RET_OK; }
/** * convertion: hexadecimal to decimal */ long hextol(const char *str) { long r = 0; char *p = (char *) str; if (p == NULL) { return 0; } while (*p) { if (is_digit(*p)) { // 0123456789 r = (r << 4) + ((*p) - 48); } else if (*p >= 65 && *p <= 70) { // ABCDEF r = (r << 4) + ((*p) - 55); } else if (*p >= 97 && *p <= 102) { // abcdef r = (r << 4) + ((*p) - 87); } p++; } return r; }
double HttpRequest::getDouble ( char *field , double defaultDouble ) { long len; char *value = getValue ( field, &len, NULL ); // return default if no match if ( ! value || len == 0 ) return defaultDouble; // otherwise, it's a match char c = value[len]; value[len] = '\0'; double res = strtod ( value , NULL ); value[len] = c; if ( res == +0.0 ) { // may be an error. if so return the default long i = 0; while ( i < len && is_wspace_a(value[i]) ) i++; if ( i < len && (value[i] == '-' || value[i] == '+' || value[i] == '.') ) i++; if ( i >= len || !is_digit(value[i]) ) return defaultDouble; } return res; }
/* Test whether the character can be a part of a NCName */ static bool is_name_char(uint32_t ch) { /* Refer http://www.w3.org/TR/REC-xml/ for detail */ if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || /* !start */ (ch == '_') || (ch == ':') || (ch == '-') || (ch == '.') || (ch == 0xB7) || /* !start */ ((ch >= 0xC0) && (ch <= 0xD6)) || ((ch >= 0xD8) && (ch <= 0xF6)) || ((ch >= 0xF8) && (ch <= 0x2FF)) || ((ch >= 0x300) && (ch <= 0x36F)) || /* !start */ ((ch >= 0x370) && (ch <= 0x37D)) || ((ch >= 0x37F) && (ch <= 0x1FFF)) || ((ch >= 0x200C) && (ch <= 0x200D)) || ((ch >= 0x203F) && (ch <= 0x2040)) || /* !start */ ((ch >= 0x2070) && (ch <= 0x218F)) || ((ch >= 0x2C00) && (ch <= 0x2FEF)) || ((ch >= 0x3001) && (ch <= 0xD7FF)) || ((ch >= 0xF900) && (ch <= 0xFDCF)) || ((ch >= 0xFDF0) && (ch <= 0xFFFD)) || ((ch >= 0x10000) && (ch <= 0xEFFFF))) return true; if (is_letter(ch) == true) return true; if (is_digit(ch) == true) return true; if (is_combining_char(ch) == true) return true; if (is_extender(ch) == true) return true; if (ch == (uint32_t) '.' || ch == (uint32_t) '-' || ch == (uint32_t) '_' || ch == (uint32_t) ':') return true; return false; }
int main(int argc, char *argv[]) { program_name = argv[0]; exit_status = 0; if(argc != 2) reboot_usage_exit(EXIT_FAILURE); if(is_digit(argv[1],strlen(argv[1])) == 0) reboot_usage_exit(EXIT_FAILURE); if(reboot(atoi(argv[1])) != 0) { fprintf(stderr, "[%s] error [%s]\n", program_name, strerror(errno)); exit_status = errno; } exit(exit_status); }
// zn_proc_processor pid_list zn_proc_processor::_get_pidlist() { pid_list ret; DIR *dirp = opendir("/proc"); if (dirp != NULL) { struct dirent *direntp = NULL; while ((direntp = readdir(dirp)) != NULL) { if ((direntp->d_type == DT_DIR) && is_digit(direntp->d_name[0])) { ret.push_back(from_string<pid_t>(direntp->d_name)); } } closedir(dirp); std::sort(ret.begin(), ret.end()); } return ret; }
bool is_numeric_value(char ch, char *value) { assert(ch); assert(value); char test_value[2] = {ch, '\0'}; // dots are valid for numeric values, but only if we don't already have one if(test_value[0] == '.') { char *existing_dot = strchr(value, '.'); if(existing_dot) { return false; } return true; } else { return is_digit(ch, value); } }
float eval(char *expr) { STACK solution,temp; init_stack(&solution);init_stack(&temp); float value,operand1,operand2; while(*expr) { if(!is_permitted(*expr)){ fprintf(stderr,"error: invalid expression\n"); exit(1); } if(is_digit(*expr)) push(&temp, (float) (*expr - '0')); else if(is_delimiter(*expr)){ if(!is_empty(&temp)) push(&solution,process_data(&temp)); else { expr++;continue; } } else { operand2 = pop(&solution); operand1 = pop(&solution); value = operation(operand1,operand2,*expr); push(&solution,value); } expr++; } if(!is_empty(&temp)) push(&solution,process_data(&temp)); if(get_top(&solution)>0){ printf("error: invalid postfix expression\n"); exit(1); } return pop(&solution); }
int main(void) { unsigned char size = 0xff; char *beg, *end, str[ size ]; //char *x = "MynameisAlexI'm29yearsoldIwasbornin1983"; char *x = "1913-isbestyearofRussiain20century"; strcpy ( str, x ); str[ strlen ( x ) ] = '\0'; beg = &str;end = &str[ strlen ( x ) - 1 ]; while ( beg != end ) { if ( !is_digit( *beg ) ) { beg++; } else if ( !is_char( *end ) ) { end--; } else { swap (beg, end); beg++;end--; } } printf ( "%s\n", x ); printf ( "%s\n", str ); return 0; }
//input: *c=='[' **pc==':' static u16 bracket_class(u8 *c,u8 **pc,u8 **sc,u8 not,u8 sc_folded) { u8 char_class[CHAR_CLASS_MAX+1];//don't forget the 0 terminating char u16 r=bracket_char_class_get(c,pc,not,sc_folded,&char_class[0]); if(r!=OK) return r; if((STREQ(char_class,"alnum")&&is_alnum(**sc)) ||(STREQ(char_class,"alpha")&&is_alpha(**sc)) ||(STREQ(char_class,"blank")&&is_blank(**sc)) ||(STREQ(char_class,"cntrl")&&is_cntrl(**sc)) ||(STREQ(char_class,"digit")&&is_digit(**sc)) ||(STREQ(char_class,"graph")&&is_graph(**sc)) ||(STREQ(char_class,"lower")&&is_lower(**sc)) ||(STREQ(char_class,"print")&&is_print(**sc)) ||(STREQ(char_class,"punct")&&is_punct(**sc)) ||(STREQ(char_class,"space")&&is_space(**sc)) ||(STREQ(char_class,"upper")&&is_upper(**sc)) ||(STREQ(char_class,"xdigit")&&is_xdigit(**sc))) return bracket_matched(c,pc,not); *c=*(*pc)++; return OK; }
inline std::vector<int> available_process_files(std::string const& filename) { if (!filesystem::exists(filename)) return std::vector<int>(); std::vector<int> result; for (filesystem::directory_iterator i(filename), end; i != end; ++i) { if (!filesystem::is_regular(*i)) boost::throw_exception(std::runtime_error("directory contains non-regular entries")); std::string process_name = i->path().filename().string(); for (std::string::size_type i = 0; i < process_name.size(); ++i) if (!is_digit(process_name[i])) boost::throw_exception(std::runtime_error("directory contains files with invalid names")); result.push_back(boost::lexical_cast<int>(process_name)); } return result; }
SERD_API bool serd_uri_string_has_scheme(const uint8_t* utf8) { // RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) if (!utf8 || !is_alpha(utf8[0])) { return false; // Invalid scheme initial character, URI is relative } for (uint8_t c; (c = *++utf8) != '\0';) { switch (c) { case ':': return true; // End of scheme case '+': case '-': case '.': break; // Valid scheme character, continue default: if (!is_alpha(c) && !is_digit(c)) { return false; // Invalid scheme character } } } return false; }
/****** sge_binding_hlp/binding_striding_parse_step_size() ************************* * NAME * binding_striding_parse_step_size() -- Parses the step size out of the "striding" query. * * SYNOPSIS * int binding_striding_parse_step_size(const char* parameter) * * FUNCTION * Parses the step size for the core binding strategy "striding" out of the * query. * * The query string is expected to have following syntax: * * "striding:<amount>:<stepsize>[:<socket>,<core>]" * * INPUTS * const char* parameter - Points to the string with the query. * * RESULT * int - Returns the step size or -1 when it could not been parsed. * * NOTES * MT-NOTE: binding_striding_parse_step_size() is not MT safe * *******************************************************************************/ int binding_striding_parse_step_size(const char* parameter) { /* striding:<amount>:<step-size>: */ if (parameter != NULL && strstr(parameter, "striding") != NULL) { /* fetch "striding:" */ if (sge_strtok(parameter, ":") != NULL) { if (sge_strtok(NULL, ":") != NULL) { /* fetch step size */ char* stepsize = NULL; if ((stepsize = sge_strtok(NULL, ":")) != NULL && is_digit(stepsize, ':')) { /* the step size must be followed by " " or ":" or "\0" in order to avoid garbage like "striding:2:0,0" */ /* return step size */ return atoi(stepsize); } } } } /* in default case take each core */ return -1; }
IntegralValue::IntegralValue(std::wstring s, int width, bool sign) { assert(s.size() > 0); wchar_t base = '\0'; if(s.size() > 2 and s[0] == L'0' and !is_digit(s[1])) { base = s[1]; s.erase(0, 2); } std::string t = ""; std::transform(s.begin(), s.end(), std::back_inserter(t), digit_utf_to_ascii); unsigned char radix = 10; switch(base) { case L'\0': radix = 10; break; case L'x': radix = 16; break; case L'b': radix = 2; break; case L'o': radix = 8; break; default: assert(false && "Compilation error - unsupported radix character"); } auto srt = llvm::StringRef(t); value = llvm::APSInt(llvm::APInt(width, srt, radix), !sign); }
/** * string to double */ var_num_t sb_strtof(const char *str) { char *p = (char *) str; var_num_t r = 0.0; var_num_t d = 10.0; var_num_t sign = 1; int decp = 0; if (p == NULL) { return 0; } if (*p == '-') { sign = -1; p++; } else if (*p == '+') { p++; } while (*p) { if (is_digit(*p)) { if (!decp) { r = (r * 10) + ((*p) - 48); } else { r += (((*p) - 48) * 1 / d); d *= 10; } } else if (*p == '.') { decp = 1; } else if (*p == ' ') { break; } else { r = 0; break; } p++; } return r * sign; }
static string index_name_sub (tree t, bool all) { if (is_atomic (t)) { string s= t->label, r; int i, n= N(s); for (i=0; i<n; i++) if (is_iso_alpha (s[i]) || is_digit (s[i]) || (s[i] == ' ') || (all && (s[i] >= ' '))) r << s[i]; return r; } else if (is_concat (t)) { string r; int i, n= N(t); for (i=0; i<n; i++) r << index_name_sub (t[i], all); return r; } else if (is_tuple (t)) { string r; int i, j, n= N(t); for (i=0; i<n; i++) { if (i!=0) r << "\t"; string s= index_name_sub (t[i], false); if (s == "") s= index_name_sub (t[i], true); tree u= copy (followup [s]); for (j=0; j<N(u); j++) if (u[j] == t[i]) break; if (j == N(u)) { u << t[i]; followup (s)= u; } r << s; if (j != 0) r << "\n" << as_string (j); } return r; } else if (all && is_func (t, WITH)) return index_name_sub (t[N(t)-1], all); else return ""; }
static int varisset(const char *name, int nulok) { if (*name == '!') return backgndpidset(); else if (*name == '@' || *name == '*') { if (*shellparam.p == NULL) return 0; if (nulok) { char **av; for (av = shellparam.p; *av; av++) if (**av != '\0') return 1; return 0; } } else if (is_digit(*name)) { char *ap; long num; errno = 0; num = strtol(name, NULL, 10); if (errno != 0 || num > shellparam.nparam) return 0; if (num == 0) ap = arg0; else ap = shellparam.p[num - 1]; if (nulok && (ap == NULL || *ap == '\0')) return 0; } return 1; }
unsigned long strtol(char *ptr, char **endp, int base) { unsigned long result = 0; unsigned long value; if (base == 0) { if (ptr[0] == '0' && ptr[1] == 'x') { base = 16; ptr += 2; } else base = 10; } while (is_digit(*ptr)) { value = is_hex(*ptr) ? to_lower(*ptr) - 'a' + 10 : *ptr - '0'; result = result * base + value; ptr++; } if (endp && *endp) *endp = ptr; return result; }
unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base) { unsigned long result = 0,value; if (!base) { base = 10; if (*cp == '0') { base = 8; cp++; if ((*cp == 'x') && is_xdigit(cp[1])) { cp++; base = 16; } } } while (is_xdigit(*cp) && (value = is_digit(*cp) ? *cp-'0' : (is_lower(*cp) ? toupper(*cp) : *cp)-'A'+10) < base) { result = result*base + value; cp++; } if (endp) *endp = (char *)cp; return result; }
/* * Convert a string to an integer * returns 0 if it cannot be converted. */ int string_to_integer(char* input) { int result = 0; int length = strlen(input); int num_digits = length; int sign = 1; int i = 0; int factor = 1; if (input[0] == '-') { num_digits--; sign = -1; } for (i = 0; i < num_digits; i++, length--) { if (!is_digit(input[length-1])) { return 0; } if (i > 0) factor*=10; result += (input[length-1] - '0') * factor; } return sign * result; }
string xml_html_parser::expand_entities (string s) { string r; int i, n= N(s); for (i=0; i<n; ) { if (s[i] == '&' || s[i] == '%') { int start= i++; if (i<n && s[i] == '#') { i++; if (i<n && (s[i] == 'x' || s[i] == 'X')) { i++; while (i<n && is_hex_digit (s[i])) i++; } else while (i<n && is_digit (s[i])) i++; } else while (i<n && is_name_char (s[i])) i++; if (i<n && s[i] == ';') i++; r << expand_entity (s (start, i)); } else r << s[i++]; } if (r == s) return r; return expand_entities (r); }
int name_find_ordinal_suffix(const char* name, int* endPos) { // Walk backwards, see if this name even has an ordinal suffix. int search = *endPos - 1; if (*endPos == -1) search = (int) strlen(name) - 1; bool foundADigit = false; bool foundOrdinalSuffix = false; while (true) { if (search < 0) break; if (is_digit(name[search])) { foundADigit = true; search--; continue; } if (foundADigit && name[search] == '#') { // Found a suffix of the form #123. foundOrdinalSuffix = true; *endPos = search; break; } break; } if (!foundOrdinalSuffix) return -1; // Parse and return the ordinal number. return atoi(name + *endPos + 1); }
/* Parse floating point. */ static boolean parse_float( const char **pcur, float *val ) { const char *cur = *pcur; boolean integral_part = FALSE; boolean fractional_part = FALSE; *val = (float) atof( cur ); if (*cur == '-' || *cur == '+') cur++; if (is_digit( cur )) { cur++; integral_part = TRUE; while (is_digit( cur )) cur++; } if (*cur == '.') { cur++; if (is_digit( cur )) { cur++; fractional_part = TRUE; while (is_digit( cur )) cur++; } } if (!integral_part && !fractional_part) return FALSE; if (uprcase( *cur ) == 'E') { cur++; if (*cur == '-' || *cur == '+') cur++; if (is_digit( cur )) { cur++; while (is_digit( cur )) cur++; } else return FALSE; } *pcur = cur; return TRUE; }
// number = [ '-' ] digit+ [ '.' digit+ ] [ ['e'|'E'] ['+'|'-'] digit+ ] static int parse_number(struct frozen *f) { int ch = cur(f); TRY(capture_ptr(f, f->cur, JSON_TYPE_NUMBER)); if (ch == '-') f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; if (f->cur < f->end && f->cur[0] == '.') { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; } if (f->cur < f->end && (f->cur[0] == 'e' || f->cur[0] == 'E')) { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); if ((f->cur[0] == '+' || f->cur[0] == '-')) f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; } capture_len(f, f->num_tokens - 1, f->cur); return 0; }
/* number = [ '-' ] digit+ [ '.' digit+ ] [ ['e'|'E'] ['+'|'-'] digit+ ] */ static int parse_number(struct frozen *f) { int ch = cur(f); SET_STATE(f, f->cur, JSON_TYPE_NUMBER, "", 0); if (ch == '-') f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; if (f->cur < f->end && f->cur[0] == '.') { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; } if (f->cur < f->end && (f->cur[0] == 'e' || f->cur[0] == 'E')) { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); if ((f->cur[0] == '+' || f->cur[0] == '-')) f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; } CALL_BACK(f); return 0; }
boost::tribool request_parser::consume(request& req, char input) { switch (state_) { case method_start: if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } else { state_ = method; req.method.push_back(input); return boost::indeterminate; } case method: if (input == ' ') { state_ = uri; return boost::indeterminate; } else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } else { req.method.push_back(input); return boost::indeterminate; } case uri: if (input == ' ') { state_ = http_version_h; return boost::indeterminate; } else if (is_ctl(input)) { return false; } else { req.uri.push_back(input); return boost::indeterminate; } case http_version_h: if (input == 'H') { state_ = http_version_t_1; return boost::indeterminate; } else { return false; } case http_version_t_1: if (input == 'T') { state_ = http_version_t_2; return boost::indeterminate; } else { return false; } case http_version_t_2: if (input == 'T') { state_ = http_version_p; return boost::indeterminate; } else { return false; } case http_version_p: if (input == 'P') { state_ = http_version_slash; return boost::indeterminate; } else { return false; } case http_version_slash: if (input == '/') { req.http_version_major = 0; req.http_version_minor = 0; state_ = http_version_major_start; return boost::indeterminate; } else { return false; } case http_version_major_start: if (is_digit(input)) { req.http_version_major = req.http_version_major * 10 + input - '0'; state_ = http_version_major; return boost::indeterminate; } else { return false; } case http_version_major: if (input == '.') { state_ = http_version_minor_start; return boost::indeterminate; } else if (is_digit(input)) { req.http_version_major = req.http_version_major * 10 + input - '0'; return boost::indeterminate; } else { return false; } case http_version_minor_start: if (is_digit(input)) { req.http_version_minor = req.http_version_minor * 10 + input - '0'; state_ = http_version_minor; return boost::indeterminate; } else { return false; } case http_version_minor: if (input == '\r') { state_ = expecting_newline_1; return boost::indeterminate; } else if (is_digit(input)) { req.http_version_minor = req.http_version_minor * 10 + input - '0'; return boost::indeterminate; } else { return false; } case expecting_newline_1: if (input == '\n') { state_ = header_line_start; return boost::indeterminate; } else { return false; } case header_line_start: if (input == '\r') { state_ = expecting_newline_3; return boost::indeterminate; } else if (!req.headers.empty() && (input == ' ' || input == '\t')) { state_ = header_lws; return boost::indeterminate; } else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } else { req.headers.push_back(header()); req.headers.back().name.push_back(input); state_ = header_name; return boost::indeterminate; } case header_lws: if (input == '\r') { state_ = expecting_newline_2; return boost::indeterminate; } else if (input == ' ' || input == '\t') { return boost::indeterminate; } else if (is_ctl(input)) { return false; } else { state_ = header_value; req.headers.back().value.push_back(input); return boost::indeterminate; } case header_name: if (input == ':') { state_ = space_before_header_value; return boost::indeterminate; } else if (!is_char(input) || is_ctl(input) || is_tspecial(input)) { return false; } else { req.headers.back().name.push_back(input); return boost::indeterminate; } case space_before_header_value: if (input == ' ') { state_ = header_value; return boost::indeterminate; } else { return false; } case header_value: if (input == '\r') { state_ = expecting_newline_2; return boost::indeterminate; } else if (is_ctl(input)) { return false; } else { req.headers.back().value.push_back(input); return boost::indeterminate; } case expecting_newline_2: if (input == '\n') { state_ = header_line_start; return boost::indeterminate; } else { return false; } case expecting_newline_3: return (input == '\n'); default: return false; } }
// returns protocol, auth, hostname, port, path std::tuple<std::string, std::string, std::string, int, std::string> parse_url_components(std::string url, error_code& ec) { std::string hostname; // hostname only std::string auth; // user:pass std::string protocol; // http or https for instance int port = -1; std::string::iterator at; std::string::iterator colon; std::string::iterator port_pos; // PARSE URL auto start = url.begin(); // remove white spaces in front of the url while (start != url.end() && is_space(*start)) ++start; auto end = std::find(url.begin(), url.end(), ':'); protocol.assign(start, end); if (end == url.end()) { ec = errors::unsupported_url_protocol; goto exit; } ++end; if (end == url.end() || *end != '/') { ec = errors::unsupported_url_protocol; goto exit; } ++end; if (end == url.end() || *end != '/') { ec = errors::unsupported_url_protocol; goto exit; } ++end; start = end; at = std::find(start, url.end(), '@'); colon = std::find(start, url.end(), ':'); end = std::find(start, url.end(), '/'); if (at != url.end() && colon != url.end() && colon < at && at < end) { auth.assign(start, at); start = at; ++start; } // this is for IPv6 addresses if (start != url.end() && *start == '[') { port_pos = std::find(start, url.end(), ']'); if (port_pos == url.end()) { ec = errors::expected_close_bracket_in_address; goto exit; } // strip the brackets hostname.assign(start + 1, port_pos); port_pos = std::find(port_pos, url.end(), ':'); } else { port_pos = std::find(start, url.end(), ':'); if (port_pos < end) hostname.assign(start, port_pos); else hostname.assign(start, end); } if (port_pos < end) { ++port_pos; for (auto i = port_pos; i < end; ++i) { if (is_digit(*i)) continue; ec = errors::invalid_port; goto exit; } port = std::atoi(std::string(port_pos, end).c_str()); } start = end; exit: return std::make_tuple(std::move(protocol) , std::move(auth) , std::move(hostname) , port , std::string(start, url.end())); }
static void varvalue(const char *name, int quoted, int subtype, int flag) { int num; char *p; int i; char sep[2]; char **ap; switch (*name) { case '$': num = rootpid; break; case '?': num = oexitstatus; break; case '#': num = shellparam.nparam; break; case '!': num = backgndpidval(); break; case '-': for (i = 0 ; i < NOPTS ; i++) { if (optlist[i].val) STPUTC(optlist[i].letter, expdest); } return; case '@': if (flag & EXP_FULL && quoted) { for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { strtodest(p, flag, subtype, quoted); if (*ap) STPUTC('\0', expdest); } return; } /* FALLTHROUGH */ case '*': if (ifsset()) sep[0] = ifsval()[0]; else sep[0] = ' '; sep[1] = '\0'; for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { strtodest(p, flag, subtype, quoted); if (!*ap) break; if (sep[0]) strtodest(sep, flag, subtype, quoted); else if (flag & EXP_FULL && !quoted && **ap != '\0') STPUTC('\0', expdest); } return; default: if (is_digit(*name)) { num = atoi(name); if (num == 0) p = arg0; else if (num > 0 && num <= shellparam.nparam) p = shellparam.p[num - 1]; else return; strtodest(p, flag, subtype, quoted); } return; } expdest = cvtnum(num, expdest); }
size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param) { if (!buffer || !maxlen) { return 0; } int arg; int args = params[0]; char *buf_p; char ch; int flags; int width; int prec; int n; char sign; const char *fmt; size_t llen = maxlen - 1; buf_p = buffer; arg = *param; fmt = format; while (true) { // run through the format string until we hit a '%' or '\0' for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++) { *buf_p++ = ch; llen--; } if ((ch == '\0') || (llen <= 0)) { goto done; } // skip over the '%' fmt++; // reset formatting state flags = 0; width = 0; prec = -1; sign = '\0'; rflag: ch = *fmt++; reswitch: switch(ch) { case '-': { flags |= LADJUST; goto rflag; } case '.': { n = 0; while(is_digit((ch = *fmt++))) { n = 10 * n + (ch - '0'); } prec = (n < 0) ? -1 : n; goto reswitch; } case '0': { flags |= ZEROPAD; goto rflag; } case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { n = 0; do { n = 10 * n + (ch - '0'); ch = *fmt++; } while(is_digit(ch)); width = n; goto reswitch; } case 'c': { CHECK_ARGS(0); if (!llen) { goto done; } char *c; pCtx->LocalToString(params[arg], &c); *buf_p++ = *c; llen--; arg++; break; } case 'b': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddBinary(&buf_p, llen, *value, width, flags); arg++; break; } case 'd': case 'i': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddInt(&buf_p, llen, static_cast<int>(*value), width, flags); arg++; break; } case 'u': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddUInt(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case 'f': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddFloat(&buf_p, llen, sp_ctof(*value), width, prec, flags); arg++; break; } case 'L': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); char buffer[255]; if (*value) { CPlayer *player = g_Players.GetPlayerByIndex(*value); if (!player || !player->IsConnected()) { return pCtx->ThrowNativeError("Client index %d is invalid", *value); } const char *auth = player->GetAuthString(); if (!auth || auth[0] == '\0') { auth = "STEAM_ID_PENDING"; } int userid = engine->GetPlayerUserId(player->GetEdict()); UTIL_Format(buffer, sizeof(buffer), "%s<%d><%s><>", player->GetName(), userid, auth); } else { UTIL_Format(buffer, sizeof(buffer), "Console<0><Console><Console>"); } AddString(&buf_p, llen, buffer, width, prec); arg++; break; } case 'N': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); const char *name = "Console"; if (*value) { CPlayer *player = g_Players.GetPlayerByIndex(*value); if (!player || !player->IsConnected()) { return pCtx->ThrowNativeError("Client index %d is invalid", *value); } name = player->GetName(); } AddString(&buf_p, llen, name, width, prec); arg++; break; } case 's': { CHECK_ARGS(0); char *str; int err; if ((err=pCtx->LocalToString(params[arg], &str)) != SP_ERROR_NONE) { pCtx->ThrowNativeErrorEx(err, "Could not deference string"); return 0; } AddString(&buf_p, llen, str, width, prec); arg++; break; } case 'T': { CHECK_ARGS(1); char *key; bool error; size_t res; cell_t *target; pCtx->LocalToString(params[arg++], &key); pCtx->LocalToPhysAddr(params[arg++], &target); res = Translate(buf_p, llen, pCtx, key, *target, params, &arg, &error); if (error) { return 0; } buf_p += res; llen -= res; break; } case 't': { CHECK_ARGS(0); char *key; bool error; size_t res; cell_t target = g_SourceMod.GetGlobalTarget(); pCtx->LocalToString(params[arg++], &key); res = Translate(buf_p, llen, pCtx, key, target, params, &arg, &error); if (error) { return 0; } buf_p += res; llen -= res; break; } case 'X': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); flags |= UPPERDIGITS; AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case 'x': { CHECK_ARGS(0); cell_t *value; pCtx->LocalToPhysAddr(params[arg], &value); AddHex(&buf_p, llen, static_cast<unsigned int>(*value), width, flags); arg++; break; } case '%': { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } case '\0': { if (!llen) { goto done; } *buf_p++ = '%'; llen--; goto done; } default: { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } } } done: *buf_p = '\0'; *param = arg; return (maxlen - llen - 1); }
size_t gnprintf(char *buffer, size_t maxlen, const char *format, void **args) { if (!buffer || !maxlen) { return 0; } int arg = 0; char *buf_p; char ch; int flags; int width; int prec; int n; char sign; const char *fmt; size_t llen = maxlen - 1; buf_p = buffer; fmt = format; while (true) { // run through the format string until we hit a '%' or '\0' for (ch = *fmt; llen && ((ch = *fmt) != '\0') && (ch != '%'); fmt++) { *buf_p++ = ch; llen--; } if ((ch == '\0') || (llen <= 0)) { goto done; } // skip over the '%' fmt++; // reset formatting state flags = 0; width = 0; prec = -1; sign = '\0'; rflag: ch = *fmt++; reswitch: switch(ch) { case '-': { flags |= LADJUST; goto rflag; } case '.': { n = 0; while(is_digit((ch = *fmt++))) { n = 10 * n + (ch - '0'); } prec = (n < 0) ? -1 : n; goto reswitch; } case '0': { flags |= ZEROPAD; goto rflag; } case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { n = 0; do { n = 10 * n + (ch - '0'); ch = *fmt++; } while(is_digit(ch)); width = n; goto reswitch; } case 'c': { if (!llen) { goto done; } char *c = (char *)args[arg]; *buf_p++ = *c; llen--; arg++; break; } case 'b': { int *value = (int *)args[arg]; AddBinary(&buf_p, llen, *value, width, flags); arg++; break; } case 'd': case 'i': { int *value = (int *)args[arg]; AddInt(&buf_p, llen, *value, width, flags); arg++; break; } case 'u': { unsigned int *value = (unsigned int *)args[arg]; AddUInt(&buf_p, llen, *value, width, flags); arg++; break; } case 'f': { float *value = (float *)args[arg]; AddFloat(&buf_p, llen, *value, width, prec, flags); arg++; break; } case 's': { const char *str = (const char *)args[arg]; AddString(&buf_p, llen, str, width, prec); arg++; break; } case 'X': { unsigned int *value = (unsigned int *)args[arg]; flags |= UPPERDIGITS; AddHex(&buf_p, llen, *value, width, flags); arg++; break; } case 'x': { unsigned int *value = (unsigned int *)args[arg]; AddHex(&buf_p, llen, *value, width, flags); arg++; break; } case '%': { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } case '\0': { if (!llen) { goto done; } *buf_p++ = '%'; llen--; goto done; } default: { if (!llen) { goto done; } *buf_p++ = ch; llen--; break; } } } done: *buf_p = '\0'; return (maxlen - llen - 1); }