/** * (Internal) * Read word from a text file, * word is a sequence of symbols not equal ' '. * handle - file handler, * buf - buffer for input, * return number of read symbols or -1 if something wrong */ int jsr238_read_word(void *handle, unsigned char *buf) { int res; unsigned char lbuf[MAX_LOCALE_LENGTH]; unsigned char *buf_ptr; if (buf == NULL) { buf_ptr = lbuf; } else { buf_ptr = buf; } if ((res = jsr238_skip_space(handle, buf_ptr)) > 0) { if (*buf_ptr == QUOTATION_MARK) { if (pcsl_file_read(handle, buf_ptr, 1) <= 0) { return -1; } } res = 0; while (*buf_ptr > QUOTATION_MARK) { buf_ptr++; res++; if (pcsl_file_read(handle, buf_ptr, 1) <= 0) { break; } } *buf_ptr = 0; return res; } return -1; }
/** * Gets a resource length for pointed reource identifier and locale. * * @param resource_id resource identifier * @param locale_index index of the locale * @return resource length in case successful operation and -1 if something is wrong */ JSR238_STATUSCODE jsr238_get_resource_length(jsize* length /* OUT */, jint resource_id, jint locale_index) { ResourceTableEntryType entry; unsigned char buf[4]; void * handle; JSR238_STATUSCODE res = JSR238_STATUSCODE_FAIL; REPORT_CALL_TRACE(LC_LOWUI, "LF:STUB:jsr238_get_resource_length()\n"); if (jsr238_devresource_file_open(locale_index, &handle) >= 0) { pcsl_file_read(handle, (char*)&ResourceFileHeader, 4); pcsl_file_read(handle, buf, 4); jsr238_fillInt(&ResourceFileHeader.headerLength, buf); if (jsr238_seekEntry(handle, resource_id, ResourceFileHeader.headerLength / 8, &entry)){ *length = entry.resourceOffset; pcsl_file_read(handle, buf, 4); pcsl_file_read(handle, buf, 4); buf[0] = 0; jsr238_fillInt(&entry.resourceOffset, buf); *length = entry.resourceOffset - *length; res = JSR238_STATUSCODE_OK; } pcsl_file_close(handle); } return res; }
/** * Checks if resource with given identifier exists. * * @param resource_id resource identifier * @param locale_index index of the locale * @return 1 if resource ID is valid and 0 if something is wrong */ jboolean jsr238_is_valid_resource_id(jint resource_id, jint locale_index) { ResourceTableEntryType entry; unsigned char buf[4]; void * handle; jboolean res = PCSL_FALSE; REPORT_CALL_TRACE(LC_LOWUI, "LF:STUB:jsr238_is_valid_resource_id()\n"); if (jsr238_devresource_file_open(locale_index, &handle) >= 0) { pcsl_file_read(handle, (char*)&ResourceFileHeader, 4); pcsl_file_read(handle, buf, 4); jsr238_fillInt(&ResourceFileHeader.headerLength, buf); res = jsr238_seekEntry(handle, resource_id, ResourceFileHeader.headerLength / 8, &entry); pcsl_file_close(handle); } return res; }
/** * (Internal) * Skip spaces in a text file. * handle - file handler, * buf - buffer for input, * return 1 if non-delimiting symbol is read, 0 on EOF, -1 on error */ int jsr238_skip_space(void * handle, unsigned char *buf) { int res; do { res = pcsl_file_read(handle, buf, 1); } while ((*buf <= WORDS_DELIMITER) && (res > 0)); return res; }
/** * Gets a resource for pointed resource identifier and locale. * * @param resource buffer for the resource * @param res_len length of the resource buffer * @param resource_id resource identifier * @param locale_index index of the locale * @return 0 in case successful operation and -1 if something is wrong */ JSR238_STATUSCODE jsr238_get_resource(jbyte* resource, jint res_len, jint resource_id, jint locale_index) { void * handle; ResourceTableEntryType entry; unsigned char buf[4]; int res = JSR238_STATUSCODE_FAIL; REPORT_CALL_TRACE(LC_LOWUI, "LF:STUB:jsr238_get_resource()\n"); if (jsr238_devresource_file_open(locale_index, &handle) >= 0) { pcsl_file_read(handle, (char*)&ResourceFileHeader, 4); pcsl_file_read(handle, buf, 4); jsr238_fillInt(&ResourceFileHeader.headerLength, buf); if (jsr238_seekEntry(handle, resource_id, ResourceFileHeader.headerLength / 8, &entry)){ pcsl_file_seek(handle, entry.resourceOffset, 0); pcsl_file_read(handle, resource, res_len); res = JSR238_STATUSCODE_OK; } pcsl_file_close(handle); } return res; }
/** * Tests for pcsl_file_get_time(). */ void testTimes() { int res; long time1, time2; int fileID; int i, j; res = pcsl_file_get_time(&dummy1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query succeeded for non-existent file", res == -1); res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_TRUNC | PCSL_FILE_O_CREAT, (void **)&fileID); pcsl_file_close((void *)fileID); assertTrue("File creation failed", res == 0); res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query failed", res == 0); res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_APPEND, (void **)&fileID); assertTrue("File opening failed", res == 0); for (i = 0; i < 1000000; i++) { res = pcsl_file_write((void *)fileID, (unsigned char *)&time1, 4); assertTrue("Write failed", res == 4); } pcsl_file_close((void *)fileID); /* Writing 4 MB to a file should take several seconds */ res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time2); assertTrue("Time query failed", res == 0); assertTrue("Modification time should be greater", time2 > time1); for (j = 0; j < 10; j++) { res = pcsl_file_open(&file1, PCSL_FILE_O_RDWR | PCSL_FILE_O_APPEND, (void **)&fileID); assertTrue("File opening failed", res == 0); for (i = 0; i < 1000000; i++) { res = pcsl_file_read((void *)fileID, (unsigned char *)&time1, 4); assertTrue("Read failed", res == 4); } pcsl_file_close((void *)fileID); } /* Reading anything from a file shouldn't lead to modification time change */ res = pcsl_file_get_time(&file1, PCSL_FILE_TIME_LAST_MODIFIED, &time1); assertTrue("Time query failed", res == 0); assertTrue("Modification time shouldn't change", time1 == time2); pcsl_file_unlink(&file1); }
/** * (Internal) Seek required entry in the device resource file header. * handle - device resource file handler. * entry_id - ID of the required entry. * rn - number of entries in the device resource file. * entry - pointer to the buffer for the entry. */ jboolean jsr238_seekEntry(void *handle, int entry_id, int rn, ResourceTableEntryType * entry){ int i; unsigned char bufEntry[8]; jboolean res = PCSL_FALSE; for (i=0;i < rn;i++) { pcsl_file_read(handle, bufEntry, 8); jsr238_fillInt(&entry->resourceID, bufEntry); if (entry->resourceID == entry_id) { entry->resourceType = bufEntry[4]; bufEntry[4] = 0; jsr238_fillInt(&entry->resourceOffset, &bufEntry[4]); res = PCSL_TRUE; break; } } return res; }
/* * Read from an open file in storage, returning the number of bytes read or * -1 for the end of the file. May read less than the length of the buffer. * * If not successful *ppszError will set to point to an error string, * on success it will be set to NULL. */ long storageRead(char** ppszError, int handle, char* buffer, long length) { long bytesRead; *ppszError = NULL; if (0 == length) { return 0; } bytesRead = pcsl_file_read((void *)handle, (unsigned char*)buffer, length); REPORT_INFO2(LC_CORE, "storageRead on fd %d res = %ld\n", handle, bytesRead); if (-1 == bytesRead) { *ppszError = getLastError("storageRead()"); } else if (0 == bytesRead) { /* end of file in java is -1 */ bytesRead = -1; } return bytesRead; }
size_t OsFile_read(OsFile_Handle handle, void *buffer, size_t size, size_t count) { return (pcsl_file_read(handle->pcsl_handle, (unsigned char*)buffer, (long)(size*count))) / size; }