int main(int argc, char *argv[]) { char input_char; FILE *fp; if((fp = fopen(argv[1], "r")) == NULL) { printf("\nError!\n\n%s file doesn't exist!\n\n", argv[1]); return -1; } if((output_fp = fopen(argv[2], "w")) == NULL) { printf("\nError!\n\n%s.out file can't be created doesn't exist!\n\n", argv[2]); return -1; } while(!feof(fp)) { input_char = fgetc(fp); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } if (ispunct(input_char)) { symbol_handler(fp, input_char); } } fclose(fp); fclose(output_fp); return 0; }
int main(int argc, char** argv) { google::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]); HttpServer server(FLAGS_threads_num); HesTokenHandler token_handler(&server); HesVerifyTokenHandler verify_token_handler(&server); std::string address = StringConcat( FLAGS_server_ip, ":", FLAGS_server_port); if (!server.Start(address)) { LOG(ERROR) << "start failed. address " << address; return 1; } LOG(INFO) << "listen on " << address; int ret = server.Run(); LOG(INFO) << "server finish. ret " << ret; return ret; }
hubbub_error token_handler(const hubbub_token *token, void *pw) { static const char *token_names[] = { "DOCTYPE", "StartTag", "EndTag", "Comment", "Character", "EOF" }; size_t i; context *ctx = (context *) pw; struct json_object *obj = NULL; struct array_list *items; for (; ctx->output_index < array_list_length(ctx->output); ctx->output_index++) { /* Get object for index */ obj = (struct json_object *) array_list_get_idx(ctx->output, ctx->output_index); /* If it's not a string, we've found the expected output */ if (json_object_get_type(obj) != json_type_string) break; /* Otherwise, it must be a parse error */ assert(strcmp(json_object_get_string(obj), "ParseError") == 0); } /* If we've run off the end, this is an error -- the tokeniser has * produced more tokens than expected. We allow for the generation * of a terminating EOF token, however. */ assert("too many tokens" && (ctx->output_index < array_list_length(ctx->output) || token->type == HUBBUB_TOKEN_EOF)); /* Got a terminating EOF -- no error */ if (ctx->output_index >= array_list_length(ctx->output)) return HUBBUB_OK; /* Now increment the output index so we don't re-expect this token */ ctx->output_index++; /* Expected output must be an array */ assert(json_object_get_type(obj) == json_type_array); items = json_object_get_array(obj); printf("got %s: expected %s\n", token_names[token->type], json_object_get_string((struct json_object *) array_list_get_idx(items, 0))); /* Make sure we got the token we expected */ assert(strcmp(token_names[token->type], json_object_get_string((struct json_object *) array_list_get_idx(items, 0))) == 0); switch (token->type) { case HUBBUB_TOKEN_DOCTYPE: { const char *expname = json_object_get_string( array_list_get_idx(items, 1)); const char *exppub = json_object_get_string( array_list_get_idx(items, 2)); const char *expsys = json_object_get_string( array_list_get_idx(items, 3)); bool expquirks = !json_object_get_boolean( array_list_get_idx(items, 4)); const char *gotname = (const char *)token->data.doctype.name.ptr; const char *gotpub, *gotsys; printf("'%.*s' %sids:\n", (int) token->data.doctype.name.len, gotname, token->data.doctype.force_quirks ? "(force-quirks) " : ""); if (token->data.doctype.public_missing) { gotpub = NULL; printf("\tpublic: missing\n"); } else { gotpub = (const char *) token->data.doctype.public_id.ptr; printf("\tpublic: '%.*s' (%d)\n", (int) token->data.doctype.public_id.len, gotpub, (int) token->data.doctype.public_id.len); } if (token->data.doctype.system_missing) { gotsys = NULL; printf("\tsystem: missing\n"); } else { gotsys = (const char *) token->data.doctype.system_id.ptr; printf("\tsystem: '%.*s' (%d)\n", (int) token->data.doctype.system_id.len, gotsys, (int) token->data.doctype.system_id.len); } assert(token->data.doctype.name.len == strlen(expname)); assert(strncmp(gotname, expname, strlen(expname)) == 0); assert((exppub == NULL) == (token->data.doctype.public_missing == true)); if (exppub) { assert(token->data.doctype.public_id.len == strlen(exppub)); assert(strncmp(gotpub, exppub, strlen(exppub)) == 0); } assert((expsys == NULL) == (token->data.doctype.system_missing == true)); if (gotsys) { assert(token->data.doctype.system_id.len == strlen(expsys)); assert(strncmp(gotsys, expsys, strlen(expsys)) == 0); } assert(expquirks == token->data.doctype.force_quirks); } break; case HUBBUB_TOKEN_START_TAG: { const char *expname = json_object_get_string( array_list_get_idx(items, 1)); struct lh_entry *expattrs = json_object_get_object( array_list_get_idx(items, 2))->head; bool self_closing = json_object_get_boolean( array_list_get_idx(items, 3)); const char *tagname = (const char *) token->data.tag.name.ptr; printf("expected: '%s' %s\n", expname, (self_closing) ? "(self-closing) " : ""); printf(" got: '%.*s' %s\n", (int) token->data.tag.name.len, tagname, (token->data.tag.self_closing) ? "(self-closing) " : ""); if (token->data.tag.n_attributes > 0) { printf("attributes:\n"); } assert(token->data.tag.name.len == strlen(expname)); assert(strncmp(tagname, expname, strlen(expname)) == 0); assert((token->data.tag.n_attributes == 0) == (expattrs == NULL)); assert(self_closing == token->data.tag.self_closing); for (i = 0; i < token->data.tag.n_attributes; i++) { char *expname = (char *) expattrs->k; const char *expval = json_object_get_string( (struct json_object *) expattrs->v); const char *gotname = (const char *) token->data.tag.attributes[i].name.ptr; size_t namelen = token->data.tag.attributes[i].name.len; const char *gotval = (const char *) token->data.tag.attributes[i].value.ptr; size_t vallen = token->data.tag.attributes[i].value.len; printf("\t'%.*s' = '%.*s'\n", (int) namelen, gotname, (int) vallen, gotval); assert(namelen == strlen(expname)); assert(strncmp(gotname, expname, strlen(expname)) == 0); assert(vallen == strlen(expval)); assert(strncmp(gotval, expval, strlen(expval)) == 0); expattrs = expattrs->next; } assert(expattrs == NULL); } break; case HUBBUB_TOKEN_END_TAG: { const char *expname = json_object_get_string( array_list_get_idx(items, 1)); const char *tagname = (const char *) token->data.tag.name.ptr; printf("'%.*s' %s\n", (int) token->data.tag.name.len, tagname, (token->data.tag.n_attributes > 0) ? "attributes:" : ""); assert(token->data.tag.name.len == strlen(expname)); assert(strncmp(tagname, expname, strlen(expname)) == 0); } break; case HUBBUB_TOKEN_COMMENT: { const char *expstr = json_object_get_string( array_list_get_idx(items, 1)); const char *gotstr = (const char *) token->data.comment.ptr; printf("expected: '%s'\n", expstr); printf(" got: '%.*s'\n", (int) token->data.comment.len, gotstr); assert(token->data.comment.len == strlen(expstr)); assert(strncmp(gotstr, expstr, strlen(expstr)) == 0); } break; case HUBBUB_TOKEN_CHARACTER: { int expstrlen = json_object_get_string_len( array_list_get_idx(items, 1)); const char *expstr =json_object_get_string( array_list_get_idx(items, 1)); const char *gotstr = (const char *) token->data.character.ptr; size_t len = min(token->data.character.len, expstrlen - ctx->char_off); printf("expected: '%.*s'\n", (int) len, expstr + ctx->char_off); printf(" got: '%.*s'\n", (int) token->data.character.len, gotstr); assert(memcmp(gotstr, expstr + ctx->char_off, len) == 0); if (len < token->data.character.len) { /* Expected token only contained part of the data * Calculate how much is left, then try again with * the next expected token */ hubbub_token t; t.type = HUBBUB_TOKEN_CHARACTER; t.data.character.ptr += len; t.data.character.len -= len; ctx->char_off = 0; token_handler(&t, pw); } else if (strlen(expstr + ctx->char_off) > token->data.character.len) { /* Tokeniser output only contained part of the data * in the expected token; calculate the offset into * the token and process the remainder next time */ ctx->char_off += len; ctx->output_index--; } else { /* Exact match - clear offset */ ctx->char_off = 0; } } break; case HUBBUB_TOKEN_EOF: printf("\n"); break; } return HUBBUB_OK; }
int symbol_handler(FILE *fp, char input_char) { while(ispunct(input_char)) { switch(input_char) { case '+': fprintf(output_fp, "43\n"); break; case '-': fprintf(output_fp, "45\n"); break; case '*': fprintf(output_fp, "42\n"); break; case '/': input_char = fgetc(fp); if (input_char == '/') comment_handler(fp, 1); else if (input_char == '*') comment_handler(fp, 2); else { fprintf(output_fp, "47\n"); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } continue; } break; case '&': input_char = fgetc(fp); if (input_char == '&') { fprintf(output_fp, "256\n"); } else { fprintf(output_fp, "\nError!\n Unless you're trying C code on my compiler. The scanner is yet not able to accept this kind of input\n\n"); return -1; } break; case '|': input_char = fgetc(fp); if (input_char == '|') { fprintf(output_fp, "257\n"); } else { printf("\nError!\n Unless you're trying C code on my compiler. The scanner is yet not able to accept this kind of input\n\n"); return -1; } break; case '!': input_char = fgetc(fp); if (input_char == '=') fprintf(output_fp, "261\n"); else { fprintf(output_fp, "33\n"); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } continue; } break; case '<': input_char = fgetc(fp); if (input_char == '=') fprintf(output_fp, "258\n"); else { fprintf(output_fp, "60\n"); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } continue; } break; case '>': input_char = fgetc(fp); if (input_char == '=') fprintf(output_fp, "259\n"); else { fprintf(output_fp, "62\n"); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } continue; } break; case '=': input_char = fgetc(fp); if (input_char == '=') fprintf(output_fp, "260\n"); else { fprintf(output_fp, "61\n"); if (isalnum(input_char)) { if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } continue; } break; case '(': fprintf(output_fp, "40\n"); break; case ')': fprintf(output_fp, "41\n"); break; case '[': fprintf(output_fp, "91\n"); break; case ']': fprintf(output_fp, "93\n"); break; case '{': fprintf(output_fp, "123\n"); break; case '}': fprintf(output_fp, "125\n"); break; case ',': fprintf(output_fp, "44\n"); break; case ';': fprintf(output_fp, "59\n"); break; case '"': string_handler(fp); break; } input_char = fgetc(fp); if (isdigit(input_char)) { digit_handler(fp, input_char); } if (isalpha(input_char)) { token_handler(fp, input_char); } } return 0; }