void basicInput::readAsciiString(cString& ret, uint8 terminate, const uint numberOfCharacter) { /* Read single characters */ uint8 ch = terminate + 1; uint left = numberOfCharacter; while (ch != terminate) { if (left == 0) { TRACE(TRACE_VERY_LOW, "readString: Number of bytes for string exceeded."); XSTL_THROW(cException, EXCEPTION_READ_ERROR); } // Try to read a single character from the stream uint read = pipeRead((void *)(&ch), sizeof(ch)); if (read == 0) { // EOF reached and the NULL terminate couldn't be found. XSTL_THROW(cException, EXCEPTION_READ_ERROR); } left--; // Remove the terminate character if (ch != terminate) ret+= (char)(ch); } }
int read(int fd, void* buffer, unsigned int count) { bool result = false; _FD_STRUCT* fds; DWORD numRead; fds = fds_from_index(fd); if (fds == NULL) { errno = EBADF; return -1; } initStdHandlesInline(); if (fds->pipe != NULL) { numRead = pipeRead(fds->pipe, (unsigned char*)buffer, count); } else if (fds->hFile != INVALID_HANDLE_VALUE) { if (!ReadFile(fds->hFile, buffer, count, &numRead, NULL)) { if (GetLastError() == ERROR_HANDLE_EOF) fds->eof = TRUE; // else // fds->error = TRUE; return 0; } } else return 0; return (int)numRead; }
cString basicInput::readAsciiPascal16String(const uint numberOfCharacter /* = MAX_CHAR */) { uint16 length; // Read the length streamReadUint16(length); if (length > numberOfCharacter) { // Invalid number of characters XSTL_THROW(cException, EXCEPTION_READ_ERROR); } // Allocate dynamic memory cBuffer string(length + 1); // Read all bytes uint read = pipeRead(string.getBuffer(), length); if (read != length) { // EOF reached! XSTL_THROW(cException, EXCEPTION_READ_ERROR); } string[length] = 0; return cString((char *)string.getBuffer()); }
bool SchedulerBase::CreateSignalChannel(int *outSigId, SignalCallback callback, void *userdata) { LogAssert(IsMainThread()); if (!LogVerify(outSigId) || !LogVerify(callback)) return false; *outSigId = -1; // Create a set of pipes int fdPipe[2]; int flags; int ret = pipe(fdPipe); if (ret != 0) { gLog.ErrnoError(errno, "Unable to create pipe for signaling"); return false; } FileDescriptor pipeRead(fdPipe[0]); FileDescriptor pipeWrite(fdPipe[1]); flags = fcntl(pipeRead, F_GETFL); flags = flags | O_NONBLOCK; if (-1 == fcntl(pipeRead, F_SETFL, flags)) { gLog.LogError("Failed to set read pipe to non-blocking."); return false; } flags = fcntl(pipeWrite, F_GETFL); flags = flags | O_NONBLOCK; if (-1 == fcntl(pipeWrite, F_SETFL, flags)) { gLog.LogError("Failed to set write pipe to non-blocking."); return false; } if (!watchSocket(pipeRead)) return false; schedulerSignalItem item; item.callback = callback; item.userdata = userdata; item.fdWrite = pipeWrite; item.fdRead = pipeRead; m_signals[item.fdRead] = item; pipeWrite.Detach(); pipeRead.Detach(); *outSigId = item.fdWrite; gLog.Optional(Log::TimerDetail, "Created signal channel from %d to %d .", item.fdWrite, item.fdRead); return true; }
void basicInput::streamReadUint8(uint8& byte) { uint readed = pipeRead(&byte, sizeof(uint8)); if (readed != sizeof(char)) { XSTL_THROW(cException, EXCEPTION_READ_ERROR); } }
void basicInput::streamReadUint16(uint16& word) { uint8 buffer[sizeof(uint16)]; uint readed = pipeRead(&buffer, sizeof(uint16)); if (readed != sizeof(uint16)) { XSTL_THROW(cException, EXCEPTION_READ_ERROR); } word = ((cEndian*)(this))->readUint16(buffer); }
void basicInput::readAllStream(cBuffer& rawData) { if (canLength && canGetPointer) { uint left = length() - getPointer(); pipeRead(rawData, left); } else { // Changing the page size into the default reading size. uint savePageSize = rawData.getPageSize(); rawData.setPageSize(getPipeReadBestRequest()); uint oldBufferSize = rawData.getSize(); rawData.changeSize(oldBufferSize + getPipeReadBestRequest(), false); uint readed = 0; uint total = oldBufferSize; uint8* buffer = rawData.getBuffer() + total; // Reading loop do { // Read a chunk total += (readed = pipeRead(buffer, getPipeReadBestRequest())); // Save space for the next reading operation if (readed == getPipeReadBestRequest()) { rawData.changeSize(total + getPipeReadBestRequest()); buffer = rawData.getBuffer() + total; } else { // Restore the page size rawData.setPageSize(savePageSize); // Chop free space rawData.changeSize(total); } } while (readed == getPipeReadBestRequest()); // Until there isn't error } }
MED_RET_T evt_getFullStatus(int num_evt_pipe, struct evt_t * evt) { //printk("evt chk: handler=%d evt @ %x\n",num_evt_pipe,evt); if(num_evt_pipe >= 0 && num_evt_pipe < NB_EVT_PIPES && evt != NULL) { evt->evt=0; evt->evt_class=0; evt->data=0; if(evt_pipe_tab[num_evt_pipe].used!=1) return -MED_ENBUSY; pipeRead(&(evt_pipe_tab[num_evt_pipe].evt_pipe), evt, sizeof(struct evt_t)); } else return -MED_EINVAL; return MED_OK; }
cString basicInput::readFixedSizeString(uint numberOfCharacters, int unicodeSize) { cBuffer buffer((numberOfCharacters + 1) * unicodeSize); uint8* buf = buffer.getBuffer(); pipeRead(buf, numberOfCharacters * unicodeSize); if (unicodeSize > 1) { buf[numberOfCharacters * unicodeSize] = 0; cBuffer ret(sizeof(character) * (numberOfCharacters + 1)); character* retBuf = (character*)ret.getBuffer(); cChar::covert2string(retBuf, numberOfCharacters+1, buf, unicodeSize); return cString(retBuf); } else { buf[numberOfCharacters] = 0; return cString((char*)buf); } }
uint basicInput::pipeRead(cBuffer& buffer, const uint length) { uint oldSize = buffer.getSize(); // Old size of the buffer uint readed = 0; // The readed bytes. /* Prepare the output buffer */ buffer.changeSize(oldSize + length); /* Read the data */ readed = pipeRead((buffer.getBuffer() + oldSize), length); /* Chope the data */ if (readed < length) { buffer.changeSize(oldSize + readed); } return readed; }
void basicInput::readUnicodeString(cString& ret, unichar terminate, const uint numberOfCharacter) { /* Read single characters */ unichar ch = terminate + 1; uint left = numberOfCharacter; while (ch != terminate) { if (left == 0) { TRACE(TRACE_VERY_LOW, "readString: Number of bytes for string exceeded."); XSTL_THROW(cException, EXCEPTION_READ_ERROR); } // Try to read a single character from the stream uint read = pipeRead((void *)(&ch), sizeof(ch)); if (read == 0) { // EOF reached and the NULL terminate couldn't be found. XSTL_THROW(cException, EXCEPTION_READ_ERROR); } left--; // Remove the terminate character if (ch != terminate) { #ifndef XSTL_UNICODE ret+= cChar::covert2Ascii(ch); #else ret+= ch; #endif } } }
cString basicInput::readAsciiPascal8String(const uint numberOfCharacter /* = MAX_CHAR */) { uint8 length; uint8 string[256]; // Read the length streamReadUint8(length); if (length > numberOfCharacter) { // Invalid number of characters XSTL_THROW(cException, EXCEPTION_READ_ERROR); } // Read the string bytes uint read = pipeRead(string, length); if (read != length) { // EOF reached! XSTL_THROW(cException, EXCEPTION_READ_ERROR); } string[length] = 0; return cString((char *)string); }
void basicInput::pipeReadNotEos(cBuffer& buffer, const uint length) { CHECK(pipeRead(buffer, length) == length); }