2015年12月27日星期日

An example of TextQuery

//check the word inputed by cin in the "text" file

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<memory>
#include<fstream>
#include<sstream>
class TextQuery{
  public:
    std::vector<std::string>::size_type line_no;
    std::map<std::vector<std::string>::size_type,int> query(std::string keyword,std::shared_ptr<std::vector<std::string>> file);
    std::shared_ptr<std::vector<std::string>> save_file(std::ifstream& inputer);
    void outputer(std::ostream& output,std::string keyword);
    void output_file(std::ofstream& outputf,std::string keyword);
    TextQuery(std::string url,std::string keyword);
  private:
    std::shared_ptr<std::vector<std::string>> file;
    std::map<std::vector<std::string>::size_type,int> result;
};
TextQuery::TextQuery(std::string url,std::string keyword){
  this->line_no = 0;
  std::ifstream inputer(url,std::ifstream::in);
  this->file = this->save_file(inputer);
  this->result = this->query(keyword,this->file);
}
std::shared_ptr<std::vector<std::string>> TextQuery::save_file(std::ifstream& inputer){
  std::shared_ptr<std::vector<std::string>> saver = std::make_shared<std::vector<std::string>>();
  std::string temp;
  while(getline(inputer,temp)){
    saver->push_back(temp);
    line_no ++;
  }
  return saver;
}
std::map<std::vector<std::string>::size_type,int> TextQuery::query(std::string keyword,std::shared_ptr<std::vector<std::string>> file){
  std::vector<std::string>::size_type count = 0;
  std::string temp;
  std::map<std::vector<std::string>::size_type,int> result;
  auto ite1 = file->cbegin(), ite2 = file->cend();
  while(ite1 != ite2 ){
    std::istringstream str_input(*ite1);   
    while(str_input >> temp){
      if(temp == keyword){
        if(result.find(count) == result.cend()) result.insert(std::make_pair(count,1));
        else
           result[count]++;
      }
    }
    ite1 ++;
    count ++;
  }
  return result;
}
void TextQuery::outputer(std::ostream& output,std::string keyword){
  int sum = 0;
  auto ite1 = this->result.cbegin();
  auto ite2 = this->result.cend();
  while(ite1 != ite2){
    sum += ite1->second;
    ite1 ++;
  }
  output << keyword << " occurs "<<sum<<" times"<<'\n';
  ite1 = this->result.cbegin();
  while(ite1 != ite2){
  if(ite1->second == 1 ){
    output<<"("<< ite1->second << " time in line " << ite1->first <<") "<< file->at(ite1->first)<<'\n';
  }
  else{
    output<<"("<< ite1->second << " times in line " << ite1->first <<") "<< file->at(ite1->first)<<'\n';
  }
  ite1 ++; 
  }
}
void TextQuery::output_file(std::ofstream& outputf,std::string keyword){
  int sum = 0;
  auto ite1 = this->result.cbegin();
  auto ite2 = this->result.cend();
  while(ite1 != ite2){
    sum += ite1->second;
    ite1 ++;
  }
  outputf << keyword << " occurs "<<sum<<" times"<<'\n';
  ite1 = this->result.cbegin();
  while(ite1 != ite2){
    if(ite1->second == 1 ){
      outputf<<"("<< ite1->second << " time in line " << ite1->first <<") "<< file->at(ite1->first)<<'\n';
    }
    else{
      outputf<<"("<< ite1->second << " times in line " << ite1->first <<") "<< file->at(ite1->first)<<'\n';
    }
    ite1 ++; 
  }
}
int main(){
  std::string keyword;
  std::cin >> keyword;
  std::string url = "./text";   //the file name
  TextQuery query(url,keyword);
  std::ofstream outputf("./textquery",std::ofstream::out);//output in "textquery"
  query.output_file(outputf,keyword);
}

Systemd in Ubuntu 14.04



 
  Since the resource monitor of Ubuntu system doesn't offer enough message and operations for         me, I was looking for a similar tool for some time. Just now I saw an essay about systemd and I       decided to try it.


Search for "systemd" and select the "system test" item.
if there's no such an item in your system, try the command:
apt-get install systemd-services





 And this is the first page. I'm wondering what's going to happen





Select suite? I suppose that I just need the default one for the reason that I'm just a beginner. But what's the next?

Thanks for select all for me and I have no reason to cancel any item.







Testing......And result?





Just a local webpage. Nothing special.
However, it can be useful when it comes to server.
But I was looking for a tool to kill a process by PID.




Google gave the answer.
  Command: ps -e check all the PID
                     kill [PID] stop it 

2015年12月25日星期五

Run .exe file in Ubuntu ( Using PlayOnLinux)

Although Mono Runtime can start a Windows program in some cases, it doesn't work well when it comes to a program that calls libraries of Windows. In that case, Wine may be the most convenient, and I found a tool of Wine called PlayOnLinux yesterday. It's really friendly for a beginner like me because all the commands needed are listed on https://www.playonlinux.com/en/download.html .
The tool requests your installing at least one version of Wine, but it's easy since I can "apt-get" it.


And it's the UI of the tool, you can see that I installed a gal-game.
Since I installed the tool in default, and the PC is in China (System language is Chinese), the version pushed to me is in Chinese UI.




The second picture is the guide of windows application installation. I don't know why but it seems to be in German. ( title and the buttons in Chinese) But thanks to the shadowsocks server, it's OK to google them for meanings.The left button is "next" and the right is "cancel" as I expected.

I tried the two choices. And it can be the first one if you are not testing. The second choice should be selected when you wanted to install a application in an existed virtual disk.(It might not work well if the Wine versions vary)




Just give a name to the virtual disk and start it.





32-bit or 64-bit?
I guess that most applications that I shall use would be run in a 64-bit system.




Just click the button and select the install.exe you want to install. Of course, an ordinary exe file can also be run here, but it's recommended not to do so. We can use "wine [file name]" in the terminal.















How to start a shadow-socks server

Since my pptp server didn't work well as expected, I decided to start a shadow-socks server. Fortunately, it's not a difficult task for a Linux beginner like me, and the price of VPS service is acceptable. ( I'm using Conoha VPS now and the price is about $9/month )
I chose Ubuntu 14.04 system since I'm a beginner.
At first, start the server and use ssh to log in the remote server. As a beginner, I just used the root account to operate, which is not a good habit. Since I didn't have a plan about starting a website or doing something else with the server, using root account can be more convenient because I didn't know whether I should add "sudo" before the commands that I was typing.
Commands:
  apt-get install python-pip(use the "apt-get update" command if failed)
  pip install shadowsocks
It's OK to write a configuration file for shadowsocks, but I thought that it's easier to configure it while starting.
 ssserver -p 1080 -k password -t 600 -m aes-256-cfb -d start
-p means port
-k means keyword
-t means time-delay
-m means the method to encrypt
-d means that the service can be background.(important)

Then I downloaded a client for Android. Since the UI is in Chinese, I googled the characters and got the meaning of these key items.
服务器  means server. I suppose that an IP address is expected.
远程端口 means remote port. Just fill it with the number after "-p" in the last command.
本地端口 means local port. 1080 may be the default number.
密码 means password.
加密方法 means the method to encrypt.