llsearchhistory.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:4k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file llsearchhistory.cpp
  3.  * @brief Search history container implementation
  4.  *
  5.  * $LicenseInfo:firstyear=2009&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2009-2010, Linden Research, Inc.
  8.  * 
  9.  * Second Life Viewer Source Code
  10.  * The source code in this file ("Source Code") is provided by Linden Lab
  11.  * to you under the terms of the GNU General Public License, version 2.0
  12.  * ("GPL"), unless you have obtained a separate licensing agreement
  13.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  14.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16.  * 
  17.  * There are special exceptions to the terms and conditions of the GPL as
  18.  * it is applied to this Source Code. View the full text of the exception
  19.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  20.  * online at
  21.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22.  * 
  23.  * By copying, modifying or distributing this software, you acknowledge
  24.  * that you have read and understood your obligations described above,
  25.  * and agree to abide by those obligations.
  26.  * 
  27.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29.  * COMPLETENESS OR PERFORMANCE.
  30.  * $/LicenseInfo$
  31.  */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llsearchhistory.h"
  34. #include "llfile.h"
  35. #include "llsdserialize.h"
  36. #include "llxmlnode.h"
  37. std::string LLSearchHistory::SEARCH_QUERY = "search_query";
  38. std::string LLSearchHistory::SEARCH_HISTORY_FILE_NAME = "search_history.txt";
  39. LLSearchHistory::LLSearchHistory()
  40. {
  41. }
  42. bool LLSearchHistory::load()
  43. {
  44. // build filename for each user
  45. std::string resolved_filename = getHistoryFilePath();
  46. llifstream file(resolved_filename);
  47. if (!file.is_open())
  48. {
  49. return false;
  50. }
  51. clearHistory();
  52. // add each line in the file to the list
  53. std::string line;
  54. LLPointer<LLSDParser> parser = new LLSDNotationParser();
  55. while (std::getline(file, line)) 
  56. {
  57. LLSD s_item;
  58. std::istringstream iss(line);
  59. if (parser->parse(iss, s_item, line.length()) == LLSDParser::PARSE_FAILURE)
  60. {
  61. break;
  62. }
  63. mSearchHistory.push_back(s_item);
  64. }
  65. file.close();
  66. return true;
  67. }
  68. bool LLSearchHistory::save()
  69. {
  70. // build filename for each user
  71. std::string resolved_filename = getHistoryFilePath();
  72. // open a file for writing
  73. llofstream file (resolved_filename);
  74. if (!file.is_open())
  75. {
  76. return false;
  77. }
  78. search_history_list_t::const_iterator it = mSearchHistory.begin();
  79. for (; mSearchHistory.end() != it; ++it)
  80. {
  81. file << LLSDOStreamer<LLSDNotationFormatter>((*it).toLLSD()) << std::endl;
  82. }
  83. file.close();
  84. return true;
  85. }
  86. std::string LLSearchHistory::getHistoryFilePath()
  87. {
  88. return gDirUtilp->getExpandedFilename(LL_PATH_PER_SL_ACCOUNT, SEARCH_HISTORY_FILE_NAME);
  89. }
  90. void LLSearchHistory::addEntry(const std::string& search_query)
  91. {
  92. if(search_query.empty())
  93. {
  94. return;
  95. }
  96. search_history_list_t::iterator it = 
  97. find(mSearchHistory.begin(), mSearchHistory.end(), search_query);
  98. if(mSearchHistory.end() != it)
  99. {
  100. mSearchHistory.erase(it);
  101. }
  102. LLSearchHistoryItem item(search_query);
  103. mSearchHistory.push_front(item);
  104. }
  105. bool LLSearchHistory::LLSearchHistoryItem::operator < (const LLSearchHistory::LLSearchHistoryItem& right)
  106. {
  107. S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
  108. return result < 0;
  109. }
  110. bool LLSearchHistory::LLSearchHistoryItem::operator > (const LLSearchHistory::LLSearchHistoryItem& right)
  111. {
  112. S32 result = LLStringUtil::compareInsensitive(search_query, right.search_query);
  113. return result > 0;
  114. }
  115. bool LLSearchHistory::LLSearchHistoryItem::operator==(const LLSearchHistory::LLSearchHistoryItem& right)
  116. {
  117. return 0 == LLStringUtil::compareInsensitive(search_query, right.search_query);
  118. }
  119. bool LLSearchHistory::LLSearchHistoryItem::operator==(const std::string& right)
  120. {
  121. return 0 == LLStringUtil::compareInsensitive(search_query, right);
  122. }
  123. LLSD LLSearchHistory::LLSearchHistoryItem::toLLSD() const
  124. {
  125. LLSD ret;
  126. ret[SEARCH_QUERY] = search_query;
  127. return ret;
  128. }