std::string const XmlTokenizer::next(Token &t) { static tokenDef_s tokens_def[] = { {NULL, &XmlTokenizer::token_mark_start, Token::MARK_START}, {">", &XmlTokenizer::token_str, Token::MARK_END}, {"/", &XmlTokenizer::token_str, Token::MARK_CLOSE}, {"_-:.", &XmlTokenizer::token_chr, Token::NAME}, {"=", &XmlTokenizer::token_str, Token::EQUAL}, {"\"", &XmlTokenizer::token_literal_str, Token::STRING}, {NULL, &XmlTokenizer::token_eof, Token::END_OF_FILE}, }; parse_spaces(); for (auto const &def : tokens_def) { _oss.str(""); if ((this->*def.f)(def.str)) { t = def.token; parse_spaces(); return (_oss.str()); } } throw std::domain_error(ft::f("Unknown token '\033[91m%\033[0m' at " "\033[97mline %\033[0m", (char)_is.peek(), _line)); }
/** * Parses token of non-whitespaces. Closes it with \0. * TODO poprawic jakosc implementacji */ static int parse_token(FILE * file, char * out_token, const int limit) { parse_spaces(file); int num = 0; int c; while (EOF != (c = fgetc(file))) { if (isspace(c)) { ungetc(c, file); out_token[num] = '\0'; return 0; } if (num >= limit - 1) { ungetc(c, file); out_token[num] = '\0'; return -1; } out_token[num++] = c; } out_token[num] = '\0'; return 0; }
int parse_expect( char c ) { parse_spaces(); if (*iparser==c) { iparser++; return 1; } else return 0; }
int parse_name(char *str) { int len; char *end; parse_spaces(); end=iparser; while ( (*end!=0)&&(*end!=' ')&&(*end!='\t')&&(*end!=13)&&(*end!=10)&&(*end!=']')&&(*end!=':') ) end++; if ( (len=end-iparser)>0 ) { if (len>=255) len=255; // check for length memcpy(str, iparser, len); iparser = end; } str[len] = 0; return len; }
int parse_value(char *str, char *delimiters) { int len; char *end; parse_spaces(); end=iparser; while ( !charpos(*end,delimiters) && (*end!=0) ) end++; if ( (len=end-iparser)>0 ) { if (len>=255) len=255; // check for length memcpy(str, iparser, len); iparser = end; } str[len] = 0; return len; }
int parse_bin(char *str) { int len; char *end; parse_spaces(); end=iparser; while ( (*end=='0')||(*end=='1') ) end++; if ( (len=end-iparser)>0 ) { if (len>=255) len=255; // check for length memcpy(str, iparser, len); iparser = end; } str[len] = 0; return len; }
int parse_hex(char *str) { int len; char *end; parse_spaces(); end=iparser; while ( ((*end>='0')&&(*end<='9'))||((*end>='A')&&(*end<='F'))||((*end>='a')&&(*end<='f')) ) end++; if ( (len=end-iparser)>0 ) { if (len>=255) len=255; // check for length memcpy(str, iparser, len); iparser = end; } str[len] = 0; return len; }
bool XmlTokenizer::token_mark_start(char const *str) { char c; c = _is.peek(); if (c == '<') { _is.get(); _oss.put(c); c = _is.peek(); if (c != '!') return (true); _is.get(); if (token_str_hard("--") >= 0) throw std::domain_error(ft::f("Expected '<!\033[91m--\033[0m'" " at \033[97mline %\033[0m", _line)); while (token_str_hard("-->") >= 0) continue ; parse_spaces(); return (token_mark_start(NULL)); } return (false); (void)str; }
/* Read the system's package database and extract information about * 'pkgname'. Return 0 in case of success, or -1 in case of error. * * If the package is unknown, return -1 and set errno to ENOENT * If the package database is corrupted, return -1 and set errno to EINVAL */ int get_package_info(const char* pkgName, PackageInfo *info) { char* buffer; size_t buffer_len; const char* p; const char* buffer_end; int result = -1; info->uid = 0; info->isDebuggable = 0; info->dataDir[0] = '\0'; info->seinfo[0] = '\0'; buffer = map_file(PACKAGES_LIST_FILE, &buffer_len); if (buffer == NULL) return -1; p = buffer; buffer_end = buffer + buffer_len; /* expect the following format on each line of the control file: * * <pkgName> <uid> <debugFlag> <dataDir> * * where: * <pkgName> is the package's name * <uid> is the application-specific user Id (decimal) * <debugFlag> is 1 if the package is debuggable, or 0 otherwise * <dataDir> is the path to the package's data directory (e.g. /data/data/com.example.foo) * <seinfo> is the seinfo label associated with the package * * The file is generated in com.android.server.PackageManagerService.Settings.writeLP() */ while (p < buffer_end) { /* find end of current line and start of next one */ const char* end = find_first(p, buffer_end, '\n'); const char* next = (end < buffer_end) ? end + 1 : buffer_end; const char* q; int uid, debugFlag; /* first field is the package name */ p = compare_name(p, end, pkgName); if (p == NULL) goto NEXT_LINE; /* skip spaces */ if (parse_spaces(&p, end) < 0) goto BAD_FORMAT; /* second field is the pid */ uid = parse_positive_decimal(&p, end); if (uid < 0) return -1; info->uid = (uid_t) uid; /* skip spaces */ if (parse_spaces(&p, end) < 0) goto BAD_FORMAT; /* third field is debug flag (0 or 1) */ debugFlag = parse_positive_decimal(&p, end); switch (debugFlag) { case 0: info->isDebuggable = 0; break; case 1: info->isDebuggable = 1; break; default: goto BAD_FORMAT; } /* skip spaces */ if (parse_spaces(&p, end) < 0) goto BAD_FORMAT; /* fourth field is data directory path and must not contain * spaces. */ q = skip_non_spaces(p, end); if (q == p) goto BAD_FORMAT; p = string_copy(info->dataDir, sizeof info->dataDir, p, q - p); /* skip spaces */ if (parse_spaces(&p, end) < 0) goto BAD_FORMAT; /* grab the seinfo string */ q = skip_non_spaces(p, end); if (q == p) goto BAD_FORMAT; string_copy(info->seinfo, sizeof info->seinfo, p, q - p); /* Ignore the rest */ result = 0; goto EXIT; NEXT_LINE: p = next; } /* the package is unknown */ errno = ENOENT; result = -1; goto EXIT; BAD_FORMAT: errno = EINVAL; result = -1; EXIT: unmap_file(buffer, buffer_len); return result; }