Example #1
0
  libtocc::FileInfo FSHandler::get_by_path(std::string path)
  {
    // First, we check if the specified path matches exactly with a
    // Traditional Path. If it is, we return that file.
    // Then, we check if the path contains tags and title of a file. We assume
    // each directory in the path is a tag, and the last element is title of
    // a file. If it matches a single file, we return that.

    /*
     * First try: Checking if path exactly matches a Traditional Path.
     */
    // TODO: First, check the traditional path.

    /*
     * Second try: Last element is the file title and others are tags.
     */
    std::vector<std::string> path_items = split_string(path, '/');

    if (path_items.empty())
    {
      // Returning a NULL file info.
      return get_empty_file_info();
    }

    // Assume the last element is the file title.
    libtocc::Title title_expr(path_items.back().c_str());
    path_items.pop_back();
    libtocc::And main_and(title_expr);

    if (!path_items.empty())
    {
      // Each of the items considered a tag.
      std::vector<std::string>::iterator path_items_iterator = path_items.begin();
      for (; path_items_iterator != path_items.end(); path_items_iterator++)
      {
        libtocc::Tag tag_expr(path_items_iterator->c_str());
        main_and.add(tag_expr);
      }
    }

    // Executing the query.
    libtocc::Query second_query(main_and);
    libtocc::FileInfoCollection second_query_result =
        this->libtocc_manager->search_files(second_query);

    if (second_query_result.size() == 1)
    {
      // If exactly one file found, this is what we wanted.
      libtocc::FileInfoCollection::Iterator query_result_iterator(&second_query_result);
      // Copying the pointer.
      libtocc::FileInfo result(*query_result_iterator.get());

      return result;
    }

    // Nothing found. Returning a NULL FileInfo.
    return get_empty_file_info();
  }
Example #2
0
  libtocc::FileInfo FSHandler::get_by_path(std::string path)
  {
    // First, we check if the specified path matches exactly with a
    // Traditional Path. If it is, we return that file.
    // Then, we check if the path contains tags and title of a file. We assume
    // each directory in the path is a tag, and the last element is title of
    // a file. If it matches a single file, we return that.

    /*
     * First try: Checking if path exactly matches a Traditional Path.
     */
    // FIXME: The following block is commented, because of a bug in Unqlite:
    //        If database become big, this cause Segfault in UnQlite.
//    try
//    {
//      libtocc::FileInfo result =
//          this->libtocc_manager->get_file_by_traditional_path(path.c_str());
//
//      return result;
//    }
//    catch (libtocc::DatabaseScriptLogicalError& error)
//    {
//      // Nothing found.
//    }

    /*
     * Second try: Last element is the file title and others are tags.
     */
    std::vector<std::string> path_items = split_string(path, '/');

    if (path_items.empty())
    {
      // Returning a NULL file info.
      return get_empty_file_info();
    }

    // Assume the last element is the file title.
    libtocc::Title title_expr(path_items.back().c_str());
    path_items.pop_back();
    libtocc::And main_and(title_expr);

    if (!path_items.empty())
    {
      // Each of the items considered a tag.
      std::vector<std::string>::iterator path_items_iterator = path_items.begin();
      for (; path_items_iterator != path_items.end(); path_items_iterator++)
      {
        if (*path_items_iterator == ".." || *path_items_iterator == ".")
        {
          // Ignore . or ..
          continue;
        }
        libtocc::Tag tag_expr(path_items_iterator->c_str());
        main_and.add(tag_expr);
      }
    }

    // Executing the query.
    libtocc::Query second_query(main_and);
    libtocc::FileInfoCollection second_query_result =
        this->libtocc_manager->search_files(second_query);

    if (second_query_result.size() == 1)
    {
      // If exactly one file found, this is what we wanted.
      libtocc::FileInfoCollection::Iterator query_result_iterator(&second_query_result);
      // Copying the pointer.
      libtocc::FileInfo result(*query_result_iterator.get());

      return result;
    }

    // Nothing found. Returning a NULL FileInfo.
    return get_empty_file_info();
  }