Ejemplo n.º 1
0
/** Add a keyword from it's definition
 *
 * @param  _lines : lines which define the keyword
 * @return keyword
 *
 * Returns a nullptr if keyword the keyword
 */
std::shared_ptr<Keyword>
KeyFile::add_keyword(const std::vector<std::string>& _lines,
                     int64_t _line_index)
{

  // find keyword name
  const auto it =
    std::find_if(_lines.cbegin(), _lines.cend(), [](const std::string& line) {
      return (line.size() > 0 && line[0] == '*');
    });

  if (it == _lines.cend())
    throw(std::invalid_argument(
      "Can not find keyword definition (line must begin with *)"));

  // determine type
  auto kw_type = Keyword::determine_keyword_type(*it);

  // do the thing
  auto kw = create_keyword(_lines, kw_type, _line_index);
  if (kw)
    keywords[kw->get_keyword_name()].push_back(kw);

  return kw;
}
Ejemplo n.º 2
0
/* 
 * Check the given function for given options and valid combinations of 
 * options
 */
int
check_key_state (struct command_line *cmdline)
{
	int i,j;

	/* Find first given keyword */
	for (i = 0; i < CMD_KEYWORD_NUM && !cmdline->parm[i].kw_given; i++);
	
	if (i >= CMD_KEYWORD_NUM) {
		error_print ("No valid parameter specified");
		print_usage ();
		return -1;
	}

	/* Check keywords */
	for (j = 0; j < CMD_KEYWORD_NUM; j++) {

		switch (cmd_key_table[i][j]) {
		case req:
			/* Missing keyword on command line */
			if (!(cmdline->parm[j].kw_given)) {
				error_print ("Option '%s' required when "
					     "specifying '%s'",
					     get_keyword_name (j),
					     get_keyword_name (i));
				return -1;
			}
			break;
		case inv:
			/* Invalid keyword on command line */
			if (cmdline->parm[j].kw_given) {
				error_print ("Only one of options '%s' and "
					     "'%s' allowed",
					     get_keyword_name (i),
					     get_keyword_name (j));
				return -1;
			}
			break;
		case opt:
			break;
		}
	}

	return 0;
}
Ejemplo n.º 3
0
/*
 * Save the given command together with its parameter. 
 */
static int
store_option (struct command_line* cmdline, enum cmd_keyword_id keyword,
	      char* value)
{
	if ((cmdline->parm[(int) keyword]).kw_given) {
		error_print ("Option '%s' specified more than once",
			     get_keyword_name (keyword));
		return -1;
	}
	cmdline->parm[(int) keyword].kw_given = 1;
	cmdline->parm[(int) keyword].data = value;
	return 0;
}
Ejemplo n.º 4
0
/*
 * Execute the command.
 */
int
do_command (char* device, struct command_line cmdline)
{
	int i, rc;

	rc = 0;
	for (i = 0; !cmdline.parm[i].kw_given; i++);

	switch (i) {
	case cmd_keyword_get_cache:
                rc = disk_get_cache (device); 
		break;
	case cmd_keyword_cache:
		rc = disk_set_cache (device, 
				     cmdline.parm[cmd_keyword_cache].data,
				     cmdline.parm[cmd_keyword_no_cyl].data);
		break;
	case cmd_keyword_no_cyl:
		break;
	case cmd_keyword_reserve:
		rc = disk_reserve (device);
		break;
	case cmd_keyword_release:
		rc = disk_release (device);
		break;
	case cmd_keyword_slock:
		rc = disk_slock (device);
		break;
	case cmd_keyword_profile: 
		rc = disk_profile (device, 
				   cmdline.parm[cmd_keyword_prof_item].data);
		break;
	case cmd_keyword_reset_prof:
		rc = disk_reset_prof (device);
		break;
	case cmd_keyword_prof_item:
		break;
	case cmd_keyword_query_reserve:
		rc = disk_query_reserve_status(device);
		break;
	default:
		error_print ("Unknown command '%s' specified",
			     get_keyword_name (i));
		break;
	}

	return rc;
}
Ejemplo n.º 5
0
/** Parse a keyfile
 *
 * @param _load_mesh : whether the mesh shall loaded
 * @return success : whether loading the data was successful
 *
 * The parameter can be used to prevent the loading of the mesh,
 * even though we use parse_mesh. We need this for includes.
 */
bool
KeyFile::load(bool _load_mesh)
{

  // read file
  auto my_filepath = resolve_include_filepath(get_filepath());
  std::vector<char> char_buffer = read_binary_file(my_filepath);
  has_linebreak_at_eof = char_buffer.back() == '\n';

#ifdef QD_DEBUG
  std::cout << "done." << std::endl;
#endif

  // init parallel worker if master file
  // if (parent_kf == this)
  //   _wq.init_workers(1);

  // convert buffer into blocks
  size_t iLine = 0;
  std::string last_keyword;
  std::vector<std::string> line_buffer;
  std::vector<std::string> line_buffer_tmp;
  bool found_pgp_section = false;

  std::string line;
  auto string_buffer = std::string(char_buffer.begin(), char_buffer.end());
  std::stringstream st(string_buffer);
  // for (; std::getline(st, line); ++iLine) {
  for (; std::getline(st, line); ++iLine) {

    if (line.find("-----BEGIN PGP") != std::string::npos) {
      found_pgp_section = true;
#ifdef QD_DEBUG
      std::cout << "Found PGP Section\n";
#endif
    }

    // remove windows file ending ... I hate it ...
    if (line.size() != 0 && line.back() == '\r')
      line.pop_back();

    // new keyword
    if (line[0] == '*' || found_pgp_section) {

      if (!line_buffer.empty() && !last_keyword.empty()) {

        // transfer possible header for following keyword (see function)
        transfer_comment_header(line_buffer, line_buffer_tmp);

        // get type
        auto kw_type = Keyword::determine_keyword_type(last_keyword);

#ifdef QD_DEBUG
        std::cout << last_keyword << " -> ";
        switch (kw_type) {
          case (Keyword::KeywordType::NODE):
            std::cout << "NODE\n";
            break;
          case (Keyword::KeywordType::ELEMENT):
            std::cout << "ELEMENT\n";
            break;
          case (Keyword::KeywordType::PART):
            std::cout << "PART\n";
            break;
          case (Keyword::KeywordType::GENERIC):
            std::cout << "GENERIC\n";
            break;
          case (Keyword::KeywordType::INCLUDE):
            std::cout << "INCLUDE\n";
            break;
          case (Keyword::KeywordType::INCLUDE_PATH):
            std::cout << "INCLUDE_PATH\n";
            break;
        }
#endif

        auto kw = create_keyword(line_buffer,
                                 kw_type,
                                 iLine - line_buffer.size() -
                                   line_buffer_tmp.size() + 1);
        if (kw)
          keywords[kw->get_keyword_name()].push_back(kw);

        // transfer cropped data
        line_buffer = line_buffer_tmp;
      }

      // we always trim keywords
      trim_right(line);
      last_keyword = line;

    } // IF:line[0] == '*'

    // Encrypted Sections
    //
    // Extracts encrypted section here and places it in a line in the
    // line buffer. An encrypted section is treated like a keyword.
    if (found_pgp_section) {
      found_pgp_section = false;

      // get stream position
      const auto stream_position = st.tellg();

      const auto end_position =
        string_buffer.find("-----END PGP", stream_position);

      if (end_position == std::string::npos)
        throw(
          std::runtime_error("Could not find \"-----END PGP MESSAGE-----\" for "
                             "corresponding \"-----BEGIN PGP MESSAGE-----\" "));

      // set stream position behind encrypted section
      st.seekg(end_position);

      // extract encrypted stuff
      line += '\n';
      line += std::string(char_buffer.begin() + stream_position,
                          char_buffer.begin() + end_position);

      // print_string_as_hex(line);

      if (line.back() == '\n')
        line.pop_back();
      if (line.back() == '\r')
        line.pop_back();
    }

    // we stupidly add every line to the buffer
    line_buffer.push_back(line);

  } // for:line

  // allocate last block
  if (!line_buffer.empty() && !last_keyword.empty()) {

    auto kw = create_keyword(line_buffer,
                             Keyword::determine_keyword_type(last_keyword),
                             iLine - line_buffer.size() + 1);
    if (kw)
      keywords[kw->get_keyword_name()].push_back(kw);
  }

  // only load files above *END!
  const auto end_kw_position = get_end_keyword_position();

  // includes
  if (load_includes) {

    // update include dirs
    get_include_dirs(true);

    // do the thing
    for (auto& include_kw : include_keywords) {

      if (include_kw->get_position() < end_kw_position)
        // Note: prevent loading the mesh here
        include_kw->load(false);
    }
  }

  // wait for threads to finish preloading
  // _wq.wait_for_completion();

  // Wait for completion
  // while (work_queue.size() != 0) {
  //   work_queue.front().wait();
  //   work_queue.pop();
  // }

  // load mesh if requested
  if (parse_mesh && _load_mesh) {

    // load nodes
    load_nodes();

    // load parts
    load_parts();

    // load elements
    load_elements();
  }

  return true;
}