CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

util.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <util.h>
00021 #include <stdlib.h>
00022 #include <time.h>
00023 #include <string>
00024 #include <algorithm>
00025 #include <iostream>
00026 #include <sptk4/CBase64.h> // Base64 encoding/decoding
00027 
00028 namespace container
00029 {
00030 namespace util
00031 {
00032 
00033 struct __SrandInit
00034 {
00035     __SrandInit(){srandom(time(0));}
00036 };
00037 static __SrandInit __srtandInit;
00038 
00042 unsigned long getRandomLong()
00043 {
00044     //srandom(time(0));
00045     return random();
00046 }
00047 
00048 
00054 std::string getRandomString(int len)
00055 {
00056     sptk::CBuffer in(len+2*sizeof(unsigned long));
00057     unsigned long* l=(unsigned long*)in.data();
00058     int cnt=len/sizeof(unsigned long)+1;
00059     for(int i=0;i<cnt;i++)
00060         l[i]=getRandomLong();
00061     in.bytes(len);
00062     sptk::CBuffer out;
00063     sptk::CBase64::encode(out,in);
00064     std::string ret(out.data(),len);
00065     std::transform(ret.begin(),ret.end(),ret.begin(),TrEq());
00066     return ret;
00067 }
00068 
00069 
00070 }
00071 }

SourceForge.net Logo