// 确保可写字节 void NetMessage::ensure_writable_bytes(size_t size) { if (writeable() < size) { make_space(size); } assert(writeable() >= size); }
//0 for non-blocking (returns immediately), -1 for infinite blocking /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/) { DBG("Trying to write %d chars", length); do { int ret = waitSpace(timeout); if(ret) { WARN("Error %d while waiting for space", ret); return ret; } int s = space(); //Prevent macro issues int writeLen = MIN( s, length ); DBG("Writing %d chars", writeLen); setupWriteableISR(false); while(writeLen) { m_outBuf.queue(*buf); buf++; length--; writeLen--; } //If m_serial tx fifo is empty we need to start the packet write if( m_outBuf.available() && m_serialTxFifoEmpty ) { writeable(); } setupWriteableISR(true); } while(length); DBG("Write successful"); return OK; }
// 分配空间 void NetMessage::make_space(size_t size) { if (writeable() + prependable() < size) { if (!is_dynmic()) { set_dynamic(); } dynamic_data_->resize(writer_pos_ + size); } else { if (!is_dynmic()) { memmove(static_data_, static_data_ + reader_pos_, writer_pos_ - reader_pos_); } else { memmove(dynamic_data_->data(), dynamic_data_->data() + reader_pos_, writer_pos_ - reader_pos_); } const size_t readable_size = readable(); reader_pos_ = 0; writer_pos_ = readable_size; assert(readable_size == readable()); } }
static void processCmd( std::string const &cmdLine, int fd ) { std::string writeable( cmdLine ); std::string parts[4]; char *next = (char *)writeable.c_str(); unsigned i ; for( i = 0 ; i < 4 ; i++ ) { char *savePtr ; char const *tok = strtok_r( next, "\t", &savePtr ); if( tok ) { parts[i] = tok ; next = 0 ; } else break; } if( 4 == i ) { char const *cmd = parts[0].c_str(); char const *approval = 0 ; char const *msgs = 0 ; if( 0 == strcmp( "enter", cmd ) ) { approval = approved ; msgs = enterMsgs ; } else if( 0 == strcmp( "exit", cmd ) ) { approval = approved ; msgs = exitMsgs ; } else if( 0 == strcmp( "retrieve", cmd ) ) { approval = report ; msgs = reportMsgs ; } else fprintf( stderr, "unknown barcode cmd %s\n", cmd ); if( 0 != approval ) { std::string responseString ; responseString = parts[1]; responseString += approval ; responseString += msgs ; responseString += '\r' ; write( fd, responseString.c_str(), responseString.size() ); } else fprintf( stderr, usage ); } else fprintf( stderr, usage ); }
bool NormalFile::setContents(const String& contents) { if ( writeable() ) { m_stats.mtime.setToCurrent(); m_contents = contents; return true; } return false; }
bool set_value(char *value) { if (writeable()) { if (_value != NULL) { FreeHeap(_value); } _value = AllocateHeap(strlen(value)+1, mtInternal); if (_value != NULL) { strcpy(_value, value); } return true; } return false; }
bool Serial_stm32::write(const uint8_t* bytes, const uint32_t size) { bool ret = false; // Queue byte if (writeable() >= size) { for (uint32_t i = 0; i < size; ++i) { tx_buffer_.put(bytes[i]); } ret = true; } // Start transmission if (tx_buffer_.readable() >= 1) { usart_enable_tx_interrupt(config_.device); } return ret; }
int main(int argc, char **argv) { // Reading input : the N words of the dictionary, and letters available int N; scanf("%d\n", &N); char dictionary[N][30]; for (int i = 0; i < N; i++) gets(dictionary[i]); char letters[8]; fgets(letters,8,stdin); // Searches max pointCount amongst writeable words int max = 0; int count; char *res = NULL; for (int i = 0; i < N; i++) if (writeable(dictionary[i], letters) && (count = pointCount(dictionary[i], WEIGHTS)) > max) { max = count; res = dictionary[i]; } printf("%s\n", res); return EXIT_SUCCESS; }
// 写入数据大小 void NetMessage::has_written(size_t size) { assert(writeable() >= size); writer_pos_ += size; }
void fssConfigSmb::read() { std::ifstream file( configFilename.c_str() ); if ( file ) { std::string old; //QRegExp rx_sectionStart("^\\s*\\[([^\\]]+)\\]"); //QRegExp rx_path("path\\s*=\\s*([^\\s][^\\n]+)"); regex_t rx_sectionStart; regex_t rx_path; regex_t rx_writeable; regmatch_t match_SectionStart[2]; regmatch_t match_path[2]; regmatch_t match_writeable[2]; if( regcomp(&rx_sectionStart, "^[[:space:]]*\\[(.+)\\]", REG_EXTENDED) || regcomp(&rx_path, "path[[:space:]]*=[[:space:]]*([^[:space:]].*)", REG_EXTENDED|REG_NEWLINE) || regcomp(&rx_writeable, "writeable[[:space:]]*=[[:space:]]*([^[:space:]].*)", REG_EXTENDED|REG_NEWLINE) ) { std::cerr << "failed to compile regex" << std::endl; exit(255); } std::string line; while ( ! file.eof() ) { getline( file, line ); if( ! regexec(&rx_sectionStart, line.c_str(), 1, match_SectionStart, 0) || file.eof() ) { std::string label(""); std::string path(""); std::string writeable("no"); if( !regexec(&rx_sectionStart, old.c_str(), 2, match_SectionStart, 0) ) { size_t size = match_SectionStart[1].rm_eo-match_SectionStart[1].rm_so; if( size > 0 && size < old.length() ) { label = old.substr( match_SectionStart[1].rm_so, size ); } } if( !regexec(&rx_path, old.c_str(), 2, match_path, 0)) { size_t size = match_path[1].rm_eo-match_path[1].rm_so; if ( size > 0 && size < old.length() ) { path = old.substr( match_path[1].rm_so, size ); } } if( !regexec(&rx_writeable, old.c_str(), 2, match_writeable, 0)) { size_t size = match_writeable[1].rm_eo-match_writeable[1].rm_so; if( size > 0 && size < old.length() ) { writeable = old.substr( match_writeable[1].rm_so, size ); } } fssShareSmb* share = new fssShareSmb( path ); if( writeable != "no" ) share->setReadOnly( false ); else share->setReadOnly( true ); share->setRawData( old ); share->setSmbLabel( label ); shareList.push_back( share ); old = ""; } old += (line+"\n"); } regfree( &rx_sectionStart ); regfree( &rx_path ); } }