Exemplo n.º 1
0
    void SplitStringToMap(const string& s1, int nDelim, char nDelim2, map<string, string>& dict)
    {
        dict.clear();

        size_t nSize = s1.size()+1;
        char* pszTemp = new char[nSize];
        memset(pszTemp, 0, nSize);

        istringstream iss(s1);
        while(iss.getline(pszTemp, (std::streamsize)nSize, nDelim))
        {
            if(strlen(Rtrim(pszTemp))>0)
            {
                string s2(pszTemp);
                string::size_type nn = s2.find(nDelim2);
                if(nn != string::npos)
                {
                    dict.insert(make_pair(s2.substr(0, nn), s2.substr(nn+1)));
                }
            }
            memset(pszTemp, 0, nSize);
        }

        delete[] pszTemp;

        return;
    }
Exemplo n.º 2
0
    void SplitStringToVector(const string& s1, int nDelim, vector<string>& ls)
    {
        ls.clear();

        size_t nSize = s1.size()+1;
        char* pszTemp = new char[nSize];
        memset(pszTemp, 0, nSize);

        istringstream iss(s1);
        while(iss.getline(pszTemp, (std::streamsize)nSize, nDelim))
        {
            if(strlen(Rtrim(pszTemp))>0)
            {
                ls.push_back(pszTemp);
            }
            memset(pszTemp, 0, nSize);
        }

        delete [] pszTemp;
        pszTemp = NULL;

        return;
    }
Exemplo n.º 3
0
    //按照分隔符nDelim拆分字符串
    list<string> SplitString(const string& s1, int nDelim)
    {
        list<string> l;

        size_t nSize = s1.size()+1;
        char* pszTemp = new char[nSize];
        memset(pszTemp, 0, nSize);

        istringstream iss(s1);
        while(iss.getline(pszTemp, (std::streamsize)nSize, nDelim))
        {
            if(strlen(Rtrim(pszTemp))>0)
            {
                l.push_back(pszTemp);
            }
            memset(pszTemp, 0, nSize);
        }

        delete [] pszTemp;
        pszTemp = NULL;

        return l;
    }
Exemplo n.º 4
0
void accept_cli(threadargs *newargs)
{
  LList *userlist=newargs->list;
  int cli_socket=newargs->sock;
  int cli_sock2;
  int recvn;	//num of recv bytes
  char buf[bufsize+1]="";
  char buf2[bufsize+1]="";
  char cmd;
  client newcli;
  client lastuser;	//the user which client talk to last time
  bzero(&newcli,sizeof(client));
  bzero(&lastuser,sizeof(client));
  if(-1==Recv(cli_socket,buf))
    pthread_exit(NULL);
  printf("%s",buf);
  if(-1==Send(cli_socket,"Server OCP v0.0.1\n"))
    pthread_exit(NULL);
  bzero(buf,bufsize);
  if(-1==Recv(cli_socket,buf))
    pthread_exit(NULL);
  Rtrim(buf);
  while(useratlist(userlist,buf)==0) //username has been used
  {
    if(-1==Send(cli_socket,":x"))
      pthread_exit(NULL);
    bzero(buf,bufsize);
    if(-1==Recv(cli_socket,buf))
      pthread_exit(NULL);

    Rtrim(buf);
  }
  Send (cli_socket,"Longin Successfully\n");

  strncpy(newcli.nick,buf,strlen(buf));
  newcli.sock=cli_socket;
  ListInsert(userlist,newcli);


  while(1)
  {
    LNode *node=userlist->head->next; //use in :a
    bzero(buf,bufsize);
    if(Recv(cli_socket,buf)==-1)	//client offline
    {
      ListDelete(userlist,cli_socket);
      pthread_exit(NULL);
    }
    if((cmd=iscmd(buf))==0)	//if message body contains only message(not have a command)
    {
      if(useratlist(userlist,lastuser.nick)==0)

      {
        cli_sock2=lastuser.sock;
        domessage(buf2,newcli.nick,buf);
        if(-1==Send(cli_sock2,buf2))
          pthread_exit(NULL);
      }

      else
      {
        if(-1==Send(cli_socket,"The user you want to talk isn't online\n"))
          pthread_exit(NULL);
      }
      continue;
    }
    switch(cmd)
    {
    case 'l':
      bzero(buf,bufsize);
      LNode *user=userlist->head->next;
      while(user!=NULL)
      {
        strcat(buf,user->e.nick);
        strcat(buf,"\n");
        user=user->next;
      }

      if(-1==Send(cli_socket,buf))
        pthread_exit(NULL);
      break;
    case 'u':	//client change user which will talk to
      if(getuser(buf,lastuser.nick,userlist)!=-1)	//buf client's message //buf2 username
      {cli_sock2=findclientsock(userlist,lastuser.nick);
        lastuser.sock=cli_sock2;
        if(getthird(buf)!=NULL)
        {
          domessage(buf2,newcli.nick,getthird(buf));
          if(-1==Send(cli_sock2,buf2))
            pthread_exit(NULL);
        }
      }
      else
      {
        if(-1==Send(cli_socket,"You doesn't specify a user,or the user you want to talk to isn't online\n"))
          pthread_exit(NULL);
      }
      break;
    case 'q':	//client quit
      if(-1==Send(cli_socket,buf))
        pthread_exit(NULL);
      ListDelete(userlist,cli_socket);
      close(cli_socket);
      pthread_exit(NULL);
      break;
    case 'a':	//client talk to all user

      while(node!=NULL)
      {
        client user=node->e;

        cli_sock2=user.sock;
        if (cli_sock2!=cli_socket)	//don't send the message to your
          {   if(getsecond(buf)!=NULL)	//if the message body only contains the :a string

            domessage(buf2,newcli.nick,getsecond(buf));
          if(-1==Send(cli_sock2,buf2))
            pthread_exit(NULL);
        }

        node=node->next;
      }
      break;
    default :
      if(-1==Send(cli_socket,"Sever can't recognize your command\n"))
        pthread_exit(NULL);

    }
  }

}