JSStringRef file_read(uint64_t descriptor) { UFILE* ufile = descriptor_to_ufile(descriptor); JSStringRef rv = NULL; void* buffer = malloc(sizeof(uint16_t) * 1024); int32_t read = u_file_read(buffer, 1024, ufile); if (read > 0) { rv = JSStringCreateWithCharacters(buffer, (size_t)read); } free(buffer); return rv; }
jz_val jz_load(JZ_STATE, UFILE* file) { UChar* str = calloc(sizeof(UChar), 2000); int32_t len; jz_str* input; jz_cons* root; jz_bytecode* bytecode; jz_val result; len = u_file_read(str, 2000, file); input = jz_str_external(jz, len, str); if (!(root = jz_parse_string(jz, input))) exit(1); free(str); if (!(bytecode = jz_compile(jz, root))) exit(1); result = jz_vm_run(jz, bytecode); return result; }
static int ofile_read_handler(void *data, unsigned char *buffer, size_t size, size_t *size_read) { yaml_parser_t *parser = data; *size_read = (u_file_read((UChar *)buffer, (int32_t)(size/sizeof(OChar)-1), (UFILE *)parser->input.file)*sizeof(OChar)); if (parser->input.file != NULL) { return 1; } else { return 0; } }
bool pelet::UCharBufferedFileClass::OpenFile(UFILE* ufile, int startingCapacity) { if (NULL != File) { u_fclose(File); File = NULL; } if (!Buffer) { LineNumber = 1; Buffer = new UChar[startingCapacity]; BufferCapacity = startingCapacity; Current = Buffer; TokenStart = Buffer; Limit = Buffer; Marker = Buffer; HasReachedEof = false; Eof = NULL; } bool opened = false; File = ufile; if (NULL != File) { // point to the start of the file LineNumber = 1; CharacterPos = 0; Current = Buffer; Limit = Buffer; opened = true; int read = u_file_read(Buffer, BufferCapacity, File); Limit = Buffer + BufferCapacity - 1; if (read < BufferCapacity) { u_fclose(File); File = NULL; // insert null character as the lexers will look for null characters as EOF Buffer[read] = '\0'; HasReachedEof = true; Eof = Buffer + read; } } return opened; }
void pelet::UCharBufferedFileClass::AppendToLexeme(int minToGet) { if (NULL != File) { //printf("getting extra %d chars\n", minToGet); // since Limit points to the last character of the string (not past), we do +1 int validContentsCount = (Limit - TokenStart + 1); int charsToGet = 0; UChar* startOfFreeSpace = 0; if (TokenStart > Buffer) { RemoveLeadingSlackSpace(); startOfFreeSpace = Buffer + validContentsCount; charsToGet = BufferCapacity - validContentsCount; } // if, after we removed the slack; we are still close to the edge; grow the buffer // choosing 20 because the longest PHP keywords is about this long if ((Current - Limit) < 20) { int oldCapacity = BufferCapacity; GrowBuffer(2 * oldCapacity); startOfFreeSpace = Buffer + validContentsCount; charsToGet = oldCapacity + (oldCapacity - validContentsCount); } // should read charsToGet bytes from file; not charsToFill // we want to get as much from the file as possible without re-allocation if (charsToGet > 0) { int read = u_file_read(startOfFreeSpace, charsToGet, File); Limit = Buffer + BufferCapacity - 1; if (read < charsToGet) { HasReachedEof = true; // insert null character as the lexers will look for null characters as EOF startOfFreeSpace[read] = '\0'; Eof = startOfFreeSpace + read; u_fclose(File); File = NULL; } } } }
static int read_chars(lua_State *L, UFILE *ufile, int32_t n) { int32_t rlen; // how much to read int32_t nr; // number of chars actually read luaL_Buffer b; luaL_buffinit(L, &b); rlen = ICU4LUA_UBUFFERSIZE; // try to read that much each time do { UChar* p = icu4lua_prepubuffer(&b); if (rlen > n) { rlen = n; // cannot read more than asked } nr = u_file_read(p, rlen, ufile); icu4lua_addusize(&b, nr); n -= nr; // still have to read `n' chars } while (n > 0 && nr == rlen); // until end of count or eof icu4lua_pushuresult(&b, UFILE_UV_USTRING_META, UFILE_UV_USTRING_POOL); if (icu4lua_ustrlen(L,-1) == 0 && n != 0) { return 0; } return 1; }
UBreakIterator* get_rules(const char *ruleFileName, UErrorCode status) { /* Read in the rule source file */ long result; long ruleFileSize; FILE *file; OFILE *ufile; UBreakIterator *return_me; file = fopen(ruleFileName, "rb"); if( file == 0 ) { fprintf(stderr, "Could not open file \"%s\"\n", ruleFileName); exit(-1); } fseek(file, 0, SEEK_END); ruleFileSize = ftell(file); fseek(file, 0, SEEK_SET); char *ruleBufferC = (char *) omalloc (ruleFileSize + 1); ruleBufferC[ruleFileSize] = '\0'; result = (long)fread(ruleBufferC, 1, ruleFileSize, file); if (result != ruleFileSize) { fprintf(stderr, "Error reading file \"%s\"\n", ruleFileName); exit (-1); } /* Look for a Unicode Signature (BOM) on the rule file */ int32_t signatureLength; const char * ruleSourceC = ruleBufferC; const char* encoding = ucnv_detectUnicodeSignature( ruleSourceC, ruleFileSize, &signatureLength, &status); /* fprintf(stderr, "DetectUnicodeSig: \"%s\"\n", encoding); */ if (U_FAILURE(status)) { fprintf(stderr, "\nCan not initialize ICU. status = %s\n", u_errorName(status)); exit(1); } if(encoding!=NULL ) { ruleSourceC += signatureLength; ruleFileSize -= signatureLength; } /* fprintf(stderr, "encoding: \"%s\"\n", encoding); */ /* Open a converter to take the rule file to UTF-16 */ UConverter* conv; conv = ucnv_open(encoding, &status); if (U_FAILURE(status)) { fprintf(stderr, "ucnv_open: ICU Error \"%s\"\n", u_errorName(status)); exit(1); } ufile = u_finit(file, NULL, NULL); u_frewind(ufile); UChar *ruleSourceU = (UChar *) omalloc ((ruleFileSize*sizeof(UChar))+1); long charsRead = u_file_read(ruleSourceU, ruleFileSize, ufile); /* u_fprintf(u_stderr, "Chars read: \"%i\", File size: \"%i\"\n", charsRead, ruleFileSize); */ ruleSourceU[charsRead] = 0; /* u_fprintf(u_stderr, "RulesourceU POST: \"%S\"\n", ruleSourceU); */ ucnv_close(conv); u_fclose(ufile); /* Create the break iterator from the rules */ /* This will compile the rules. */ UParseError parseError; parseError.line = 0; parseError.offset = 0; return_me = ubrk_openRules(ruleSourceU, ruleFileSize, NULL, 0, &parseError, &status); if (U_FAILURE(status)) { fprintf(stderr, "createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n", u_errorName(status), (int)parseError.line, (int)parseError.offset); exit(1); }; return return_me; }