BOOL vmsVideoSiteHtmlCodeParser::Parse_Sharkle(LPCSTR pszHtml) { LPCSTR psz = strstr (pszHtml, "splayer.swf?"); if (psz == NULL) return FALSE; psz = strstr (psz, "rnd="); if (psz == NULL) return FALSE; fsString strRnd; psz += 4; while (*psz && *psz != '&') strRnd += *psz++; if (strRnd.IsEmpty ()) return FALSE; fsString strUrl; strUrl = "http://sharkle.com/inc/misc/about.php?rnd="; strUrl += strRnd; strUrl += "&ssd=ZeleninGalaburda"; fsString strTitle; psz = strstr (pszHtml, "blog_header"); if (psz != NULL) { while (*psz && *psz != '>') psz++; if (*psz != 0) { psz++; while (*psz == ' ' || is_valid_char (*psz) == FALSE) psz++; while (*psz && *psz != '<') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } while (strTitle.GetLength () && strTitle [strTitle.GetLength () - 1] == ' ') strTitle [strTitle.GetLength () - 1] = 0; } } fsDecodeHtmlText (strTitle); m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "flv"; m_bDirectLink = TRUE; return TRUE; }
BOOL vmsVideoSiteHtmlCodeParser::Parse_DailyMotion(LPCSTR pszHtml) { LPCSTR psz = strstr (pszHtml, "flashvars="); if (psz == NULL) psz = strstr (pszHtml, "flashVars="); if (psz == NULL) return FALSE; _lSearchUrl: psz = strstr (psz, "url="); if (psz == NULL) return FALSE; psz += 4; if (strncmp (psz, "rev=", 4) == 0) goto _lSearchUrl; fsString strUrl; while (*psz && *psz != '&') strUrl += *psz++; fsDecodeHtmlUrl (strUrl); fsString strTitle; psz = strstr (pszHtml, "<h1"); if (psz == NULL) psz = strstr (pszHtml, "<H1"); if (psz != NULL) { while (*psz && *psz != '>') psz++; if (*psz != 0) { psz++; while (*psz == ' ' || is_valid_char (*psz) == FALSE) psz++; while (*psz && *psz != '<') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } fsDecodeHtmlText (strTitle); while (strTitle.GetLength () && strTitle [strTitle.GetLength () - 1] == ' ') strTitle [strTitle.GetLength () - 1] = 0; } } m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "flv"; m_bDirectLink = TRUE; return TRUE; }
BOOL vmsVideoSiteHtmlCodeParser::Parse_Blennus(LPCSTR pszHtml) { LPCSTR psz = strstr (pszHtml, "embed"); if (psz == NULL) psz = strstr (pszHtml, "EMBED"); if (psz == NULL) return FALSE; psz = strstr (psz, "src="); if (psz == NULL) return FALSE; psz += 4; if (*psz++ != '"') return FALSE; fsString strUrl; while (*psz && *psz != '"') strUrl += *psz++; if (strUrl.IsEmpty ()) return FALSE; fsString strTitle; psz = strstr (pszHtml, "contentheading"); if (psz != NULL) { while (*psz && *psz != '>') psz++; if (*psz != 0) { psz++; while (*psz == ' ' || is_valid_char (*psz) == FALSE) psz++; while (*psz && *psz != '<') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } fsDecodeHtmlText (strTitle); while (strTitle.GetLength () && strTitle [strTitle.GetLength () - 1] == ' ') strTitle [strTitle.GetLength () - 1] = 0; } } fsDecodeHtmlText (strTitle); m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "wmv"; m_bDirectLink = TRUE; return TRUE; }
/* TODO: replace this with a space-aware, self-WRAPing function. */ layer idxtext(layer l, int start, const char *s) { int itr, aux; int x = LEFT_MARGIN, y = start; zeroOut(l->matrix, l->x, l->y); for (itr = 0; s[itr] != 0 && y < l->y; itr++){ if (is_valid_char(s[itr])){ if (x < l->x - RIGHT_MARGIN){ l->matrix[y][x++] = s[itr]; } else if (l->mode & LYR_AUTO_WRAP) { x = LEFT_MARGIN; y++; itr--; } } else if (s[itr] == '\n'){ x = LEFT_MARGIN; y++; } else if (s[itr] == '\t'){ aux = 4; while (aux--){ if (x < l->x){ l->matrix[y][x++] = ' '; } else aux = 0; } } } return l; }
int main(int argc, char **argv) { char **sudo_tb; int index; int index_malloc; index_malloc = 0; if (argc == 10 && is_valid_number(argv) == 1) { index = 0; sudo_tb = (char**)malloc(9 * sizeof(char*)); while (index_malloc < 9) { sudo_tb[index_malloc] = (char*)malloc(10 * sizeof(char)); index_malloc++; } copy_sudo(argv, sudo_tb); if (is_valid_char(argv) == 1 && sudoku(sudo_tb, 0)) print_sudoku(sudo_tb); else write(1, "Erreur\n", 7); free_grid(sudo_tb); } else write(1, "Errer\n", 7); return (0); }
void dump_val_buf() { int i, cnt, lcnt; char cur; printf("\ndumping the val_buf (aggregate=%d):\n\n", aggregate); cnt = 0, lcnt = 0; for (i = 0, cur = val_buf[0]; i < total_num_chars;) { if (cur == val_buf[i]) { if ((cnt % aggregate) == 0) { if (is_valid_char(cur)) { printf("%c", cur); lcnt++; if (lcnt == 80) { printf("\n"); lcnt = 0; } } } cnt++; i++; } else { cur = val_buf[i]; cnt = 0; } } printf("\n"); }
CubitStatus RefEntityName::clean(CubitString &raw_name) { if (raw_name == "") return CUBIT_FAILURE; // A valid name consists of alphanumeric characters plus '.', '_', '-', or '@' CubitStatus found_invalid_character = CUBIT_FAILURE; // Initial character must be alphabetic or "_". char c = raw_name.get_at(0); if (!is_valid_first_char(c)) { if (is_valid_first_char(get_character("replace"))) raw_name.put_at(0, get_character("replace")); else raw_name.put_at(0, '_'); found_invalid_character = CUBIT_SUCCESS; } for (unsigned int i = 1; i < raw_name.length(); i++) { c = raw_name.get_at(i); if (!is_valid_char(c)) { found_invalid_character = CUBIT_SUCCESS; raw_name.put_at(i, get_character("replace")); } } return found_invalid_character; }
static char * fixup_back(char *s) { char *p, *p1, *p2; char tmp[1000]; char *t; char *w; /* first of all, check if we have to do anything */ if (!*s) return s; for (p = s + 1; *p; p++) if (*p == '\b' && *(p + 1) == '\b') break; if (!*p) return s; /* now search for runs of the form char*n, back*n, char. */ for (p = s; *p; p++) { for (p1 = p; *p1 && is_valid_char(*p1); p1++); if (*p1 == '\b') { for (p2 = p1; *p2 && *p2 == '\b'; p2++); /* do we have `enough' chars for the backspaces? */ if (p2 - p1 > 1 && p1 - p >= p2 - p1) { if (p1 - p > p2 - p1) { p = p1 - (p2 - p1); } /* the situation is as follows: p points to the first char, p1 to the first backspace (end first char run), p2 to the first char in the 2nd run Question: how to do that without tmp storage? */ (void) strncpy(tmp, p, p1 - p); t = tmp; w = p; while (t - tmp < p1 - p && *p2) { *w++ = *t++; *w++ = '\b'; *w++ = *p2++; } p = p2; } else { p = p2; } } else { /* we hit some other control char, just continue at this position */ p = p1; } } return s; }
BOOL vmsVideoSiteHtmlCodeParser::Parse_Grouper(LPCSTR pszHtml) { LPCSTR psz = strstr (pszHtml, "flvURL="); if (psz == NULL) return FALSE; psz += 7; fsString strUrl; while (*psz && *psz != '&') strUrl += *psz++; fsString strTitle; psz = strstr (pszHtml, "<h1"); if (psz == NULL) psz = strstr (pszHtml, "<H1"); if (psz != NULL) { while (*psz && *psz != '>') psz++; if (*psz != 0) { psz++; while (*psz == ' ' || is_valid_char (*psz) == FALSE) psz++; while (*psz && *psz != '<') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } fsDecodeHtmlText (strTitle); while (strTitle.GetLength () && strTitle [strTitle.GetLength () - 1] == ' ') strTitle [strTitle.GetLength () - 1] = 0; } } m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "flv"; m_bDirectLink = TRUE; return TRUE; }
// Put the actual codepont to use in the font->char_array[].codepoint_tmp field. static void fixup_codepoints(deark *c, struct font_render_ctx *fctx) { i64 i; i32 c1; i64 num_uncoded_chars = 0; u8 *used_codepoint_map = NULL; u8 codepoint_already_used; if(!fctx->render_as_unicode) { for(i=0; i<fctx->font->num_chars; i++) { fctx->codepoint_tmp[i] = fctx->font->char_array[i].codepoint_nonunicode; } goto done; } // An array of bits to remember if we've seen a codepoint before (BMP only). // A character with a duplicate codepoint will be moved to another // location, so that it doesn't get painted over the previous one. used_codepoint_map = de_malloc(c, 65536/8); for(i=0; i<fctx->font->num_chars; i++) { if(!is_valid_char(&fctx->font->char_array[i])) continue; c1 = fctx->font->char_array[i].codepoint_unicode; codepoint_already_used = 0; if(c1>=0 && c1<65536) { // Check if we've seen this codepoint before. codepoint_already_used = used_codepoint_map[c1/8] & (1<<(c1%8)); // Remember that we've seen this codepoint. used_codepoint_map[c1/8] |= 1<<(c1%8); } if(codepoint_already_used || c1==DE_CODEPOINT_INVALID) { if(codepoint_already_used) { de_dbg2(c, "moving duplicate codepoint U+%04x at index %d to private use area", (unsigned int)c1, (int)i); } // Move uncoded characters to a Private Use area. // (Supplementary Private Use Area-A = U+F0000 - U+FFFFD) if(DE_CODEPOINT_MOVED + num_uncoded_chars <= DE_CODEPOINT_MOVED_MAX) { fctx->codepoint_tmp[i] = (i32)(DE_CODEPOINT_MOVED + num_uncoded_chars); num_uncoded_chars++; } } else { fctx->codepoint_tmp[i] = c1; } } done: de_free(c, used_codepoint_map); }
/** Return true iff `str` is a valid Symbol. */ static inline bool is_valid(const std::basic_string<char>& str) { if (str.empty() || (str[0] >= '0' && str[0] <= '9')) { return false; // Must start with a letter or underscore } for (size_t i = 0; i < str.length(); ++i) { if (!is_valid_char(str[i])) { return false; // All characters must be _, a-z, A-Z, 0-9 } } return true; }
/* * postprocess the val_buf */ void postprocess() { int i, cnt, start; char cur; rec = malloc(num_threads * sizeof(int)); if (rec == NULL) { printf("Error creating rec: out of memory\n"); exit(1); } for ( i = 0 ; i < num_threads; i++ ) { rec[i] = 0; } /* postprocess the val_buf */ cnt = 0; for (i = 0, cur = val_buf[0]; i < total_num_chars;) { if (cur == val_buf[i]) { if ( is_valid_char(val_buf[i]) ) { rec[(((int)val_buf[i]) - START_CHAR)]++; } cnt++; i++; } else { if ( is_valid_char(cur) ) { if (ppvals) printf("%c: %d\n", cur, cnt); } cur = val_buf[i]; cnt = 0; } } if ( (((int)cur) >= (START_CHAR)) && (((int)cur) <= (START_CHAR+num_threads)) ) { if (ppvals) printf("%c: %d\n", cur, cnt); } }
BOOL vmsVideoSiteHtmlCodeParser::Parse_GoogleVideo(LPCSTR pszHtml) { LPCSTR psz = strstr (pszHtml, "/googleplayer.swf?"); if (psz == NULL) return FALSE; psz = strstr (psz, "videoUrl"); if (psz == NULL) return FALSE; psz = strstr (psz, "http"); if (psz == NULL) return FALSE; fsString strUrl; while (*psz && *psz != '"') strUrl += *psz++; if (strUrl [strUrl.GetLength () - 1] == '\\') strUrl [strUrl.GetLength () - 1] = 0; fsDecodeHtmlUrl (strUrl); fsString strTitle; psz = strstr (pszHtml, "pvprogtitle"); if (psz) { while (*psz && *psz != '>') psz++; if (*psz) { psz++; while (*psz == ' ') psz++; while (*psz && *psz != '<') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } while (strTitle.GetLength () && strTitle [strTitle.GetLength () - 1] == ' ') strTitle [strTitle.GetLength () - 1] = 0; fsDecodeHtmlText (strTitle); } } m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "flv"; m_bDirectLink = TRUE; return TRUE; }
void RefEntityName::set_character(char rep, const CubitString &type) { if (is_valid_char(rep)) { if (type.get_at(0) == 'R' || type.get_at(0) == 'r') replacementCharacter = rep; else if (type.get_at(0) == 'S' || type.get_at(0) == 's') suffixCharacter = rep; else PRINT_ERROR("Invalid character type '%s', must be " "'replacement' or 'suffix'\n", type.c_str()); } else { PRINT_ERROR("Character '%c' is not a valid entity name character\n", rep); } }
static int line_is_valid(char* line) { char c = 0; while(c = *line++, c) { int ret = is_delimeter(c) || is_valid_char(c) || is_space_char(c); if(!ret){ err("line contain invalid chars!! ascii val(0x%x)\n", c); return 0; } } return 1;//line is valid }
int cti_teplotu(const int fd, const char adresa, const int format) { char buff[BUFF_LENGTH]; char temp[BUFF_LENGTH]; int res = 0; int i = 0; int j = 0; int value; buff[i++] = 'T'; buff[i++] = adresa; buff[i++] = (format == FORMAT_READ) ? 'R' : 'I' ; buff[i] = 0; printf("zapisuju : %s\n", buff); res = temp_zapis_data(fd, buff, i); printf("zapsano : %d\n", res); res = 0; res = temp_cti_data(fd, buff, BUFF_LENGTH - 1, 10, 0x0D); //TQS by taky mel pouzivat 0x0D // sprintf(buff, "*H-025.9C"); //testovani zaporne hodnoty // buff[9] = 0x0D; // res = 10; if(res <= 8) return TEMP_ERR; buff[res] = 0; printf("prislo z cidla : %s\n", buff); memset(temp, 0, BUFF_LENGTH); for(j = 0, i = 0 ; buff[j] != 0 && j < BUFF_LENGTH; j++) { if(is_valid_char(buff[j])) temp[i++] = buff[j]; } printf("zkopirovano %s\n", temp); sscanf(temp, "%d", &value); printf("hodnota : %d", value); return value; }
/** Convert a string to a valid symbol. * * This will make a best effort at turning `str` into a complete, valid * Symbol, and will always return one. */ static inline Symbol symbolify(const std::basic_string<char>& in) { if (in.empty()) { return Symbol("_"); } std::basic_string<char> out(in); for (size_t i = 0; i < in.length(); ++i) { if (!is_valid_char(out[i])) { out[i] = '_'; } } if (is_valid_start_char(out[0])) { return Symbol(out); } else { return Symbol(std::string("_") + out); } }
static void get_min_max_codepoint(struct font_render_ctx *fctx) { i64 i; fctx->min_codepoint = 0x10ffff; fctx->max_codepoint = 0; fctx->num_valid_chars = 0; for(i=0; i<fctx->font->num_chars; i++) { if(!is_valid_char(&fctx->font->char_array[i])) continue; if(fctx->codepoint_tmp[i] == DE_CODEPOINT_INVALID) continue; fctx->num_valid_chars++; if(fctx->codepoint_tmp[i] < fctx->min_codepoint) fctx->min_codepoint = fctx->codepoint_tmp[i]; if(fctx->codepoint_tmp[i] > fctx->max_codepoint) fctx->max_codepoint = fctx->codepoint_tmp[i]; } }
BOOL vmsVideoSiteHtmlCodeParser::Parse_Further_LiveDigital(LPCSTR pszTxt) { LPCSTR psz = strstr (pszTxt, "content_url="); if (psz == NULL) return FALSE; fsString strUrl; psz += lstrlen ("content_url="); while (*psz && *psz != '&') strUrl += *psz++; fsDecodeHtmlUrl (strUrl); fsString strTitle; psz = strstr (pszTxt, "title="); if (psz) { psz += lstrlen ("title="); while (*psz && *psz != '&') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } fsDecodeHtmlText (strTitle); } m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = (LPCSTR)strUrl + strUrl.GetLength () - 3; m_bDirectLink = TRUE; return TRUE; }
BOOL vmsVideoSiteHtmlCodeParser::Parse_Youtube(LPCSTR pszHtml) { fsString strTitle, strUrl; LPSTR psz = const_cast<LPSTR>(strstr (pszHtml, "<meta name=\"title\"")); if (psz) { psz = strstr (psz, "content="); if (psz) { psz += lstrlen ("content="); if (*psz == '"') { psz++; while (*psz && *psz != '"') { if (is_valid_char (*psz)) strTitle += *psz++; else psz++; } } } } psz = const_cast<LPSTR>(strstr (pszHtml, "swfArgs =")); if (psz == NULL) return FALSE; psz = strchr (psz, '{'); if (psz == NULL) return FALSE; psz++; fsString strBase = "http://youtube.com/"; fsString strParams; while (*psz != '}') { while (*psz == ' ' || *psz == ',') psz++; fsString str; while (*psz && *psz != ':') { if (*psz == ' ') { str = ""; break; } str += *psz++; } if (str.IsEmpty ()) break; if (*psz == ':') psz++; while (*psz == ' ') psz++; if (str.Length () > 2 && str [0] == '"' && str [str.Length () - 1] == '"') { lstrcpy (str, str.pszString+1); str [str.Length () - 1] = 0; } if (lstrcmpi (str, "BASE_YT_URL") == 0) { strBase = ExtractValue (psz); } else { if (strParams.IsEmpty () == FALSE) strParams += '&'; strParams += str; strParams += "="; strParams += ExtractValue (psz); } } strUrl = strBase; strUrl += "get_video?"; strUrl += strParams; fsDecodeHtmlText (strTitle); m_strVideoTitle = strTitle; m_strVideoUrl = strUrl; m_strVideoType = "flv"; m_bDirectLink = TRUE; return TRUE; }
static void do_parse_xml( const char *xml, const char **lp, int *line, value callb, const char *parentname ) { STATE state = BEGIN; STATE next = BEGIN; field aname = (field)0; value attribs = NULL_VAL; value nodename = NULL_VAL; const char *start = NULL; const char *p = *lp; char c = *p; int nsubs = 0, nbrackets = 0; while( c ) { switch( state ) { case IGNORE_SPACES: switch( c ) { case '\n': case '\r': case '\t': case ' ': break; default: state = next; continue; } break; case BEGIN: switch( c ) { case '<': state = IGNORE_SPACES; next = BEGIN_NODE; break; default: start = p; state = PCDATA; continue; } break; case PCDATA: if( c == '<' ) { val_ocall1(callb,id_pcdata,copy_string(start,p-start)); nsubs++; state = IGNORE_SPACES; next = BEGIN_NODE; } break; case CDATA: if( c == ']' && p[1] == ']' && p[2] == '>' ) { val_ocall1(callb,id_cdata,copy_string(start,p-start)); nsubs++; p += 2; state = BEGIN; } break; case BEGIN_NODE: switch( c ) { case '!': if( p[1] == '[' ) { p += 2; if( (p[0] != 'C' && p[0] != 'c') || (p[1] != 'D' && p[1] != 'd') || (p[2] != 'A' && p[2] != 'a') || (p[3] != 'T' && p[3] != 't') || (p[4] != 'A' && p[4] != 'a') || (p[5] != '[') ) ERROR("Expected <![CDATA["); p += 5; state = CDATA; start = p + 1; break; } if( p[1] == 'D' || p[1] == 'd' ) { if( (p[2] != 'O' && p[2] != 'o') || (p[3] != 'C' && p[3] != 'c') || (p[4] != 'T' && p[4] != 't') || (p[5] != 'Y' && p[5] != 'y') || (p[6] != 'P' && p[6] != 'p') || (p[7] != 'E' && p[7] != 'e') ) ERROR("Expected <!DOCTYPE"); p += 7; state = DOCTYPE; start = p + 1; break; } if( p[1] != '-' || p[2] != '-' ) ERROR("Expected <!--"); p += 2; state = COMMENT; start = p + 1; break; case '?': state = HEADER; start = p; break; case '/': if( parentname == NULL ) ERROR("Expected node name"); start = p + 1; state = IGNORE_SPACES; next = CLOSE; break; default: state = TAG_NAME; start = p; continue; } break; case TAG_NAME: if( !is_valid_char(c) ) { if( p == start ) ERROR("Expected node name"); nodename = copy_string(start,p-start); attribs = alloc_empty_object(); state = IGNORE_SPACES; next = BODY; continue; } break; case BODY: switch( c ) { case '/': state = WAIT_END; nsubs++; val_ocall2(callb,id_xml,nodename,attribs); break; case '>': state = CHILDS; nsubs++; val_ocall2(callb,id_xml,nodename,attribs); break; default: state = ATTRIB_NAME; start = p; continue; } break; case ATTRIB_NAME: if( !is_valid_char(c) ) { value tmp; if( start == p ) ERROR("Expected attribute name"); tmp = copy_string(start,p-start); aname = val_id(val_string(tmp)); if( !val_is_null(val_field(attribs,aname)) ) ERROR("Duplicate attribute"); state = IGNORE_SPACES; next = EQUALS; continue; } break; case EQUALS: switch( c ) { case '=': state = IGNORE_SPACES; next = ATTVAL_BEGIN; break; default: ERROR("Expected ="); } break; case ATTVAL_BEGIN: switch( c ) { case '"': case '\'': state = ATTRIB_VAL; start = p; break; default: ERROR("Expected \""); } break; case ATTRIB_VAL: if( c == *start ) { value aval = copy_string(start+1,p-start-1); alloc_field(attribs,aname,aval); state = IGNORE_SPACES; next = BODY; } break; case CHILDS: *lp = p; do_parse_xml(xml,lp,line,callb,val_string(nodename)); p = *lp; start = p; state = BEGIN; break; case WAIT_END: switch( c ) { case '>': val_ocall0(callb,id_done); state = BEGIN; break; default : ERROR("Expected >"); } break; case WAIT_END_RET: switch( c ) { case '>': if( nsubs == 0 ) val_ocall1(callb,id_pcdata,alloc_string("")); val_ocall0(callb,id_done); *lp = p; return; default : ERROR("Expected >"); } break; case CLOSE: if( !is_valid_char(c) ) { if( start == p ) ERROR("Expected node name"); { value v = copy_string(start,p - start); if( _strcmpi(parentname,val_string(v)) != 0 ) { buffer b = alloc_buffer("Expected </"); buffer_append(b,parentname); buffer_append(b,">"); ERROR(buffer_data(b)); } } state = IGNORE_SPACES; next = WAIT_END_RET; continue; } break; case COMMENT: if( c == '-' && p[1] == '-' && p[2] == '>' ) { val_ocall1(callb,id_comment,copy_string(start,p-start)); p += 2; state = BEGIN; } break; case DOCTYPE: if( c == '[' ) nbrackets++; else if( c == ']' ) nbrackets--; else if( c == '>' && nbrackets == 0 ) { val_ocall1(callb,id_doctype,copy_string(start,p-start)); state = BEGIN; } break; case HEADER: if( c == '?' && p[1] == '>' ) { p++; val_ocall1(callb,id_comment,copy_string(start,p-start)); state = BEGIN; } break; } c = *++p; if( c == '\n' ) (*line)++; } if( state == BEGIN ) { start = p; state = PCDATA; } if( parentname == NULL && state == PCDATA ) { if( p != start || nsubs == 0 ) val_ocall1(callb,id_pcdata,copy_string(start,p-start)); return; } ERROR("Unexpected end"); }
void Lexer::consume_line(string str, const char *file, int line) { int len = str.length(); while (len > 0 && isspace(str[len-1])) len--; bool is_string = false; string curr_block; for (int i=0 ; i < len ; i++) { char ch = str[i]; assert(ch != '\r' && ch != '\n'); //!! This is wrong, because a tab replaces several spaces. //!! from here on, columns numbers are likely to be wrong //## TABS IN STRINGS SHOULD NOT BE ALLOWED if (ch == '\t') ch = ' '; if (is_string) { if (ch == '"') { tokens.push_back(Token(curr_block + ch, line, i-curr_block.length(), file)); curr_block = ""; is_string = false; } else { curr_block += ch; if (ch == '\\') { if (++i >= len) break; ch = str[i]; if (ch == 'n' || ch == '"' || ch == '\\') curr_block += ch; else on_invalid_char(ch, file, line, i); // Input error in string } } } else { if (ch == '/' && i+1 < len && str[i+1] == '/') { len = i; break; } if (ch == '"') { if (curr_block != "") { consume_token_block(curr_block, i-curr_block.length(), file, line); curr_block = ""; } is_string = true; curr_block += ch; } else { if (!is_valid_char(ch)) on_invalid_char(ch, file, line, i); // Input error outside string curr_block += ch; } } } if (is_string) on_error("Non-terminated string", file, line, len); if (curr_block != "") consume_token_block(curr_block, len-curr_block.length(), file, line); }
int ConfFile::parse(string filename) { enum CFScanState {CF_ERROR,CF_RESET,CF_SECTNAME,CF_COMMENT,CF_VARNAME,CF_VALUE, CF_STRING,CF_WAITSECT,CF_WAITEQ,CF_WAITVAL,CF_READY}; err_str = ""; ifstream infile(filename.c_str()); if (!infile) return -1; CFScanState state = CF_RESET; line_num=1; string sectname; string varname; string valuestr; ConfSection* cursect=0; while (char ch = next_char(infile)){ switch (ch) { case 0x0a:line_num++; // Fall through... case 0x0d:switch (state) { case CF_READY: case CF_RESET: case CF_COMMENT: state=CF_RESET; break; case CF_VALUE: state=CF_READY; break; default: err_str = "Unexpected end of line."; state=CF_ERROR; } break; case 0x09: case ' ': switch (state) { case CF_VARNAME: state=CF_WAITEQ; break; case CF_VALUE: state=CF_READY; break; case CF_STRING: valuestr+=ch; break; default: ; } break; case '#': switch (state) { case CF_READY: case CF_RESET: state=CF_COMMENT; break; case CF_STRING: valuestr+=ch; break; default: err_str = "Char '#' not valid here."; state=CF_ERROR; } break; case '=': switch (state) { case CF_VARNAME: case CF_WAITEQ: state=CF_WAITVAL; valuestr=""; break; case CF_STRING: valuestr+=ch; break; case CF_COMMENT: break; default: err_str = "Char '=' not valid here."; state=CF_ERROR; } break; case '"': switch (state) { case CF_WAITVAL: state=CF_STRING; valuestr=""; break; case CF_STRING: state=CF_READY; break; case CF_COMMENT: break; default: err_str = "Char '\"' not valid here."; state=CF_ERROR; } break; case '[': switch (state) { case CF_RESET: state=CF_WAITSECT; sectname=""; cursect=0; break; case CF_STRING: valuestr+=ch; break; case CF_COMMENT: break; default: err_str = "Char '[' not valid here."; state=CF_ERROR; } break; case ']': switch (state) { case CF_SECTNAME: state=CF_RESET; if (sectname.empty()) state=CF_ERROR; else { cursect = new ConfSection; cursect->SetName(sectname); sections.push_back(cursect); sectname=""; } break; case CF_STRING: valuestr+=ch; break; case CF_COMMENT: break; default: err_str = "Char ']' not valid here."; state=CF_ERROR; } break; default: if (state==CF_STRING) valuestr+=ch; else { if (is_valid_char(ch)){ switch(state) { case CF_WAITSECT: sectname=ch; state=CF_SECTNAME; break; case CF_WAITVAL: valuestr=ch; state=CF_VALUE; break; case CF_SECTNAME: sectname+=ch; break; case CF_RESET: varname=ch; state=CF_VARNAME; break; case CF_VARNAME: varname+=ch; break; case CF_VALUE: case CF_STRING: valuestr+=ch; break; case CF_WAITEQ: err_str = "'=' expected."; state=CF_ERROR; break; default: ; } } else if (state != CF_COMMENT){ err_str = "Illegal char found."; state=CF_ERROR; } } } //switch (ch)... if (state==CF_READY){ if (cursect) { cursect->AddValue(varname,valuestr); varname=""; valuestr=""; state=CF_RESET; } else{ err_str = "Section header missing."; state=CF_ERROR; } } if (state==CF_ERROR){ ostringstream oss; oss << "(Line " << line_num << "): " << err_str; err_str = oss.str(); break; } } //while if (state==CF_ERROR) return -2; return 0; }
int main(int argc, char *argv[]) { int i, len, first_flag = 0, end_flag = 0, middle_flag = 0, spec_flag = 1; char special_char = '*'; char input[MAX_CHAR_LEN] = {0}; char buff[MAX_CHAR_LEN] = {0}; char target[MAX_CHAR_LEN] = {0}; //fprintf(stdout, "Please input your chars:"); fgets(input, MAX_CHAR_LEN, stdin); memcpy(buff, input, MAX_CHAR_LEN); len = strlen(buff); for(i=0; i<len; i++) //the last character is '\n', { if(buff[i] == '\n') break; #if 0 if(!is_valid_char(buff[i], special_char)) { //including invalid char, just return back printf("the character '%c' is not a valid character\n", buff[i]); return -1; } #endif if(is_alpha_or_num(buff[i])) { spec_flag = 0; if(middle_flag != 0) { end_flag = i; memcpy(target+middle_flag, buff+i, len-i); break; } else { first_flag = i; } } if(buff[i] == special_char) { if(first_flag != 0) { middle_flag = i; memcpy(target, buff, i); first_flag = 0; //reset the flag } } } //printf("you input is: %s", buff); if(middle_flag != 0 && end_flag != 0 && first_flag == 0) printf("%s", target); else if(spec_flag == 1) printf(" "); else printf("%s", buff); return 0; }
void de_font_bitmap_font_to_image(deark *c, struct de_bitmap_font *font1, de_finfo *fi, unsigned int createflags) { struct font_render_ctx *fctx = NULL; i64 i, j, k; de_bitmap *img = NULL; i64 xpos, ypos; i64 img_leftmargin, img_topmargin; i64 img_rightmargin, img_bottommargin; i64 img_vpixelsperchar; i64 img_width, img_height; i64 num_table_rows_to_display; i64 num_table_rows_total; i64 last_valid_row; struct de_bitmap_font *dfont = NULL; i64 chars_per_row = 32; const char *s; struct row_info_struct *row_info = NULL; struct col_info_struct *col_info = NULL; int unicode_req = 0; i64 label_stride; i64 rownum, colnum; i64 curpos; unsigned int dnflags; fctx = de_malloc(c, sizeof(struct font_render_ctx)); fctx->font = font1; if(fctx->font->num_chars<1) goto done; if(fctx->font->num_chars>17*65536) goto done; if(fctx->font->nominal_width>512 || fctx->font->nominal_height>512) { de_err(c, "Font size too big (%d"DE_CHAR_TIMES"%d). Not supported.", (int)fctx->font->nominal_width, (int)fctx->font->nominal_height); goto done; } // -1 = "no preference" unicode_req = de_get_ext_option_bool(c, "font:tounicode", -1); if(unicode_req==0 && (fctx->font->has_nonunicode_codepoints || !fctx->font->has_unicode_codepoints)) { ; // Render as nonunicode. } else if(fctx->font->has_unicode_codepoints && (unicode_req>0 || fctx->font->prefer_unicode || !fctx->font->has_nonunicode_codepoints)) { fctx->render_as_unicode = 1; } s = de_get_ext_option(c, "font:charsperrow"); if(s) { chars_per_row = de_atoi64(s); if(chars_per_row<1) chars_per_row=1; } dfont = make_digit_font(c); fctx->codepoint_tmp = de_mallocarray(c, fctx->font->num_chars, sizeof(i32)); fixup_codepoints(c, fctx); get_min_max_codepoint(fctx); if(fctx->num_valid_chars<1) goto done; num_table_rows_total = fctx->max_codepoint/chars_per_row+1; // TODO: Clean up these margin calculations, and make it more general. if(fctx->render_as_unicode) { img_leftmargin = dfont->nominal_width * 5 + 6; } else { if(fctx->max_codepoint >= 1000) img_leftmargin = dfont->nominal_width * 5 + 6; else img_leftmargin = dfont->nominal_width * 3 + 6; } img_topmargin = dfont->nominal_height + 6; img_rightmargin = 1; img_bottommargin = 1; // Scan the characters, and record relevant information. row_info = de_mallocarray(c, num_table_rows_total, sizeof(struct row_info_struct)); col_info = de_mallocarray(c, chars_per_row, sizeof(struct col_info_struct)); for(i=0; i<chars_per_row; i++) { #define MIN_CHAR_CELL_WIDTH 5 col_info[i].display_width = MIN_CHAR_CELL_WIDTH; } for(k=0; k<fctx->font->num_chars; k++) { i64 char_display_width; if(fctx->codepoint_tmp[k] == DE_CODEPOINT_INVALID) continue; if(!is_valid_char(&fctx->font->char_array[k])) continue; rownum = fctx->codepoint_tmp[k] / chars_per_row; colnum = fctx->codepoint_tmp[k] % chars_per_row; if(rownum<0 || rownum>=num_table_rows_total) { de_err(c, "internal: bad rownum"); de_fatalerror(c); } // Remember that there is at least one valid character in this character's row. row_info[rownum].is_visible = 1; // Track the maximum width of any character in this character's column. char_display_width = (i64)(fctx->font->char_array[k].width + (int)fctx->font->char_array[k].extraspace_l + (int)fctx->font->char_array[k].extraspace_r); if(char_display_width > col_info[colnum].display_width) { col_info[colnum].display_width = char_display_width; } } img_vpixelsperchar = fctx->font->nominal_height + 1; // Figure out how many rows are used, and where to draw them. num_table_rows_to_display = 0; last_valid_row = -1; curpos = img_topmargin; for(j=0; j<num_table_rows_total; j++) { if(!row_info[j].is_visible) continue; // If we skipped one or more rows, leave some extra vertical space. if(num_table_rows_to_display>0 && !row_info[j-1].is_visible) curpos+=3; last_valid_row = j; row_info[j].display_pos = curpos; curpos += img_vpixelsperchar; num_table_rows_to_display++; } if(num_table_rows_to_display<1) goto done; // Figure out the positions of the columns. curpos = img_leftmargin; for(i=0; i<chars_per_row; i++) { col_info[i].display_pos = curpos; curpos += col_info[i].display_width + 1; } img_width = col_info[chars_per_row-1].display_pos + col_info[chars_per_row-1].display_width + img_rightmargin; img_height = row_info[last_valid_row].display_pos + img_vpixelsperchar -1 + img_bottommargin; img = de_bitmap_create(c, img_width, img_height, 1); // Clear the image for(j=0; j<img->height; j++) { for(i=0; i<img->width; i++) { de_bitmap_setpixel_gray(img, i, j, 128); } } // Draw/clear the cell backgrounds for(j=0; j<num_table_rows_total; j++) { if(!row_info[j].is_visible) continue; ypos = row_info[j].display_pos; for(i=0; i<chars_per_row; i++) { i64 ii, jj; xpos = col_info[i].display_pos; for(jj=0; jj<img_vpixelsperchar-1; jj++) { for(ii=0; ii<col_info[i].display_width; ii++) { de_bitmap_setpixel_gray(img, xpos+ii, ypos+jj, (ii/2+jj/2)%2 ? 176 : 192); } } } } // Draw the labels in the top margin. // TODO: Better label spacing logic. if(fctx->font->nominal_width <= 12) label_stride = 2; else label_stride = 1; for(i=0; i<chars_per_row; i++) { if(i%label_stride != 0) continue; xpos = col_info[i].display_pos + col_info[i].display_width/2; ypos = img_topmargin - 3; dnflags = DNFLAG_HCENTER; if(fctx->render_as_unicode) dnflags |= DNFLAG_HEX; draw_number(c, img, dfont, i, xpos, ypos, dnflags); } // Draw the labels in the left margin. for(j=0; j<num_table_rows_total; j++) { if(!row_info[j].is_visible) continue; xpos = img_leftmargin - 3; ypos = row_info[j].display_pos + (img_vpixelsperchar + dfont->nominal_height + 1)/2; dnflags = 0; if(fctx->render_as_unicode) dnflags |= DNFLAG_HEX | DNFLAG_LEADING_ZEROES; draw_number(c, img, dfont, j*chars_per_row, xpos, ypos, dnflags); } // Render the glyphs. for(k=0; k<fctx->font->num_chars; k++) { if(fctx->codepoint_tmp[k] == DE_CODEPOINT_INVALID) continue; if(!is_valid_char(&fctx->font->char_array[k])) continue; rownum = fctx->codepoint_tmp[k] / chars_per_row; colnum = fctx->codepoint_tmp[k] % chars_per_row; if(rownum<0 || rownum>=num_table_rows_total) { de_err(c, "internal: bad rownum"); de_fatalerror(c); } xpos = col_info[colnum].display_pos; ypos = row_info[rownum].display_pos; de_font_paint_character_idx(c, img, fctx->font, k, xpos, ypos, DE_STOCKCOLOR_BLACK, DE_STOCKCOLOR_WHITE, 0); } de_bitmap_write_to_file_finfo(img, fi, createflags); done: if(dfont) { de_free(c, dfont->char_array); de_destroy_bitmap_font(c, dfont); } de_bitmap_destroy(img); de_free(c, row_info); de_free(c, col_info); if(fctx) { de_free(c, fctx->codepoint_tmp); de_free(c, fctx); } }
static int is_char_back_char(char *s) { return is_valid_char(*s) && *(s + 1) == '\b' && *(s + 2) && is_valid_char(*(s + 2)); }
command_t read_command_stream (command_stream_t s) { /* take a command stream, return a command_t object to be used by main.c Read a command from STREAM; return it, or NULL on EOF. If there is` an error, report the error and exit instead of returning. Given: array of pointers to c strings (command_stream) Output: singular constructed command objects such that when read_command_stream is called one command object is returned, and the next time it is called the second command object is returned, etc. command object is returned, and the next time */ int index = s->cmd_count; int list_size = s->size; struct command* cmd_ptr = NULL; struct command* special_ptr = NULL; struct command* outer_special_ptr = NULL; char ** word_list = NULL; int word_list_size = 0; char dir = 'l'; char outside_dir = 'l'; bool found_start_subshell = false; if (index > list_size-1) return NULL; // Skip repeating \n's if (strcmp(s->command_list[index], "\n") == 0 && cmd_ptr == NULL) { while ( strcmp(s->command_list[index], "\n") == 0 ) { index++; s->line_count++; if (index > list_size-1) return NULL; } } //short loop_iteration = 0; while ( strcmp(s->command_list[index], "\n") != 0) { // printf("Iteration: %d\n", loop_iteration++); // Syntax Check if (!is_valid_char(s->command_list[index])) { syn_error(s); } // For ooerators if(is_special_char(s->command_list[index])) { if(strcmp(s->command_list[index], "(") == 0) { found_start_subshell = true; if(word_list_size !=0 && word_list != NULL && special_ptr==NULL) syn_error(s); outer_special_ptr = special_ptr; special_ptr = NULL; outside_dir = dir; dir = 'l'; } else if(strcmp(s->command_list[index], ")")==0) { if(found_start_subshell) { found_start_subshell = false; } else { syn_error(s); } if(!build_word_command(word_list, &cmd_ptr)) { syn_error(s); } word_list = NULL; word_list_size = 0; //if there is a special command if(special_ptr != NULL) { if(!add_cmd_to_special(&cmd_ptr, &special_ptr, 'r')) syn_error(s); cmd_ptr = special_ptr; } if(!build_sub_command(&cmd_ptr, &special_ptr)) syn_error(s); cmd_ptr = special_ptr; special_ptr = outer_special_ptr; dir = outside_dir; // printf("I came here!\n"); } //check first for redirection special chars else if(strcmp(s->command_list[index], "<") == 0) { if (word_list != NULL) { if (!build_word_command(word_list, &cmd_ptr)) syn_error(s); word_list = NULL; word_list_size = 0; } //if the next command = special char output an error if(index+1 < list_size) { if(is_special_char(s->command_list[index+1]) || strcmp(s->command_list[index+1], "\n") == 0) { syn_error(s); } //else output an error if(!add_word_to_IO(s->command_list[index+1], &cmd_ptr, 'i')) { syn_error(s); } } //increment index an additional time because //command for index+1 is already constructed index++; } else if(strcmp(s->command_list[index], ">") ==0) { if (word_list != NULL) { if (!build_word_command(word_list, &cmd_ptr)) syn_error(s); word_list = NULL; word_list_size = 0; } if(index+1 < list_size)//check condition later { if(is_special_char(s->command_list[index+1]) || strcmp(s->command_list[index+1], "\n") == 0) syn_error(s); if(!add_word_to_IO(s->command_list[index+1], &cmd_ptr, 'o')) syn_error(s); } index++; } //else condition: not "<" or ">" so implement l and r ptrs // Add to left only in first iteration else if(dir == 'l') { if (word_list != NULL) { if (!build_word_command(word_list, &cmd_ptr)) syn_error(s); word_list = NULL; word_list_size = 0; } if (!build_special_command(s->command_list[index], &special_ptr)) syn_error(s); if (!add_cmd_to_special(&cmd_ptr,&special_ptr, 'l')) syn_error(s); cmd_ptr = NULL; dir = 'r'; } // Add to further special commands else { if (word_list != NULL) { if (!build_word_command(word_list, &cmd_ptr)) syn_error(s); word_list = NULL; word_list_size = 0; } if (!add_cmd_to_special(&cmd_ptr,&special_ptr,'r')) syn_error(s); cmd_ptr = special_ptr; special_ptr = NULL; if (!build_special_command(s->command_list[index], &special_ptr)) syn_error(s); if (!add_cmd_to_special(&cmd_ptr,&special_ptr,'l')) syn_error(s); } // printf("i made it this far\n"); //solves "dangling \n" problem if ( index+2 < list_size) { if(strcmp(s->command_list[index+1], "\n") == 0 && special_ptr != NULL) { index++; } } } // For Single Words else { // Append to word_list before making commands if(!add_cmd_to_list(s->command_list[index], &word_list, word_list_size)) syn_error(s); // printf("Added command %s to position %d\n", s->command_list[index], word_list_size); word_list_size++; } index++; } s->cmd_count = index + 1; // Fix case for single sided sequence commands if (special_ptr != NULL) { if (special_ptr->type == SEQUENCE_COMMAND && special_ptr->u.command[1] == NULL) { add_cmd_to_list(" ", &word_list, word_list_size); } } // Build remaining list if (cmd_ptr == NULL) { if (!build_word_command(word_list, &cmd_ptr)) syn_error(s); // int i; // for(i = 0; i< word_list_size; i++) // { // printf("adding to cmd word %s\n", word_list[i]); // } word_list_size = 0; word_list = NULL; } if(found_start_subshell) syn_error(s); // If reaming word_list commands and incomplete tree if (special_ptr != NULL) { if(!add_cmd_to_special(&cmd_ptr, &special_ptr, 'r')) syn_error(s); cmd_ptr = special_ptr; } //printf("I made it this far!"); // Increment line count s->line_count++; return cmd_ptr; }
// Paint a character at the given index in the given font, to the given bitmap. void de_font_paint_character_idx(deark *c, de_bitmap *img, struct de_bitmap_font *font, i64 char_idx, i64 xpos, i64 ypos, u32 fgcol, u32 bgcol, unsigned int flags) { i64 i, j; i64 i_src; // -1 = No source position i64 j_src; u8 x; int fg; u32 clr; struct de_bitmap_font_char *ch; i64 num_x_pixels_to_paint; int vga9col_flag = 0; if(char_idx<0 || char_idx>=font->num_chars) return; ch = &font->char_array[char_idx]; if(!is_valid_char(ch)) return; if(ch->width > font->nominal_width) return; if(ch->height > font->nominal_height) return; num_x_pixels_to_paint = (i64)ch->extraspace_l + (i64)ch->width + (i64)ch->extraspace_r; if((flags&DE_PAINTFLAG_VGA9COL) && ch->width==8) { vga9col_flag = 1; num_x_pixels_to_paint = 9; } for(j=0; j<ch->height; j++) { j_src = j; if(flags&DE_PAINTFLAG_TOPHALF) { j_src = j/2; } else if(flags&DE_PAINTFLAG_BOTTOMHALF) { j_src = (ch->height+j)/2; } for(i=0; i<num_x_pixels_to_paint; i++) { i_src = i; if(flags&DE_PAINTFLAG_LEFTHALF) { i_src = i/2; } else if(flags&DE_PAINTFLAG_RIGHTHALF) { i_src = (num_x_pixels_to_paint+i)/2; } if(i_src==8 && vga9col_flag) { // Manufacture a column 8. if(ch->codepoint_nonunicode>=0xb0 && ch->codepoint_nonunicode<=0xdf) { i_src = 7; // Make this pixel a duplicate of the one in col #7. } else { i_src = -1; // Make this pixel a background pixel. } } i_src -= (i64)ch->extraspace_l; if(i_src>=0 && i_src<ch->width) { x = ch->bitmap[j_src*ch->rowspan + i_src/8]; fg = (x & (1<<(7-i_src%8))) ? 1 : 0; } else { fg = 0; } if(fg || !(flags&DE_PAINTFLAG_TRNSBKGD)) { clr = fg ? fgcol : bgcol; de_bitmap_setpixel_rgba(img, xpos+i, ypos+ch->v_offset+j, clr); } } } }