char *next_code_line(char *ptr) { ptr = next_expr(ptr, "\\;\r\n"); if (*ptr == ';') return skip_to_next_line(ptr); if (*ptr == '\\') ptr++; if (*ptr == '\r') ptr++; if (*ptr == '\n') ptr++; return ptr; }
struct tml_token tml_stream_pop(struct tml_stream *stream) { struct tml_token token; token.value = NULL; token.value_size = 0; for (;;) { int ch = peek_char(stream); if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { next_char(stream); continue; } token.offset = stream->index; if (ch == TML_OPEN_CHAR) { next_char(stream); token.type = TML_TOKEN_OPEN; return token; } else if (ch == TML_CLOSE_CHAR) { next_char(stream); token.type = TML_TOKEN_CLOSE; return token; } else if (ch == TML_DIVIDER_CHAR) { next_char(stream); if (peek_char(stream) == TML_DIVIDER_CHAR) { skip_to_next_line(stream); continue; } else { token.type = TML_TOKEN_DIVIDER; return token; } } else if (ch == -1) { token.type = TML_TOKEN_EOF; return token; } else { parse_word_item(stream, &token); return token; } } }
static std::size_t one_time_init_major_version() { std::size_t version{0}; char const * cpu_info_path{"/proc/cpuinfo"}; char const board_version_label[]{"Revision"}; char const * eof_label{"###"}; std::size_t const board_version_size{sizeof(board_version_label)}; bool not_done{true}; char buffer[board_version_size]; std::size_t const board_version_length{board_version_size-1}; char const * label{nullptr}; FILE * info_file{fopen(cpu_info_path, "r")}; do { label = get_label(info_file, buffer, board_version_length, eof_label); not_done = std::strcmp(label, eof_label)!=0; if (not_done) { not_done = std::strcmp(label, board_version_label)!=0; if (not_done) { skip_to_next_line( info_file ); } else { version = extract_version( info_file ); } } } while (not_done); if ( info_file ) { fclose( info_file ); } if (version>0&&version<=3) return 1; else if (version>3&&version<=0xf) return 2; else if (version==0x10 || version==0x12) return 3; // 0x10:B+, 0x12:A+ else if (version==0x11) return 4; // Compute module else { throw std::runtime_error( "rpi_init::init_major_version: Unable to " "deduce board version from /proc/cpuinfo." ); } }
static LRC_RESULT lrc_parser_parse(LrcParser* thiz) { LRC_RESULT ret = LRC_RESULT_OK; const char* p = thiz->p; if(thiz->builder->on_begin != NULL) { thiz->builder->on_begin(thiz->builder, p); } while(*p != '\0') { if(*p == '[') { if(*(p+4) == '-')//Skip the line like this: [00:-3.00]XXX... { skip_to_next_line(thiz->p); } else { thiz->p = ++p; skip_space(thiz->p); lrc_parser_parse_tag(thiz); } } else if(*p == '\n' || *p == '\r') { int i = 1; while((*(p-i) == ' ' || *(p-i) == '\t') && *(p-i) != '\0')++i; if(*(p-i) == ']') //for null lrc but only have time tag line #4218 { thiz->p = p; lrc_parser_parse_lrc(thiz); } skip_to_next_line(thiz->p); } else if(*p == ' ' || *p == '\t') { skip_space(thiz->p); } else if(*p != ']') { thiz->p = p; lrc_parser_parse_lrc(thiz); } else { ++thiz->p; } p = thiz->p; } if(thiz->builder->on_end != NULL) { thiz->builder->on_end(thiz->builder); } return ret; }
/* * Parse a file */ int CTadsGameInfo::parse_file(osfildef *fp, unsigned long res_seek_pos, unsigned long res_size) { /* find the tail of the existing list */ tads_valinfo *last_val; for (last_val = first_val_ ; last_val != 0 && last_val->nxt != 0 ; last_val = last_val->nxt) ; /* we found the resource - seek to it in the file */ if (osfseek(fp, res_seek_pos, OSFSK_SET)) return FALSE; /* * Allocate a block of memory for loading the game information. The * game information resource is typically fairly small, so for * simplicity we'll just allocate a single block of memory and load * the whole resource into the block. Allocate space for one extra * byte, so that we can ensure we have a newline at the end of the * buffer. */ buf_ = (char *)osmalloc(res_size + 1); if (buf_ == 0) return FALSE; /* read the data */ if (osfrb(fp, buf_, res_size)) return FALSE; /* * store an extra newline at the end of the buffer, so that we can be * certain that the last line ends in a newline - at worst, this will * add an extra blank line to the end, but since we ignore blank lines * this will do no harm */ buf_[res_size++] = '\n'; /* parse the data */ utf8_ptr p(buf_); for (size_t rem = res_size ; rem != 0 ; ) { /* skip any leading whitespace */ while (rem != 0 && is_space(p.getch())) p.inc(&rem); /* if the line starts with '#', it's a comment, so skip it */ if (rem != 0 && p.getch() == '#') { /* skip the entire line, and go back for the next one */ skip_to_next_line(&p, &rem); continue; } /* we must have the start of a name - note it */ utf8_ptr name_start = p; /* skip ahead to a space or colon */ while (rem != 0 && p.getch() != ':' && !is_hspace(p.getch())) p.inc(&rem); /* note the length of the name */ size_t name_len = p.getptr() - name_start.getptr(); /* skip any whitespace before the presumed colon */ while (rem != 0 && is_hspace(p.getch())) p.inc(&rem); /* if we're not at a colon, the line is ill-formed, so skip it */ if (rem == 0 || p.getch() != ':') { /* skip the entire line, and go back for the next one */ skip_to_next_line(&p, &rem); continue; } /* skip the colon and any whitespace immediately after it */ for (p.inc(&rem) ; rem != 0 && is_hspace(p.getch()) ; p.inc(&rem)) ; /* * Whatever terminated the name, replace it with a null character * - this is safe, since we at least have a colon character to * replace, and we've already skipped it so we can overwrite it * now. A null character in utf-8 is simply a single zero byte, * so we can store our byte directly without worrying about any * utf-8 multi-byte strangeness. */ *(name_start.getptr() + name_len) = '\0'; /* note where the value starts */ utf8_ptr val_start = p; /* set up to write to the buffer at the current point */ utf8_ptr dst = p; /* * Now find the end of the value. The value can span multiple * lines; if we find a newline followed by a space, it means that * the next line is a continuation of the current value, and that * the entire sequence of newlines and immediately following * whitespace should be converted to a single space in the value. * * Note that we copy the transformed value directly over the old * version of the value in the buffer. This is safe because the * transformation can only remove characters - we merely collapse * each newline-whitespace sequence into a single space. */ while (rem != 0) { /* get this character */ wchar_t ch = p.getch(); /* check for a newline */ if (is_vspace(ch)) { /* * it's a newline - skip it (and any other characters in * the newline sequence) */ skip_newline(&p, &rem); /* * if there's no leading whitespace on the next line, * we've reached the end of this value */ if (rem == 0 || !is_hspace(p.getch())) { /* * no whitespace -> end of the value - stop scanning * the value */ break; } /* skip leading whitespace on the line */ while (rem != 0 && is_hspace(p.getch())) p.inc(&rem); /* * add a single whitespace character to the output for the * entire sequence of the newline plus the leading * whitespace on the line */ dst.setch(' '); } else if (ch == 0) { /* change null bytes to spaces */ dst.setch(' '); /* skip this character */ p.inc(&rem); } else { /* it's not a newline - simply copy it out and keep going */ dst.setch(ch); /* skip this character */ p.inc(&rem); } } /* * Store a null terminator at the end of the value (it's safe to * write this to the buffer because at worst it'll overwrite the * newline at the end of the last line, which we've already * skipped in the source). Note that we obtain the length of the * value string before storing the null terminator, because we * don't want to count the null byte in the value's length. */ dst.setch('\0'); /* * Create a new value list entry. Point the entry directly into * our buffer, since we're keeping the buffer around as long as * the value list is around. */ tads_valinfo *val_info = (tads_valinfo *)osmalloc(sizeof(tads_valinfo)); val_info->name = name_start.getptr(); val_info->name_len = name_len; val_info->val = store_value(val_start.getptr(), dst.getptr() - val_start.getptr()); val_info->nxt = 0; /* link the new value at the end of our list */ if (last_val != 0) last_val->nxt = val_info; else first_val_ = val_info; last_val = val_info; } /* success */ return TRUE; }
/* reads an entire attribute from the input buffer, leaving p pointing at the start of the next line (past the \r\n) */ static EVCardAttribute* read_attribute (char **p) { char *attr_group = NULL; char *attr_name = NULL; EVCardAttribute *attr = NULL; GString *str; char *lp; gboolean is_qp = FALSE; /* first read in the group/name */ str = g_string_new (""); for( lp = skip_newline( *p, is_qp ); *lp != '\n' && *lp != '\r' && *lp != '\0'; lp = skip_newline( lp, is_qp ) ) { if (*lp == ':' || *lp == ';') { if (str->len != 0) { /* we've got a name, break out to the value/attribute parsing */ attr_name = g_string_free (str, FALSE); break; } else { /* a line of the form: * (group.)?[:;] * * since we don't have an attribute * name, skip to the end of the line * and try again. */ g_string_free (str, TRUE); *p = lp; skip_to_next_line(p); goto lose; } } else if (*lp == '.') { if (attr_group) { g_warning ("extra `.' in attribute specification. ignoring extra group `%s'", str->str); g_string_free (str, TRUE); str = g_string_new (""); } if (str->len != 0) { attr_group = g_string_free (str, FALSE); str = g_string_new (""); } } else if (g_unichar_isalnum (g_utf8_get_char (lp)) || *lp == '-' || *lp == '_') { g_string_append_unichar (str, g_utf8_get_char (lp)); } else { g_warning ("invalid character found in attribute group/name"); g_string_free (str, TRUE); *p = lp; skip_to_next_line(p); goto lose; } lp = g_utf8_next_char(lp); } if (!attr_name) { skip_to_next_line (p); goto lose; } attr = e_vcard_attribute_new (attr_group, attr_name); g_free (attr_group); g_free (attr_name); if (*lp == ';') { /* skip past the ';' */ lp = g_utf8_next_char(lp); read_attribute_params (attr, &lp, &is_qp); if (is_qp) attr->encoding = EVC_ENCODING_RAW; } if (*lp == ':') { /* skip past the ':' */ lp = g_utf8_next_char(lp); read_attribute_value (attr, &lp, is_qp); } *p = lp; if (!attr->values) goto lose; return attr; lose: if (attr) e_vcard_attribute_free (attr); return NULL; }
static void read_attribute_params (EVCardAttribute *attr, char **p, gboolean *quoted_printable) { char *lp; GString *str; EVCardAttributeParam *param = NULL; gboolean in_quote = FALSE; str = g_string_new (""); for( lp = skip_newline( *p, *quoted_printable ); *lp != '\n' && *lp != '\r' && *lp != '\0'; lp = skip_newline( lp, *quoted_printable ) ) { if (*lp == '"') { in_quote = !in_quote; lp = g_utf8_next_char (lp); } else if (in_quote || g_unichar_isalnum (g_utf8_get_char (lp)) || *lp == '-' || *lp == '_') { g_string_append_unichar (str, g_utf8_get_char (lp)); lp = g_utf8_next_char (lp); } /* accumulate until we hit the '=' or ';'. If we hit * a '=' the string contains the parameter name. if * we hit a ';' the string contains the parameter * value and the name is either ENCODING (if value == * QUOTED-PRINTABLE) or TYPE (in any other case.) */ else if (*lp == '=') { if (str->len > 0) { param = e_vcard_attribute_param_new (str->str); g_string_assign (str, ""); lp = g_utf8_next_char (lp); } else { skip_until (&lp, ":;"); if (*lp == ';') { lp = g_utf8_next_char (lp); } else if (*lp == ':') { /* do nothing */ } else { skip_to_next_line( &lp ); break; } } } else if (*lp == ';' || *lp == ':' || *lp == ',') { gboolean colon = (*lp == ':'); gboolean comma = (*lp == ','); if (param) { if (str->len > 0) { e_vcard_attribute_param_add_value (param, str->str); g_string_assign (str, ""); if (!colon) lp = g_utf8_next_char (lp); } else { /* we've got a parameter of the form: * PARAM=(.*,)?[:;] * so what we do depends on if there are already values * for the parameter. If there are, we just finish * this parameter and skip past the offending character * (unless it's the ':'). If there aren't values, we free * the parameter then skip past the character. */ if (!param->values) { e_vcard_attribute_param_free (param); param = NULL; if (!colon) lp = g_utf8_next_char (lp); } } if (param && !g_ascii_strcasecmp (param->name, "encoding") && !g_ascii_strcasecmp (param->values->data, "quoted-printable")) { *quoted_printable = TRUE; e_vcard_attribute_param_free (param); param = NULL; } } else { if (str->len > 0) { char *param_name; if (!g_ascii_strcasecmp (str->str, "quoted-printable")) { param_name = "ENCODING"; *quoted_printable = TRUE; } /* apple's broken addressbook app outputs naked BASE64 parameters, which aren't even vcard 3.0 compliant. */ else if (!g_ascii_strcasecmp (str->str, "base64")) { param_name = "ENCODING"; g_string_assign (str, "b"); } else { param_name = "TYPE"; } if (param_name) { param = e_vcard_attribute_param_new (param_name); e_vcard_attribute_param_add_value (param, str->str); } g_string_assign (str, ""); if (!colon) lp = g_utf8_next_char (lp); } else { /* we've got an attribute with a truly empty attribute parameter. So it's of the form: ATTR;[PARAM=value;]*;[PARAM=value;]*: (note the extra ';') the only thing to do here is, well.. nothing. we skip over the character if it's not a colon, and the rest is handled for us: We'll either continue through the loop again if we hit a ';', or we'll break out correct below if it was a ':' */ if (!colon) lp = g_utf8_next_char (lp); } } if (param && !comma) { e_vcard_attribute_add_param (attr, param); param = NULL; } if (colon) break; } else { g_warning ("invalid character found in parameter spec"); g_string_assign (str, ""); /* skip_until (&lp, ":;"); */ skip_to_next_line( &lp ); } } if (str) g_string_free (str, TRUE); *p = lp; }
static void read_attribute_value (EVCardAttribute *attr, char **p, gboolean quoted_printable) { char *lp = *p; GString *str; /* read in the value */ str = g_string_new (""); for( lp = skip_newline( *p, quoted_printable ); *lp != '\n' && *lp != '\r' && *lp != '\0'; lp = skip_newline( lp, quoted_printable ) ) { if (*lp == '=' && quoted_printable) { char a, b; if ((a = *(++lp)) == '\0') break; if ((b = *(++lp)) == '\0') break; if (isxdigit(a) && isxdigit (b)) { char c; a = tolower (a); b = tolower (b); c = (((a>='a'?a-'a'+10:a-'0')&0x0f) << 4) | ((b>='a'?b-'a'+10:b-'0')&0x0f); g_string_append_c (str, c); /* add decoded byte (this is not a unicode yet) */ } else { g_string_append_c (str, a); g_string_append_c (str, b); } lp++; } else if (*lp == '\\') { /* convert back to the non-escaped version of the characters */ lp = g_utf8_next_char(lp); if (*lp == '\0') { g_string_append_c (str, '\\'); break; } /* beware, there might be a line break due to folding, * need next real character */ lp = skip_newline (lp, quoted_printable); switch (*lp) { case 'n': g_string_append_c (str, '\n'); break; case 'N': g_string_append_c (str, '\n'); break; case 'r': g_string_append_c (str, '\r'); break; case 'R': g_string_append_c (str, '\r'); break; case ';': g_string_append_c (str, ';'); break; case ',': g_string_append_c (str, ','); break; case '\\': g_string_append_c (str, '\\'); break; default: g_warning ("invalid escape, passing it through"); g_string_append_c (str, '\\'); g_string_append_unichar (str, g_utf8_get_char(lp)); break; } lp = g_utf8_next_char(lp); } else if ((*lp == ';') || (*lp == ',' && !g_ascii_strcasecmp (attr->name, "CATEGORIES"))) { e_vcard_attribute_add_value (attr, str->str); g_string_assign (str, ""); lp = g_utf8_next_char(lp); } else { g_string_append_unichar (str, g_utf8_get_char (lp)); lp = g_utf8_next_char(lp); } } if (str) { e_vcard_attribute_add_value (attr, str->str); g_string_free (str, TRUE); } skip_to_next_line( &lp ); *p = lp; }