Example #1
0
File: INIFile.C Project: HeyJJ/ball
	bool INIFile::appendLine(const String& sectionName, const String& line)
	{
		String section_name(sectionName);

		if (section_name == "")
		{
			SectionIterator it(sections_.end());
			it--;
			section_name = it->getName();
		}

		if (!hasSection(section_name) || line[0] == '[')
		{
      Log.error() << "In INIFile " << filename_ << " , error while appending line: "
                  << line << " . Illegal section-name: " << sectionName << endl;
			return false;
		}
		
		Section& section(*getSection(section_name));

		// key?
    if (line.hasSubstring("=", 1))
    {
			String key(line.before("="));
			key.trim();

			if (section.key_map_.has(key) && check_duplicate_keys_)
			{
				Log.error() << "In INIFile " << filename_ << " , error while appending line: "
										<< line << " . Key '" << key << "' already exists in section." << endl;   
				return false;
 			}
			
			section.lines_.push_back(line);
			list<String >::iterator	line_it(section.lines_.end());
			line_it--;

			section.key_map_[key] = line_it;

			return true;
		}

		section.lines_.push_back(line);

		return true;
	}
Example #2
0
File: INIFile.C Project: HeyJJ/ball
	bool INIFile::insertLine(LineIterator line_it, const String& line)
	{
		if (!isValid(line_it))
		{
      Log.error() << "In INIFile " << filename_ << " , error while inserting line: "
                  << line << " . Illegal iterator!" << endl;			
			return false;
		}

		if (line_it.isSectionLastLine())
    {
			return appendLine(line_it.getSection()->getName(), line);
		}

		Section& section(*line_it.getSection());

		// key?
    if (line.hasSubstring("=", 1))
    {
			String key(line.before("="));
			key.trim();

			if (section.key_map_.has(key) && check_duplicate_keys_)
			{

        Log.error() << "In INIFile " << filename_ << " , error while appending line: "
                    << line << " . Key '" << key << "' already exists in section." << endl;
				return false;
			}

			line_it.getSectionNextLine();

			section.key_map_[key] = section.lines_.insert(line_it.position_, line);
			return true;
		}

		line_it.getSectionNextLine();
		section.lines_.insert(line_it.position_, line);
		return true;
	}
Example #3
0
File: INIFile.C Project: HeyJJ/ball
	bool INIFile::setLine(LineIterator line_it, const String& line)
	{
		// section lines cannot be changed with this method
		if (!isValid(line_it) || (*line_it)[0] == '[')
		{
			return false;
		}
		
		String new_key(line.before("="));
		new_key.trim();

		if ((*line_it).hasSubstring("=", 1))
		{
			// oh, this line had a key :(
			String old_key((*line_it).before("="));
			old_key.trim();

			if (old_key == new_key)
			{
				line_it.setLine_(line);
				return true;
			}

			// its a new key: delete the old one.
			line_it.getSection()->key_map_.remove(old_key);
		}

		line_it.setLine_(line);

		if (line.hasSubstring("=", 1))
		{
			// oh, the new line has a key :(
			line_it.getSection()->key_map_[new_key] = line_it.getPosition();		
		}

		return true;
	}
Example #4
0
 bool DigestionEnzyme::setValueFromFile(const String& key, const String& value)
 {
   if (key.hasSuffix(":Name"))
 {
     setName(value);
     return true;
 }
   if (key.hasSuffix(":RegEx"))
 {
     setRegEx(value);
     return true;
 }
   if (key.hasSuffix(":RegExDescription"))
 {
     setRegExDescription(value);
     return true;
 }
   if (key.hasSubstring(":Synonyms:"))
 {
     addSynonym(value);
     return true;
 }
   return false;
 }
Example #5
0
  //reimplemented in order to handle index MzML
  bool MzMLFile::isValid(const String& filename, std::ostream& os)
  {
    //determine if this is indexed mzML or not
    bool indexed = false;
    TextFile file(filename, true, 4);
    String s;
    s.concatenate(file.begin(), file.end());
    if (s.hasSubstring("<indexedmzML"))
    {
      indexed = true;
    }
    // find the corresponding schema
    String current_location;
    if (indexed)
    {
      current_location = File::find(indexed_schema_location_);
    }
    else
    {
      current_location = File::find(schema_location_);
    }

    return XMLValidator().isValid(filename, current_location, os);
  }
Example #6
0
  void createFragment_(String & fragment, const Param & param, const std::map<int, std::string>& optional_mappings = (std::map<int, std::string>()))
  {

    //std::cerr << "FRAGMENT: " << fragment << "\n\n";

    // e.g.:  -input %BASENAME[%%in].mzML

    // we have to make this little detour param -> vector<String>
    // to sort the param names by length, otherwise we have a
    // problem with parameter substitution
    // i.e., if A is a prefix of B and gets replaced first, the
    // suffix of B remains and will cause trouble, e.g.: "%%out" vs. "%%out_fm"
    vector<String> param_names;
    param_names.reserve(param.size());
    for (Param::ParamIterator it = param.begin(); it != param.end(); ++it)
    {
      param_names.push_back(it->name);
    }
    // sort by length
    std::sort(param_names.begin(), param_names.end(), reverseComparator(StringSizeLess()));

    // iterate through all input params and replace with values:
    SignedSize allowed_percent(0); // filenames might contain '%', which are allowed to remain there (and even must remain)
    for (vector<String>::iterator it = param_names.begin(); it != param_names.end(); ++it)
    {
      if (!fragment.hasSubstring("%%" + *it)) continue;

      String s_new = paramToString_(param.getEntry(*it));
      allowed_percent += s_new.length() - String(s_new).substitute("%", "").length();
      //std::cerr << "IN: " << s_new << "(" << allowed_percent << "\n";
      fragment.substitute("%%" + *it, s_new);
    }
    if (fragment.hasSubstring("%%")) throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Invalid '%%' found in '" + fragment + "' after replacing all parameters!", fragment);

    // mapping replace> e.g.: %2
    // do it reverse, since %10 should precede %1
    for (std::map<int, std::string>::const_reverse_iterator it = optional_mappings.rbegin(); it != optional_mappings.rend(); ++it)
    {
      String m = String("%") + it->first;
      if (fragment.hasSubstring(m)) {
        writeDebug_(String("Replacing '") + m + "' in '" + fragment + "' by '" + it->second + "'\n", 10);
        fragment.substitute(m, it->second);
      }
    }

    // %TMP replace:
    fragment.substitute("%TMP", File::getTempDirectory());

    // %RND replace:
    fragment.substitute("%RND", String(UniqueIdGenerator::getUniqueId()));

    // %WORKINGDIR replace:
    fragment.substitute("%WORKINGDIR", tde_.working_directory);

    // %DIR% replace
    {
      QRegExp rx("%DIR\\[(.*)\\]");
      rx.setMinimal(true);
      int pos = 0;
      QString t_tmp = fragment.toQString();
      //std::cout << "fragment is:" << fragment << std::endl;
      while ((pos = rx.indexIn(t_tmp, pos)) != -1)
      {
        String value = rx.cap(1);   // param name (hopefully)
        // replace in fragment:
        QFileInfo qfi(value.toQString());
        //std::cout << "match @ " << pos << " " << value << " --> " << qfi.canonicalPath() << "\n";
        t_tmp = t_tmp.replace(String("%DIR[" + value + "]").toQString(), qfi.canonicalPath());
      }
      fragment = String(t_tmp);
      //std::cout << "NEW fragment is:" << fragment << std::endl;
    }

    // %BASENAME% replace
    {
      QRegExp rx("%BASENAME\\[(.*)\\]");
      rx.setMinimal(true);
      int pos = 0, count = 0;
      QString t_tmp = fragment.toQString();
      while ((pos = rx.indexIn(t_tmp, pos)) != -1)
      {
        //std::cout << "match @ " << pos << "\n";
        String value = rx.cap(1);   // param name (hopefully)
        // replace in fragment:
        QFileInfo qfi(value.toQString());
        //std::cout << "match @ " << pos << " " << value << " --> " << qfi.completeBaseName() << "\n";
        t_tmp = t_tmp.replace(String("%BASENAME[" + value + "]").toQString(), qfi.completeBaseName());
        ++count;
      }
      // update expected count of valid '%'
      allowed_percent -= (fragment.length() - String(fragment).substitute("%", "").length()) // original # of %
                         - (t_tmp.length() - String(t_tmp).substitute("%", "").length()) // new # of %
                         - count; // expected # of % due to %BASENAME
      fragment = String(t_tmp);
    }

    SignedSize diff = (fragment.length() - String(fragment).substitute("%", "").length()) - allowed_percent;
    //std::cerr << "allowed: " << allowed_percent << "\n" << "diff: " << diff << " in: " << fragment << "\n";
    if (diff > 0) throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Mapping still contains '%' after substitution! Did you use % instead of %%?", fragment);
    else if (diff < 0) throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Error: '%' from a filename where accidentally considered command tags! "
                                                                                              "This is a bug! Remove '%' from input filesnames to fix, but please report this as well!", fragment);

    //std::cout << fragment << "'\n";
  }