"Fossies" - the Fresh Open Source Software Archive

Member "postal-0.76/userlist.cpp" (14 Jan 2012, 1654 Bytes) of package /linux/privat/postal-0.76.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "userlist.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "userlist.h"
    2 #include <cstdlib>
    3 #include <cstring>
    4 #include <stdio.h>
    5 #include "expand.h"
    6 
    7 UserList::UserList(const char *userListFile, bool usePass, bool stripDom)
    8  : m_users(new STR_VEC)
    9  , m_passwords(NULL)
   10  , m_index(0)
   11  , m_maxNameLen(0)
   12  , m_primary(true)
   13 {
   14   char buf[1024];
   15   FILE *fp = fopen(userListFile, "r");
   16   if(!fp)
   17   {
   18     printf("Can't open \"%s\".\n", userListFile);
   19     exit(1);
   20   }
   21 
   22   if(usePass)
   23     m_passwords = new STR_VEC;
   24 
   25   while(fgets(buf, sizeof(buf), fp) )
   26   {
   27     strtok(buf, "\n");
   28     if(buf[0] != '\n' && buf[0] != '\r' && buf[0] != '#')
   29     {
   30       strtok(buf, " \t");
   31       char *pass = strtok(NULL, " \t");
   32       if(!pass && usePass)
   33       {
   34         printf("Need a password for \"%s\".", buf);
   35         continue;
   36       }
   37       if(usePass)
   38         m_passwords->push_back(buf + strlen(buf) + 1);
   39       if(stripDom)
   40         strtok(buf, "@");
   41       m_users->push_back(buf);
   42       if(strlen(buf) > m_maxNameLen)
   43         m_maxNameLen = strlen(buf);
   44     }
   45   }
   46   if(m_users->size() == 0)
   47   {
   48     printf("No users in file.\n");
   49     exit(1);
   50   }
   51   fclose(fp);
   52 }
   53 
   54 UserList::UserList(UserList &list)
   55  : m_users(list.m_users)
   56  , m_passwords(list.m_passwords)
   57  , m_index(0)
   58  , m_maxNameLen(0)
   59  , m_primary(false)
   60 {
   61 }
   62 
   63 UserList::~UserList()
   64 {
   65   if(m_primary)
   66   {
   67     delete m_users;
   68     delete m_passwords;
   69   }
   70 }
   71 
   72 const string &UserList::randomUser()
   73 {
   74   m_index = random() % m_users->size();
   75   return m_users[0][m_index];
   76 }
   77 
   78 string UserList::sequentialUser()
   79 {
   80   m_index++;
   81   if(m_index == m_users->size())
   82     m_index = 0;
   83   return m_users[0][m_index];
   84 }
   85 
   86 string UserList::password()
   87 {
   88   return m_passwords[0][m_index];
   89 }