ICQMessage *History::getMessage(unsigned long offs) { if (offs == MSG_NEW) return NULL; if (offs >= MSG_PROCESS_ID) return pClient->getProcessMessage(offs); QFile f; if (!open(false, f)) return NULL; if (!f.at(offs)) return NULL; string type; if (!getLine(f, type)) return NULL; ICQMessage *res = loadMessage(f, type, offs); return res; }
bool getLine(QFile &f, string &s) { s = ""; char b[0x4000]; line_start = f.at(); long res = f.readLine(b, sizeof(b)); if (res == -1) return false; b[res] = 0; for (res--; res >= 0; res--){ if ((b[res] != '\r') && (b[res] != '\n')) break; b[res] = 0; } s = b; return true; }
unsigned long History::addMessage(ICQMessage *msg) { message_type *t; for (t = types; t->type; t++){ if (t->type == msg->Type()) break; } QFile f; if (!open(true, f)) return (unsigned long)(-1); unsigned long res = f.at(); writeStr(f, "["); writeStr(f, t->name); writeStr(f, "]\n"); ::save(msg, t->param, f); writeStr(f, "\n"); msg->Id = res; f.close(); return res; }
void KConfigINIBackEnd::parseSingleConfigFile(QFile &rFile, KEntryMap *pWriteBackMap, bool bGlobal, bool bDefault) { const char *s; // May get clobbered by sigsetjump, but we don't use them afterwards. const char *eof; // May get clobbered by sigsetjump, but we don't use them afterwards. QByteArray data; if(!rFile.isOpen()) // come back, if you have real work for us ;-> return; // using kdDebug() here leads to an infinite loop // remove this for the release, aleXXX // qWarning("Parsing %s, global = %s default = %s", // rFile.name().latin1(), bGlobal ? "true" : "false", bDefault ? "true" : "false"); QCString aCurrentGroup("<default>"); unsigned int ll = localeString.length(); #ifdef HAVE_MMAP static volatile const char *map; map = (const char *)mmap(0, rFile.size(), PROT_READ, MAP_PRIVATE, rFile.handle(), 0); if(map != MAP_FAILED) { s = (const char *)map; eof = s + rFile.size(); #ifdef SIGBUS struct sigaction act; act.sa_handler = mmap_sigbus_handler; sigemptyset(&act.sa_mask); #ifdef SA_ONESHOT act.sa_flags = SA_ONESHOT; #else act.sa_flags = SA_RESETHAND; #endif sigaction(SIGBUS, &act, &mmap_old_sigact); if(sigsetjmp(mmap_jmpbuf, 1)) { qWarning("SIGBUS while reading %s", rFile.name().latin1()); munmap((char *)map, rFile.size()); sigaction(SIGBUS, &mmap_old_sigact, 0); return; } #endif } else #endif { rFile.at(0); data = rFile.readAll(); s = data.data(); eof = s + data.size(); } bool fileOptionImmutable = false; bool groupOptionImmutable = false; bool groupSkip = false; int line = 0; for(; s < eof; s++) { line++; while((s < eof) && isspace(*s) && (*s != '\n')) s++; // skip leading whitespace, shouldn't happen too often // skip empty lines, lines starting with # if((s < eof) && ((*s == '\n') || (*s == '#'))) { sktoeol: // skip till end-of-line while((s < eof) && (*s != '\n')) s++; continue; // Empty or comment or no keyword } const char *startLine = s; if(*s == '[') // group { // In a group [[ and ]] have a special meaning while((s < eof) && (*s != '\n')) { if(*s == ']') { if((s + 1 < eof) && (*(s + 1) == ']')) s++; // Skip "]]" else break; } s++; // Search till end of group } const char *e = s; while((s < eof) && (*s != '\n')) s++; // Search till end of line / end of file if((e >= eof) || (*e != ']')) { fprintf(stderr, "Invalid group header at %s:%d\n", rFile.name().latin1(), line); continue; } // group found; get the group name by taking everything in // between the brackets if((e - startLine == 3) && (startLine[1] == '$') && (startLine[2] == 'i')) { if(!kde_kiosk_exception) fileOptionImmutable = true; continue; } aCurrentGroup = decodeGroup(startLine + 1, e - startLine); // cout<<"found group ["<<aCurrentGroup<<"]"<<endl; // Backwards compatibility if(aCurrentGroup == "KDE Desktop Entry") aCurrentGroup = "Desktop Entry"; groupOptionImmutable = fileOptionImmutable; e++; if((e + 2 < eof) && (*e++ == '[') && (*e++ == '$')) // Option follows { if((*e == 'i') && !kde_kiosk_exception) { groupOptionImmutable = true; } } KEntryKey groupKey(aCurrentGroup, 0); KEntry entry = pConfig->lookupData(groupKey); groupSkip = entry.bImmutable; if(groupSkip && !bDefault) continue; entry.bImmutable |= groupOptionImmutable; pConfig->putData(groupKey, entry, false); if(pWriteBackMap) { // add the special group key indicator (*pWriteBackMap)[groupKey] = entry; } continue; } if(groupSkip && !bDefault) goto sktoeol; // Skip entry bool optionImmutable = groupOptionImmutable; bool optionDeleted = false; bool optionExpand = false; const char *endOfKey = 0, *locale = 0, *elocale = 0; for(; (s < eof) && (*s != '\n'); s++) { if(*s == '=') // find the equal sign { if(!endOfKey) endOfKey = s; goto haveeq; } if(*s == '[') // find the locale or options. { const char *option; const char *eoption; endOfKey = s; option = ++s; for(;; s++) { if((s >= eof) || (*s == '\n') || (*s == '=')) { fprintf(stderr, "Invalid entry (missing ']') at %s:%d\n", rFile.name().latin1(), line); goto sktoeol; } if(*s == ']') break; } eoption = s; if(*option != '$') { // Locale if(locale) { fprintf(stderr, "Invalid entry (second locale!?) at %s:%d\n", rFile.name().latin1(), line); goto sktoeol; } locale = option; elocale = eoption; } else { // Option while(option < eoption) { option++; if((*option == 'i') && !kde_kiosk_exception) optionImmutable = true; else if(*option == 'e') optionExpand = true; else if(*option == 'd') { optionDeleted = true; goto haveeq; } else if(*option == ']') break; } } } } fprintf(stderr, "Invalid entry (missing '=') at %s:%d\n", rFile.name().latin1(), line); continue; haveeq: for(endOfKey--;; endOfKey--) { if(endOfKey < startLine) { fprintf(stderr, "Invalid entry (empty key) at %s:%d\n", rFile.name().latin1(), line); goto sktoeol; } if(!isspace(*endOfKey)) break; } const char *st = ++s; while((s < eof) && (*s != '\n')) s++; // Search till end of line / end of file if(locale) { unsigned int cl = static_cast< unsigned int >(elocale - locale); if((ll != cl) || memcmp(locale, localeString.data(), ll)) { // backward compatibility. C == en_US if(cl != 1 || ll != 5 || *locale != 'C' || memcmp(localeString.data(), "en_US", 5)) { // cout<<"mismatched locale '"<<QCString(locale, elocale-locale +1)<<"'"<<endl; // We can ignore this one if(!pWriteBackMap) continue; // We just ignore it // We just store it as is to be able to write it back later. endOfKey = elocale; locale = 0; } } } // insert the key/value line QCString key(startLine, endOfKey - startLine + 2); QCString val = printableToString(st, s - st); // qDebug("found key '%s' with value '%s'", key.data(), val.data()); KEntryKey aEntryKey(aCurrentGroup, decodeKey(key)); aEntryKey.bLocal = (locale != 0); aEntryKey.bDefault = bDefault; KEntry aEntry; aEntry.mValue = val; aEntry.bGlobal = bGlobal; aEntry.bImmutable = optionImmutable; aEntry.bDeleted = optionDeleted; aEntry.bExpand = optionExpand; aEntry.bNLS = (locale != 0); if(pWriteBackMap) { // don't insert into the config object but into the temporary // scratchpad map pWriteBackMap->insert(aEntryKey, aEntry); } else { // directly insert value into config object // no need to specify localization; if the key we just // retrieved was localized already, no need to localize it again. pConfig->putData(aEntryKey, aEntry, false); } } if(fileOptionImmutable) bFileImmutable = true; #ifdef HAVE_MMAP if(map) { munmap((char *)map, rFile.size()); #ifdef SIGBUS sigaction(SIGBUS, &mmap_old_sigact, 0); #endif } #endif }
bool K2sendPlayer::bluePlay (QString * filename) { char buffer[buffer_size]; int bytes_done; int file_len; int bytes_sent; int current_bytes_sent; int rc; int play_length; bool stop = FALSE; unsigned long long now; fd_set rfds, wfds; struct timeval tv; K2sendPlayerCommand *cmd = 0; K2sendPlayerCommand *new_cmd = 0; K2sendStatusEvent *se = 0; TagLib::MPEG::File mp3file (filename->latin1 ()); play_length = mp3file.audioProperties ()->length (); QFile file (*filename); kdDebug (200010) << "K2sendPlayer::bluePlay name=" << *filename << " lenght=" << play_length << endl; setReset (0); setLoudness (loud_filt); setVolume (100 - volume); if (!file.open (IO_ReadOnly)) { QString msg = QString ("Error opening file "); se = new K2sendStatusEvent (K2sendStatusEvent::EventError, msg); //kdDebug(200010) << "post 34" << endl; QApplication::postEvent (m_parent, se); return FALSE; } se = new K2sendStatusEvent (K2sendStatusEvent::EventTitle, *filename); //kdDebug(200010) << "post 35" << endl; QApplication::postEvent (m_parent, se); file_len = file.size (); current_bytes_sent = bytes_sent = 0; last_ticks = start_ticks = getTime (); do { /* check if we can send(write) or receive(read) data */ FD_ZERO (&rfds); FD_SET (blue_sock, &rfds); FD_ZERO (&wfds); FD_SET (blue_sock, &wfds); tv.tv_sec = 3; tv.tv_usec = 0; rc = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); if (rc < 1) { kdDebug (200010) << "K2sendPlayer::bluePlay select()" << endl; QString msg = QString ("Communication error"); se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000); //kdDebug(200010) << "post 36" << endl; QApplication::postEvent (m_parent, se); blueClose (); return FALSE; } /* are we allowed to write? */ if (FD_ISSET (blue_sock, &wfds)) { buffer[0] = K2sendPlayerCommand::Data; if (!(bytes_done = file.readBlock (buffer + 1, sizeof (buffer) - 1))) { if (file.atEnd ()) { kdDebug (200010) << "K2sendPlayer::bluePlay Error reading file " << endl; QString msg = QString ("Error reading file"); se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000); //kdDebug(200010) << "post 37" << endl; QApplication::postEvent (m_parent, se); } } else { if (write (blue_sock, buffer, bytes_done + 1) != bytes_done + 1) { kdDebug (200010) << "K2sendPlayer::bluePlay Error sending" << endl; QString msg = QString ("Error sending data"); se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000); //kdDebug(200010) << "post 38" << endl; QApplication::postEvent (m_parent, se); } } bytes_sent += bytes_done; current_bytes_sent += bytes_done; } if (FD_ISSET (blue_sock, &rfds)) { rc = read (blue_sock, buffer, sizeof (buffer)); kdDebug (200010) << "K2sendPlayer::bluePlay got data " << rc << " " << buffer[0] << " " << buffer[1] << endl; /* handle commands */ switch (buffer[0]) { case K2sendPlayerCommand::Buttons: if ((buffer[1] & 1) == 1) { volume += 10; if (volume > 100) volume = 100; //setVolume(100 - volume,TRUE); kdDebug (200010) << "got cmd volume" << endl; new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Volume, volume); addCommand (new_cmd); } if (buffer[1] & 2) { se = new K2sendStatusEvent (K2sendStatusEvent::EventSkip, NULL, 0); //kdDebug(200010) << "post 39" << endl; kdDebug (200010) << "got cmd skip" << endl; QApplication::postEvent (m_parent, se); new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Skip, 0); addCommand (new_cmd); } if (buffer[1] & 4) { volume -= 10; if (volume < 0) volume = 0; //setVolume(100 - volume,TRUE); kdDebug (200010) << "got cmd volume" << endl; new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Volume, volume); addCommand (new_cmd); } if (buffer[1] & 8) { se = new K2sendStatusEvent (K2sendStatusEvent::EventError, "Battery low", 2000); kdDebug (200010) << "battery low" << endl; QApplication::postEvent (m_parent, se); new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Stop, 0); addCommand (new_cmd); } if (buffer[1] & 16) { new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Loudness, 0); kdDebug (200010) << "got cmd loudness" << endl; addCommand (new_cmd); } break; default: QString msg = QString ("Unknown command from BlueMP3"); se = new K2sendStatusEvent (K2sendStatusEvent::EventMessage, msg, 2000); //kdDebug(200010) << "post 41" << endl; QApplication::postEvent (m_parent, se); break; } } now = getTime (); if ((last_ticks) < now - 250) { updateStatus (bytes_sent, current_bytes_sent, file_len, play_length); last_ticks = now; } cmd = 0; cmd = command (); if (cmd) { switch (cmd->command ()) { case K2sendPlayerCommand::Loudness: if (++loud_filt > 8) loud_filt = 0; setLoudness (loud_filt); delete cmd; cmd = 0; break; case K2sendPlayerCommand::Volume: volume = cmd->value (); setVolume (100 - volume, FALSE); delete cmd; cmd = 0; break; case K2sendPlayerCommand::Play: bytes_done = 0; stop = TRUE; addCommand (cmd); break; case K2sendPlayerCommand::Stop: stop = TRUE; /* forward to global handling */ addCommand (cmd); break; case K2sendPlayerCommand::Skip: stop = TRUE; addCommand (cmd); break; case K2sendPlayerCommand::Length: start_ticks = getTime (); current_bytes_sent = 0; bytes_sent = (file_len / 100) * cmd->value (); file.at (bytes_sent); delete cmd; cmd = 0; break; default: break; } } } while (bytes_sent != file_len && !stop); file.close (); se = new K2sendStatusEvent (K2sendStatusEvent::EventTitle, ""); //kdDebug(200010) << "post 42" << endl; QApplication::postEvent (m_parent, se); this->clearStatus (); /* keep playing */ if (!stop) { kdDebug (200010) << "K2sendPlayer::bluePlay keep playing" << endl; se = new K2sendStatusEvent (K2sendStatusEvent::EventSkip, 0, 0); //kdDebug(200010) << "post 43" << endl; QApplication::postEvent (m_parent, se); new_cmd = new K2sendPlayerCommand (K2sendPlayerCommand::Play, 0); addCommand (new_cmd); } kdDebug (200010) << "K2sendPlayer::bluePlay done" << endl; return TRUE; }
bool CSVData::load(QString filename, QWidget * parent) { QFile file; file.setName(filename); if(!file.open(QIODevice::ReadOnly)) { if(parent) QMessageBox::critical(parent, tr("Open Failed"), tr("Could not open file for reading: %1").arg(qApp->translate("QFile", file.errorString()))); return FALSE; } CSVLoadProgress * progress = 0; QTime time; if(parent) { progress = new CSVLoadProgress(parent, "csv load progress"); progress->setModal(TRUE); progress->_file->setText(filename); progress->_totBytes->setText(QString("%1").arg(file.size())); progress->_readBytes->setText(QString("%1").arg(file.at())); progress->_readRecords->setText("0"); progress->_progressBar->setProgress(file.at(), file.size()); progress->show(); qApp->processEvents(); } time.start(); Q3TextStream in(&file); bool inQuote = FALSE; bool haveText = FALSE; bool peeked = FALSE; QString field = QString::null; QChar c = QChar(); QStringList row = QStringList(); while(!in.atEnd()) { if(progress && time.elapsed() > 200) { qApp->processEvents(); if(!progress->isShown()) { delete progress; return FALSE; // True for False? } progress->_readBytes->setText(QString("%1").arg(file.at())); progress->_readRecords->setText(QString("%1").arg(rows())); progress->_progressBar->setProgress(file.at()); time.restart(); } if(peeked) peeked = FALSE; else in >> c; // If we are inside a quoted string we handle // everything differently if(inQuote) { if('"' == c) { in >> c; if('"' == c) field += c; else { peeked = TRUE; inQuote = FALSE; } } else field += c; }
//-------------------------------------------------------------------------- // Parse the marker stream until SOS or EOI is seen; //-------------------------------------------------------------------------- int ExifData::ReadJpegSections (QFile & infile, ReadMode_t ReadMode) { int a; a = infile.getch(); if (a != 0xff || infile.getch() != M_SOI) { SectionsRead = 0; return false; } for(SectionsRead = 0; SectionsRead < MAX_SECTIONS-1; ) { int marker = 0; int got; unsigned int ll,lh; unsigned int itemlen; uchar * Data; for (a=0; a<7; a++) { marker = infile.getch(); if (marker != 0xff) break; if (a >= 6) { kdDebug(7034) << "too many padding bytes\n"; return false; } } if (marker == 0xff) { // 0xff is legal padding, but if we get that many, something's wrong. throw FatalError("too many padding bytes!"); } Sections[SectionsRead].Type = marker; // Read the length of the section. lh = (uchar) infile.getch(); ll = (uchar) infile.getch(); itemlen = (lh << 8) | ll; if (itemlen < 2) { throw FatalError("invalid marker"); } Sections[SectionsRead].Size = itemlen; Data = (uchar *)malloc(itemlen+1); // Add 1 to allow sticking a 0 at the end. Sections[SectionsRead].Data = Data; // Store first two pre-read bytes. Data[0] = (uchar)lh; Data[1] = (uchar)ll; got = infile.readBlock((char*)Data+2, itemlen-2); // Read the whole section. if (( unsigned ) got != itemlen-2) { throw FatalError("reading from file"); } SectionsRead++; switch(marker) { case M_SOS: // stop before hitting compressed data // If reading entire image is requested, read the rest of the data. if (ReadMode & READ_IMAGE) { unsigned long size; size = kMax( 0ul, infile.size()-infile.at() ); Data = (uchar *)malloc(size); if (Data == NULL) { throw FatalError("could not allocate data for entire image"); } got = infile.readBlock((char*)Data, size); if (( unsigned ) got != size) { throw FatalError("could not read the rest of the image"); } Sections[SectionsRead].Data = Data; Sections[SectionsRead].Size = size; Sections[SectionsRead].Type = PSEUDO_IMAGE_MARKER; SectionsRead ++; //HaveAll = 1; } return true; case M_EOI: // in case it's a tables-only JPEG stream kdDebug(7034) << "No image in jpeg!\n"; return false; case M_COM: // Comment section // pieczy 2002-02-12 // now the User comment goes to UserComment // so we can store a Comment section also in READ_EXIF mode process_COM(Data, itemlen); break; case M_JFIF: // Regular jpegs always have this tag, exif images have the exif // marker instead, althogh ACDsee will write images with both markers. // this program will re-create this marker on absence of exif marker. // hence no need to keep the copy from the file. free(Sections[--SectionsRead].Data); break; case M_EXIF: // Seen files from some 'U-lead' software with Vivitar scanner // that uses marker 31 for non exif stuff. Thus make sure // it says 'Exif' in the section before treating it as exif. if ((ReadMode & READ_EXIF) && memcmp(Data+2, "Exif", 4) == 0) { process_EXIF((uchar *)Data, itemlen); // FIXME: This call // requires Data to be array of at least 8 bytes. Code // above only checks for itemlen < 2. } else { // Discard this section. free(Sections[--SectionsRead].Data); } break; case M_SOF0: case M_SOF1: case M_SOF2: case M_SOF3: case M_SOF5: case M_SOF6: case M_SOF7: case M_SOF9: case M_SOF10: case M_SOF11: case M_SOF13: case M_SOF14: case M_SOF15: process_SOFn(Data, marker); //FIXME: This call requires Data to // be array of at least 8 bytes. Code above only checks for // itemlen < 2. break; default: break; } } return true; }