Esempio n. 1
0
/**
 * Insert a file at line @p line in the buffer.
 */
void
KWBuffer::insertFile(int line, const QString &file, QTextCodec *codec)
{
    if (line) {
        odebug << "insert File only supports insertion at line 0 == file opening" << oendl;
        return;
    }
    clear();
    QFile iofile(file);
    if (!iofile.open(IO_ReadOnly)) {
	owarn << "failed to open file " << iofile.name() << oendl;
	return;
    }
    QTextStream stream(&iofile);
    stream.setCodec(codec);
    QString qsl;
    int count=0;
    for (count=0;((qsl=stream.readLine())!=QString::null);  count++)
    {
        if (count==0)
        {
            (*m_stringListIt)->append(qsl.unicode(),qsl.length());
        }
        else
        {
            TextLine::Ptr tl=new TextLine();
            tl ->append(qsl.unicode(),qsl.length());
            m_stringListIt=m_stringList.append(tl);
        }
    }
    if (count!=0)
    {
        m_stringListCurrent=count-1;
        m_lineCount=count;
    }
}
Esempio n. 2
0
// Program's main thread of execution.
// ----------------------------------------------------------- 
int main(int argc,   // Number of strings in array argv.
	 char *argv[], // Array of command-line argument strings.
	 char *envp[]) // Array of environment variable strings.
// NOTE: None of the MSVC compilers will expand wildcard characters
// used in command-line arguments unless linked with the setargv.obj
// library. All the UNIX compliers will expand wildcard characters
// by default.
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  // If no arguments are given print usage message to the screen 
  if(argc < 2) {
    HelpMessage();
    return 0;
  }

  // Process command ling arguments and files 
  int narg;
  char *arg = argv[narg = 1];
  while (narg < argc) {
    if (arg[0] != '\0') {
      if (arg[0] == '-') { // Look for command line arguments
	if(!ProcessArgs(arg)) return 0; // Exit if argument is not valid
      }
      else { 
	if(futils_isdirectory((const char *)arg)) {
	  // Do not process directories
	  arg = argv[++narg];
	  continue;
	}
	open_file = arg; // Update the open file name pointer
	DiskFileB iofile(open_file);
	if(!iofile) { // Skip over directory names
	  if(narg < argc) {
	    arg = argv[++narg];
	    iofile.df_Close();
	    continue; 
	  }
	  else { // Cannot open the specified file
	    GXSTD::cerr << "\n";
	    GXSTD::cerr << "Cannot open file: " << open_file << "\n";
	    GXSTD::cerr << "Exiting..." << "\n";
	    GXSTD::cerr << "\n";
	    return 0;
	  }
	}
	num_files++;

	if(replace_section) {
	  num_matches = 0; num_replaced = 0; num_processed = 0;
	  if(!ReplaceBySection(iofile, GXSTD::cout)) return 0;
	  arg = argv[++narg];
	  if(num_files) iofile.df_Close();
	  continue;
	}

	// Echo the name of the file and search operation
	// ----------------------------------------------------------- 
	if(replacing_string == 1) {
	  if(string_to_insert.is_null()) {
	    GXSTD::cerr << "\n";
	    GXSTD::cerr << "The -R option must be used with the -W option." 
			<< "\n";
	    GXSTD::cerr << "Usage: -R\"string\" -W\"replacement\"" << "\n";
	    GXSTD::cerr << "\n";
	    return 0;
	  }
	  num_processed = 0; // Number of string processed for this file
	  GXSTD::cout << "\n";
	  GXSTD::cout << "Opening file: " << open_file << "\n";
	  GXSTD::cout << "Replacing: " << string_to_replace.c_str() << "\n";
	  GXSTD::cout << "With: " << string_to_insert.c_str() << "\n";
	}

	if(replacing_line) {
	  if(string_to_insert.is_null()) {
	    GXSTD::cerr << "\n";
	    GXSTD::cerr << "The -L option must be used with the -W option." 
			<< "\n";
	    GXSTD::cerr << "Usage: -L\"string\" -W\"replacement\"" << "\n";
	    return 0;
	  }
	  num_processed = 0; // Number of string processed for this file
	  GXSTD::cout << "\n";
	  GXSTD::cout << "Opening file: " << open_file << "\n";
	  GXSTD::cout << "Replacing line after: " << string_to_replace.c_str() << "\n";
	  GXSTD::cout << "With: " << string_to_insert.c_str() << "\n";
	}

	if(inserting_string ) {
	  if(string_to_insert.is_null()) {
	    GXSTD::cerr << "\n";
	    GXSTD::cerr << "The -I option must be used with the -W option." 
			<< "\n";
	    GXSTD::cerr << "Usage: -I\"string\" -W\"insert\"" << "\n";
	    GXSTD::cerr << "\n";
	    return 0;
	  }
	  num_processed = 0; // Number of string processed for this file
	  GXSTD::cout << "\n";
	  GXSTD::cout << "Opening file: " << open_file << "\n";
	  GXSTD::cout << "Inserting: " << string_to_replace.c_str() << "\n";
	  GXSTD::cout << "After: " << string_to_insert.c_str() << "\n";
	}

	if(find_string) {
	  num_processed = 0; // Number of string processed for this file
	  // Do not echo file status to the console
	}

	// Process the test file
	num_matches = 0; num_replaced = 0; num_processed = 0;
	ProcessTextFile(iofile, GXSTD::cout);
	if(num_files) iofile.df_Close();
	
	// Report the total number of strings replaced/inserted 
	// ----------------------------------------------------------- 
	if(replacing_string) {
	  GXSTD::cout << "Replaced " << num_processed << " occurrence(s)." 
		      << "\n";
	  GXSTD::cout << "\n";
	}

	if(replacing_line) {
	  GXSTD::cout << "Replaced " << num_processed << " occurrence(s)." 
		      << "\n";
	  GXSTD::cout << "\n";
	}

	if(inserting_string ) {
	  GXSTD::cout << "Inserted " << num_processed << " line(s)." << "\n";
	  GXSTD::cout << "\n";
	}

	if(find_string) {
	  if(find_all) {
	    if(num_processed > 0) {
	      GXSTD::cout << "\n";
	      GXSTD::cout << "File name: " << open_file << " String: " 
			  << string_to_find.c_str() << "\n";
	      if(num_processed > 1) 
		GXSTD::cout << "Found " << num_processed << " matches" << "\n";
	      else
		GXSTD::cout << "Found 1 match" << "\n";
	    }
	  }
	  if(num_matches == 0) { // Used by -f command if search fails
	    // Do not echo results to the console
	  }
	}
      }
      arg = argv[++narg];
    }
  }
  
  if(num_files == 0) {
    GXSTD::cerr << "\n";
    GXSTD::cerr << "You must enter a file name." << "\n";
    GXSTD::cerr << "Exiting..." << "\n";
    GXSTD::cerr << "\n";
    return 0;
  }
  
  // Return the total number of strings replaced/inserted 
  // ----------------------------------------------------------- 
  if(replacing_string) return num_replaced;
  if(replacing_line) return num_replaced;
  if(inserting_string ) return num_replaced;
  if(find_string) return num_matches;

  return 0;
}