static void setDisassemblyCode(DissassemblyData* data, PDReader* reader) { PDReaderIterator it; if (PDRead_find_array(reader, &it, "disassembly", 0) == PDReadStatus_NotFound) return; while (PDRead_get_next_entry(reader, &it)) { uint64_t address; const char* text; PDRead_find_u64(reader, &address, "address", it); PDRead_find_string(reader, &text, "line", it); insertLine(data, address, text); } }
bool DBTools::insertLines(INOUT std::vector<Line>& vLine) { int lineid = 0; std::vector<Line> vLineTemp = vLine; std::vector<Line>::iterator vLineIter = vLineTemp.begin(); while(vLineIter != vLineTemp.end()) { lineid = insertLine(*vLineIter); if(lineid == 0) return false; vLineIter++; } return true; }
int DBTools::insertSurface(INOUT Surface& surface) { std::string sql="insert into surface values();"; mysql_query(&myCont,sql.c_str()); int surfaceId = mysql_insert_id(&myCont); std::vector<SurfaceBoundary> vSurBoundTemp = surface.getSurfaceBoundaries(); std::vector<SurfaceBoundary>::iterator vsfbIter = vSurBoundTemp.begin(); while(vsfbIter != vSurBoundTemp.end()) { int lineid = insertLine(*vsfbIter); std::stringstream ss_lineid; std::stringstream ss_surfaceid; std::stringstream ss_imput; std::stringstream ss_closure; ss_lineid<<lineid; ss_surfaceid<<surfaceId; ss_imput<<vsfbIter->isImputed(); ss_closure<<vsfbIter->isClosure(); std::string sql="insert into surface_boundaries values("+ss_lineid.str()+ " , " + ss_surfaceid.str()+ " , " + ss_imput.str()+ " ," + ss_closure.str()+ ");"; mysql_query(&myCont,sql.c_str()); std::vector<int> vOrgTemp = vsfbIter->getOrigin(); std::vector<int>::iterator vOrgItor = vOrgTemp.begin(); while(vOrgItor != vOrgTemp.end()) { std::stringstream ss_lineid; std::stringstream ss_paintid; ss_lineid<<lineid; ss_paintid<<*vOrgItor; std::string sql="insert into line_origin values("+ss_lineid.str()+"," + ss_paintid.str()+ ");"; mysql_query(&myCont,sql.c_str()); vOrgItor++; } vsfbIter++; } surface.setId(surfaceId); return surfaceId; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // DccCanvasWidget // DccCanvasWidget::DccCanvasWidget(QWidget * par) : QWidget(par,"canvas_widget") { m_pCanvas = new QCanvas(this); //#warning "Make this size as parameter of Dcc ?" m_pCanvas->resize(648,480); m_pMenuBar = new QMenuBar(this); m_pSplitter = new QSplitter(QSplitter::Horizontal,this); m_pCanvasView = new KviCanvasView(m_pCanvas,this,m_pSplitter); m_pStatusLabel = new QLabel(this); m_pPropertiesWidget = new KviCanvasItemPropertiesWidget(m_pSplitter); QValueList<int> l; l.append(80); l.append(20); m_pSplitter->setSizes(l); connect(m_pPropertiesWidget,SIGNAL(propertyChanged(const QString &,const QVariant &)),m_pCanvasView,SLOT(propertyChanged(const QString &,const QVariant &))); QMenu * add = new QMenu(m_pMenuBar); QMenu * shapes = new QMenu(add); QMenu * polygons = new QMenu(add); QMenu * items = new QMenu(add); shapes->addAction(__tr2qs_ctx("&Line","dcc"),m_pCanvasView,SLOT(insertLine())); shapes->addAction(__tr2qs_ctx("&Rectangle","dcc"),m_pCanvasView,SLOT(insertRectangle())); shapes->addAction(__tr2qs_ctx("&Ellipse","dcc"),m_pCanvasView,SLOT(insertEllipse())); shapes->addAction(__tr2qs_ctx("&Pie","dcc"),m_pCanvasView,SLOT(insertPie())); shapes->addAction(__tr2qs_ctx("&Chord","dcc"),m_pCanvasView,SLOT(insertChord())); items->addAction(__tr2qs_ctx("&Rich text (html)","dcc"),m_pCanvasView,SLOT(insertRichText())); polygons->addAction(__tr2qs_ctx("&Triangle","dcc"),m_pCanvasView,SLOT(insertPolygonTriangle())); polygons->addAction(__tr2qs_ctx("&Rectangle","dcc"),m_pCanvasView,SLOT(insertPolygonRectangle())); polygons->addAction(__tr2qs_ctx("&Pentagon","dcc"),m_pCanvasView,SLOT(insertPolygonPentagon())); polygons->addAction(__tr2qs_ctx("&Hexagon","dcc"),m_pCanvasView,SLOT(insertPolygonHexagon())); add->addAction(__tr2qs_ctx("&Shape","dcc"),shapes); add->addAction(__tr2qs_ctx("&Item","dcc"),items); add->addAction(__tr2qs_ctx("&Polygons","dcc"),polygons); m_pMenuBar->addAction(__tr2qs_ctx("&Insert","dcc"),add); }
/* * Add lines which are typed in by the user. * The lines are inserted just before the specified line number. * The lines are terminated by a line containing a single dot (ugly!), * or by an end of file. */ static void addLines(int num) { int len; char buf[USERSIZE + 1]; while (1) { /* Returns: * -1 on read errors or EOF, or on bare Ctrl-D. * 0 on ctrl-C, * >0 length of input string, including terminating '\n' */ len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1); if (len <= 0) { /* Previously, ctrl-C was exiting to shell. * Now we exit to ed prompt. Is in important? */ return; } if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0')) return; if (!insertLine(num++, buf, len)) return; } }
int insertFile(Ndb* pNdb, const NdbDictionary::Table* pTab, const char* fileName){ const int MAX_LINE_LEN = 8000; char line[MAX_LINE_LEN]; int lineNo = 0; FILE* instr = fopen(fileName, "r"); if (instr == NULL){ ndbout << "Coul'd not open " << fileName << endl; return -1; } while(fgets(line, MAX_LINE_LEN, instr)){ lineNo++; if (line[strlen(line)-1] == '\n') { line[strlen(line)-1] = '\0'; } int check = insertLine(pNdb, pTab, line); if (check == -2){ ndbout << "Wrong format in input data file, line: " << lineNo << endl; fclose(instr); return -1; } if (check == -1){ fclose(instr); return -1; } } fclose(instr); return 0; }
/* * Read lines from a file at the specified line number. * Returns TRUE if the file was successfully read. */ static int readLines(const char *file, int num) { int fd, cc; int len, lineCount, charCount; char *cp; if ((num < 1) || (num > lastNum + 1)) { bb_error_msg("bad line for read"); return FALSE; } fd = open(file, 0); if (fd < 0) { bb_simple_perror_msg(file); return FALSE; } bufPtr = bufBase; bufUsed = 0; lineCount = 0; charCount = 0; cc = 0; printf("\"%s\", ", file); fflush_all(); do { cp = memchr(bufPtr, '\n', bufUsed); if (cp) { len = (cp - bufPtr) + 1; if (!insertLine(num, bufPtr, len)) { close(fd); return FALSE; } bufPtr += len; bufUsed -= len; charCount += len; lineCount++; num++; continue; } if (bufPtr != bufBase) { memcpy(bufBase, bufPtr, bufUsed); bufPtr = bufBase + bufUsed; } if (bufUsed >= bufSize) { len = (bufSize * 3) / 2; cp = xrealloc(bufBase, len); bufBase = cp; bufPtr = bufBase + bufUsed; bufSize = len; } cc = safe_read(fd, bufPtr, bufSize - bufUsed); bufUsed += cc; bufPtr = bufBase; } while (cc > 0); if (cc < 0) { bb_simple_perror_msg(file); close(fd); return FALSE; } if (bufUsed) { if (!insertLine(num, bufPtr, bufUsed)) { close(fd); return -1; } lineCount++; charCount += bufUsed; } close(fd); printf("%d lines%s, %d chars\n", lineCount, (bufUsed ? " (incomplete)" : ""), charCount); return TRUE; }
bool KileScriptDocument::insertLine(const QString &s) { return insertLine(m_view->cursorPosition().line(), s); }
void H19::processCharacter(char ch) { // mask off the high bit just in case, the real H19 would not have it set. ch &= 0x7f; if (mode_m == Normal) { switch (ch) { case ascii::NUL: case ascii::SOH: case ascii::STX: case ascii::ETX: case ascii::EOT: case ascii::ENQ: case ascii::ACK: case ascii::VT: case ascii::FF: case ascii::SO: case ascii::SI: case ascii::DLE: case ascii::DC1: case ascii::DC2: case ascii::DC3: case ascii::DC4: case ascii::NAK: case ascii::SYN: case ascii::ETB: case ascii::EM: case ascii::SUB: case ascii::FS: case ascii::GS: case ascii::RS: case ascii::US: case ascii::DEL: // From manual, these characters are not processed by the terminal break; case ascii::BEL: // Rings the bell. /// \todo - implement ringing bell. consoleLog("<BEL>"); break; case ascii::BS: // Backspace consoleLog("<BS>"); processBS(); break; case ascii::HT: // Horizontal Tab consoleLog("<TAB>"); processTAB(); break; case ascii::LF: // Line Feed processLF(); if (autoCR_m) { processCR(); } consoleLog("\n"); break; case ascii::CR: // Carriage Return processCR(); if (autoLF_m) { processLF(); } break; case ascii::CAN: // Cancel. break; case ascii::ESC: // Escape mode_m = Escape; consoleLog("<ESC>"); break; default: // if Printable character display it. #if CONSOLE_LOG fprintf(console_out, "%c", ch); #endif displayCharacter(ch); break; } } else if (mode_m == Escape) { // Assume we go back to Normal, so only the few that don't need to set the mode. mode_m = Normal; switch (ch) { case ascii::CAN: // CAN - Cancel // return to Normal mode, already set. break; case ascii::ESC: // Escape // From the ROM listing, stay in this mode. mode_m = Escape; break; // Cursor Functions case 'H': // Cursor Home posX_m = posY_m = 0; updated_m = true; break; case 'C': // Cursor Forward cursorForward(); break; case 'D': // Cursor Backward processBS(); // same processing as cursor backward break; case 'B': // Cursor Down cursorDown(); break; case 'A': // Cursor Up cursorUp(); break; case 'I': // Reverse Index reverseIndex(); break; case 'n': // Cursor Position Report cursorPositionReport(); break; case 'j': // Save cursor position saveCursorPosition(); break; case 'k': // Restore cursor position restoreCursorPosition(); break; case 'Y': // Direct Cursor Addressing mode_m = DCA_1; break; // Erase and Editing case 'E': // Clear Display clearDisplay(); break; case 'b': // Erase Beginning of Display eraseBOD(); break; case 'J': // Erase to End of Page eraseEOP(); break; case 'l': // Erase entire Line eraseEL(); break; case 'o': // Erase Beginning Of Line eraseBOL(); break; case 'K': // Erase To End Of Line eraseEOL(); break; case 'L': // Insert Line insertLine(); break; case 'M': // Delete Line deleteLine(); break; case 'N': // Delete Character deleteChar(); break; case '@': // Enter Insert Character Mode insertMode_m = true; break; case 'O': // Exit Insert Character Mode insertMode_m = false; break; // Configuration case 'z': // Reset To Power-Up Configuration reset(); break; case 'r': // Modify the Baud Rate /// \todo - determine if we should support this. debugss(ssH19, ERROR, "Error Unimplemented Modify Baud\n"); break; case 'x': // Set Mode mode_m = SetMode; break; case 'y': // Reset Mode mode_m = ResetMode; break; case '<': // Enter ANSI Mode /// \todo - implement ANSI mode. // ROM - just sets the mode debugss(ssH19, ERROR, "Error Entering ANSI mode - unsupported\n"); break; // Modes of operation case '[': // Enter Hold Screen Mode holdScreen_m = true; break; case '\\': // Exit Hold Screen Mode holdScreen_m = false; break; case 'p': // Enter Reverse Video Mode reverseVideo_m = true; break; case 'q': // Exit Reverse Video Mode reverseVideo_m = false; break; case 'F': // Enter Graphics Mode graphicMode_m = true; break; case 'G': // Exit Graphics Mode graphicMode_m = false; break; case 't': // Enter Keypad Shifted Mode keypadShifted_m = true; break; case 'u': // Exit Keypad Shifted Mode // ROM - just sets the mode keypadShifted_m = false; break; case '=': // Enter Alternate Keypad Mode // ROM - just sets the mode keypadShifted_m = true; break; case '>': // Exit Alternate Keypad Mode // ROM - just sets the mode keypadShifted_m = false; break; // Additional Functions case '}': // Keyboard Disable /// \todo - determine whether to do this. keyboardEnabled_m = false; break; case '{': // Keyboard Enable keyboardEnabled_m = true; break; case 'v': // Wrap Around at End Of Line wrapEOL_m = true; break; case 'w': // Discard At End Of Line wrapEOL_m = false; break; case 'Z': // Identify as VT52 (Data: ESC / K) debugss(ssH19, ERROR, "Identify request\n"); sendData(ascii::ESC); sendData('/'); sendData('K'); break; case ']': // Transmit 25th Line transmitLine25(); break; case '#': // Transmit Page transmitPage(); break; default: debugss(ssH19, WARNING, "Unhandled ESC: %d\n", ch); /// \todo - verify this is how the H19 ROM does it. break; } } else if (mode_m == SetMode) { mode_m = Normal; switch (ch) { case '1': // Enable 25th line // From the ROM, it erases line 25 on the enable, but here we erase on the disable. line25_m = true; break; case '2': // No key click keyClick_m = true; break; case '3': // Hold screen mode holdScreen_m = true; break; case '4': // Block Cursor cursorBlock_m = true; updated_m = true; break; case '5': // Cursor Off cursorOff_m = true; updated_m = true; break; case '6': // Keypad Shifted keypadShifted_m = true; break; case '7': // Alternate Keypad mode altKeypadMode_m = true; break; case '8': // Auto LF autoLF_m = true; break; case '9': // Auto CR autoCR_m = true; break; default: /// \todo Need to process ch as if none of this happened... debugss(ssH19, WARNING, "Invalid set Mode: %c\n", ch); break; } } else if (mode_m == ResetMode) { mode_m = Normal; switch (ch) { case '1': // Disable 25th line eraseLine(rowsMain_c); line25_m = false; updated_m = true; break; case '2': // key click keyClick_m = false; break; case '3': // Hold screen mode holdScreen_m = false; break; case '4': // Block Cursor cursorBlock_m = false; updated_m = true; break; case '5': // Cursor On cursorOff_m = false; updated_m = true; break; case '6': // Keypad Unshifted keypadShifted_m = false; break; case '7': // Exit Alternate Keypad mode altKeypadMode_m = false; break; case '8': // No Auto LF autoLF_m = false; break; case '9': // No Auto CR autoCR_m = false; break; default: /// \todo Need to process ch as if none of this happened... debugss(ssH19, WARNING, "Invalid reset Mode: %c\n", ch); break; } } else if (mode_m == DCA_1) { // From actual H19, once the line is specified, the cursor // immediately moves to that line, so no need to save the // position and wait for the entire command. /// \todo verify that this is the same on newer H19s. if (ch == ascii::CAN) { // cancel mode_m = Normal; } else { // \todo handle error conditions int pos = ch - 31; // verify valid Position if (((pos > 0) && (pos < (signed) rows_c)) || ((pos == (signed) rows_c) && (line25_m))) { posY_m = pos - 1; } else { /// \todo check to see how a real h19 handles this. debugss(ssH19, INFO, "DCA invalid Y: %d\n", pos); } mode_m = DCA_2; } } else if (mode_m == DCA_2) { if (ch == ascii::CAN) { // cancel mode_m = Normal; } else { int pos = ch - 31; if ((pos > 0) && (pos < 81)) { posX_m = pos - 1; } else { posX_m = (cols_c - 1); } updated_m = true; mode_m = Normal; } } }
int main(){ int option; do{ printMenuOptions(); scanf("%d",&option); switch(option) { case 0://Exit break; case 1://formula değerlendirme { printf("\nLutfen tablonun degeri giriniz :\n"); printf("\t \"default\" de girdiribilirsiniz :\n>"); scanf("%s",girdi); if(strcmp(girdi,"default")==0) strcpy(girdi,"4,3,10,34,37,=A1+B1+C1,40,17,34,=A2+B2+C2,=A1+A2,=B1+B2,=C1+C2,=D1+D2"); createTableFromString(girdi); calculateAndPrint(); printf("\n\n\n"); break; } case 2://satir ekleme { if(Height==MAX_ROWS){ printf("\nDaha fazla bir satir eklenmez, bir tane satir silerek yeni bir satir ekleyebilirsiniz"); break; } int newLineIndex; do{ printf("\nLutfen ekleyeceksin satir hangi satirdan sonra giriniz (0-%d) arasinda> ",Height); scanf("%d",&newLineIndex); if(newLineIndex>Height){ printf("\nSu kadar satirlar zaten yok, lutfen %d dan daha az degerler giriniz",Height); } if(newLineIndex<0){ printf("\nSifir yada sifirdan daha buyuk bir sayi giriniz."); } }while((newLineIndex>Height) || (newLineIndex<0)); //Bir satır okuyoruz kullancıdan char newLine[MAX_COLUMNS][MAX_CHARS] ; getLineContent(newLine); //okuduğumuz satır matrise ekliyoruz insertLine(inputTable,newLineIndex,newLine); //Bizim boyutlarımız güncelliyoruz Height++; //Yeniden hesaplıyoruz ve ekrende yazıyoruz calculateAndPrint(); break; } case 3://satir silme { int lineToRemoveIndex; do{ printf("\nLutfen sileceksiniz satirin indexi giriniz (1-%d) arasinda>",Height); scanf("%d",&lineToRemoveIndex); //lineToRemoveIndex--;//Because 0-based index if((lineToRemoveIndex>Height) || (lineToRemoveIndex<=0)){ printf("\nDogru bir satir indexi giriniz lutfen.\n"); } }while((lineToRemoveIndex>Height) || (lineToRemoveIndex<=0)); //Bu satır siliyoruz removeLine(inputTable, lineToRemoveIndex-1);//Because 0-based index //Boyutlarımız güncelliyoruz Height--; //Yeniden hesaplıyoruz ve ekrende yazıyoruz calculateAndPrint(); break; } case 4://Hucre degeri degiştirme { int i,j; //doğru bir hücre okuyacağız kadar yeni bir hücre yeri okuyoruz do{ readCoordinates(&i,&j); if((i>Height) || (i<0) || (j>Width) || (j<0)){ printf("\nBoyle bir hucre bulunmadi. Lutfen yeniden giriniz."); } }while((i>Height) || (i<0) || (j>Width) || (j<0)); char cell[MAX_CHARS]; printf("\nLutfen hucrenin yeni degeri giriniz>"); //Bu hücrenin değeri okuyoruz scanf("%s",cell); //doğru yerinde koyuyoruz strcpy( (*(inputTable+i))+j,cell); //Yeniden hesaplıyoruz ve ekrende yazıyoruz calculateAndPrint(); break; } case 5://Filterleme { int filter; printf("\nHangi sayi'ye gore filterleyeceksiniz>"); scanf("%d",&filter); printFilteredArray(filter); break; } } }while(option!=0);//Kullancı çıkma seçenek seçecek kadar bu menü yazıyoruz ve soruyoruz return 0; }
/* antlarr: KDE 4: make it const QString & */ int KEdit::doReplace(QString s_pattern, bool case_sensitive, bool wildcard, bool forward, int line, int col, bool replace_all){ (void) wildcard; // reserved for possible extension to regex int line_counter, length; int pos = -1; QString string; QString stringnew; QString replacement; replacement = replace_dialog->getReplaceText(); line_counter = line; replace_all_col = col; if(forward){ int num_lines = numLines(); while (line_counter < num_lines){ string = textLine(line_counter); if (replace_all){ pos = string.find(s_pattern, replace_all_col, case_sensitive); } else{ pos = string.find(s_pattern, line_counter == line ? col : 0, case_sensitive); } if (pos == -1 ){ line_counter++; replace_all_col = 0; replace_all_line = line_counter; } if( pos != -1){ length = s_pattern.length(); if(replace_all){ // automatic stringnew = string.copy(); do { stringnew.replace(pos,length,replacement); replace_all_col = pos + replacement.length(); replace_all_line = line_counter; pos = stringnew.find(s_pattern, replace_all_col, case_sensitive); } while( pos != -1); removeLine(line_counter); insertLine(stringnew,line_counter); setModified(true); } else{ // interactive setCursorPosition( line_counter , pos, false ); for(int l = 0 ; l < length; l++){ cursorRight(true); } setCursorPosition( line_counter , pos + length, true ); pattern = s_pattern; last_replace = FORWARD; can_replace = true; return 1; } } } } else{ // searching backwards while(line_counter >= 0){ string = textLine(line_counter); int line_length = string.length(); if( replace_all ){ if (replace_all_col < 0) pos = -1; else pos = string.findRev(s_pattern, replace_all_col , case_sensitive); } else{ if ((line == line_counter) && (col < 0)) pos = -1; else pos = string.findRev(s_pattern, line == line_counter ? col : line_length , case_sensitive); } if (pos == -1 ){ line_counter--; replace_all_col = 0; if(line_counter >= 0){ string = textLine(line_counter); replace_all_col = string.length(); } replace_all_line = line_counter; } if (pos != -1){ length = s_pattern.length(); if(replace_all){ // automatic stringnew = string.copy(); stringnew.replace(pos,length,replacement); removeLine(line_counter); insertLine(stringnew,line_counter); replace_all_col = pos-length; replace_all_line = line_counter; if (replace_all_col < 0) { line_counter--; if(line_counter >= 0){ string = textLine(line_counter); replace_all_col = string.length(); } replace_all_line = line_counter; } setModified(true); } else{ // interactive // printf("line_counter %d pos %d col %d\n",line_counter, pos,col); if( ! (line == line_counter && pos > col ) ){ setCursorPosition(line_counter, pos + length ,false ); for(int l = 0 ; l < length; l++){ cursorLeft(true); } setCursorPosition(line_counter, pos ,true ); pattern = s_pattern; last_replace = BACKWARD; can_replace = true; return 1; } } } } } return 0; }
void insertEntry(char * key , char * value , File * file){ const char * content = concatAll(3 , key , "=" , value); insertLine(content, file); }
/* Reads from stdin until EOF growing the buffer as needed */ void nonInteractive_appendMessage( MESSAGE *msg ) { /* Loop to the end of the message */ msg = msg->root; for ( ; msg->next; msg = msg->next ) ; /* Allocate and move to new node */ msg->next = list_getNode( msg ); msg = msg->next; /* Get and set path and time information */ list_setPath( msg ); list_setTime( msg ); int ch, lineLen, totChars, numLines, buffSize; ch = lineLen = totChars = numLines = 0; char *tmp, *buffer; tmp = buffer = NULL; buffSize = 1024; buffer = malloc( buffSize * sizeof(char) ); if ( !buffer ) { fprintf( stderr, "Failed to allocate memory for message buffer in nonInteractive_appendMessage\n" ); exit( 1 ); } LINE *line, *prev; line = prev = NULL; line = line_getLine(); while ( ( ch = getchar() ) != EOF ) { /* If we've outgrown the buffer then allocate some more memory */ if ( lineLen >= buffSize ) { buffSize *= 2; tmp = realloc( buffer, buffSize * sizeof(char *) ); if ( !tmp ) { fprintf( stderr, "Failed to allocate %d bytes in nonInteractive_appendMessage\n", buffSize ); exit( 1 ); } else { buffer = tmp; } } if ( ch != '\n' && ch != '\0' ) { buffer[lineLen] = ch; lineLen++; } else { numLines++; buffer[lineLen] = 0; /* You pass buffer + lineLen because insertLine expects the pointer to be at the end of the string */ insertLine( &line, buffer + lineLen, lineLen, numLines ); if ( numLines == 1 ) { msg->first = line; } /* Set and update the prev pointer */ line->prev = prev; prev = line; /* Get a new line and move to it */ line->next = line_getLine(); line = line->next; totChars += lineLen + 1; lineLen = 0; } } /* Update MESSAGE statistics for this message */ msg->last = line->prev; msg->numChars = totChars; msg->numLines = numLines; msg->messageNum = msg->root->totalMessages + 1; msg->root->totalMessages++; free( buffer ); }
/* * Read lines from a file at the specified line number. * Returns TRUE if the file was successfully read. */ static BOOL readLines(const char * file, NUM num) { int fd; int cc; LEN len; LEN lineCount; LEN charCount; char * cp; if ((num < 1) || (num > lastNum + 1)) { fprintf(stderr, "Bad line for read\n"); return FALSE; } fd = open(file, 0); if (fd < 0) { perror(file); return FALSE; } bufPtr = bufBase; bufUsed = 0; lineCount = 0; charCount = 0; cc = 0; printf("\"%s\", ", file); fflush(stdout); do { if (intFlag) { printf("INTERRUPTED, "); bufUsed = 0; break; } cp = memchr(bufPtr, '\n', bufUsed); if (cp) { len = (cp - bufPtr) + 1; if (!insertLine(num, bufPtr, len)) { close(fd); return FALSE; } bufPtr += len; bufUsed -= len; charCount += len; lineCount++; num++; continue; } if (bufPtr != bufBase) { memcpy(bufBase, bufPtr, bufUsed); bufPtr = bufBase + bufUsed; } if (bufUsed >= bufSize) { len = (bufSize * 3) / 2; cp = realloc(bufBase, len); if (cp == NULL) { fprintf(stderr, "No memory for buffer\n"); close(fd); return FALSE; } bufBase = cp; bufPtr = bufBase + bufUsed; bufSize = len; } cc = read(fd, bufPtr, bufSize - bufUsed); bufUsed += cc; bufPtr = bufBase; } while (cc > 0); if (cc < 0) { perror(file); close(fd); return FALSE; } if (bufUsed) { if (!insertLine(num, bufPtr, bufUsed)) { close(fd); return -1; } lineCount++; charCount += bufUsed; } close(fd); printf("%d lines%s, %d chars\n", lineCount, (bufUsed ? " (incomplete)" : ""), charCount); return TRUE; }