Bug 742179 - Move MakeRandomString to xpcom/ds. r=bsmedberg

This commit is contained in:
Doug Turner 2012-04-05 09:50:55 -07:00
Родитель dafafb2a67
Коммит ec2ac96d20
4 изменённых файлов: 32 добавлений и 54 удалений

Просмотреть файл

@ -41,33 +41,7 @@
#include "nsDirectoryServiceUtils.h" #include "nsDirectoryServiceUtils.h"
#include "nsDirectoryServiceDefs.h" #include "nsDirectoryServiceDefs.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "nsCRT.h"
// XXX this code is ripped from profile/src/nsProfile.cpp and is further
// duplicated in uriloader/exthandler. this should probably be moved
// into xpcom or some other shared library.
#include <stdlib.h>
#define TABLE_SIZE 36
static const char table[] =
{ 'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z','0','1','2','3',
'4','5','6','7','8','9' };
static void
MakeRandomString(char *buf, PRInt32 bufLen)
{
// turn PR_Now() into milliseconds since epoch
// and salt rand with that.
double fpTime;
LL_L2D(fpTime, PR_Now());
srand((uint)(fpTime * 1e-6 + 0.5)); // use 1e-6, granularity of PR_Now() on the mac is seconds
PRInt32 i;
for (i=0;i<bufLen;i++) {
*buf++ = table[rand()%TABLE_SIZE];
}
*buf = 0;
}
// XXX
nsDownloader::~nsDownloader() nsDownloader::~nsDownloader()
{ {
@ -116,7 +90,7 @@ nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
char buf[13]; char buf[13];
MakeRandomString(buf, 8); NS_MakeRandomString(buf, 8);
memcpy(buf+8, ".tmp", 5); memcpy(buf+8, ".tmp", 5);
rv = mLocation->AppendNative(nsDependentCString(buf, 12)); rv = mLocation->AppendNative(nsDependentCString(buf, 12));
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;

Просмотреть файл

@ -69,33 +69,8 @@
#include "nsITimer.h" #include "nsITimer.h"
#include "nsThreadUtils.h" #include "nsThreadUtils.h"
// XXX Duped from profile/src/nsProfile.cpp.
#include <stdlib.h>
using namespace mozilla; using namespace mozilla;
#define TABLE_SIZE 36
static const char table[] =
{ 'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z','0','1','2','3',
'4','5','6','7','8','9' };
static void
MakeRandomString(char *buf, PRInt32 bufLen)
{
// turn PR_Now() into milliseconds since epoch
// and salt rand with that.
double fpTime;
LL_L2D(fpTime, PR_Now());
srand((uint)(fpTime * 1e-6 + 0.5)); // use 1e-6, granularity of PR_Now() on the mac is seconds
PRInt32 i;
for (i=0;i<bufLen;i++) {
*buf++ = table[rand()%TABLE_SIZE];
}
*buf = 0;
}
NS_IMPL_ISUPPORTS1(nsDataObj::CStream, nsIStreamListener) NS_IMPL_ISUPPORTS1(nsDataObj::CStream, nsIStreamListener)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -1467,7 +1442,7 @@ HRESULT nsDataObj::DropImage(FORMATETC& aFE, STGMEDIUM& aSTG)
// Photoshop which handle multiple drags into a single window. // Photoshop which handle multiple drags into a single window.
char buf[13]; char buf[13];
nsCString filename; nsCString filename;
MakeRandomString(buf, 8); NS_MakeRandomString(buf, 8);
memcpy(buf+8, ".bmp", 5); memcpy(buf+8, ".bmp", 5);
filename.Append(nsDependentCString(buf, 12)); filename.Append(nsDependentCString(buf, 12));
dropFile->AppendNative(filename); dropFile->AppendNative(filename);

Просмотреть файл

@ -41,6 +41,9 @@
#include "nsCRTGlue.h" #include "nsCRTGlue.h"
#include "nsXPCOM.h" #include "nsXPCOM.h"
#include "nsDebug.h" #include "nsDebug.h"
#include "prtime.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
@ -270,6 +273,30 @@ bool NS_IsAsciiDigit(PRUnichar aChar)
return aChar >= '0' && aChar <= '9'; return aChar >= '0' && aChar <= '9';
} }
#define TABLE_SIZE 36
static const char table[] = {
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z','0','1','2','3',
'4','5','6','7','8','9'
};
void NS_MakeRandomString(char *aBuf, PRInt32 aBufLen)
{
// turn PR_Now() into milliseconds since epoch
// and salt rand with that.
double fpTime;
LL_L2D(fpTime, PR_Now());
srand((uint)(fpTime * 1e-6 + 0.5)); // use 1e-6, granularity of PR_Now() on the mac is seconds
PRInt32 i;
for (i=0;i<aBufLen;i++) {
*aBuf++ = table[rand()%TABLE_SIZE];
}
*aBuf = 0;
}
#if defined(XP_WIN) #if defined(XP_WIN)
void void
printf_stderr(const char *fmt, ...) printf_stderr(const char *fmt, ...)

Просмотреть файл

@ -133,6 +133,8 @@ NS_COM_GLUE bool NS_IsAsciiWhitespace(PRUnichar aChar);
NS_COM_GLUE bool NS_IsAscii(const char* aString); NS_COM_GLUE bool NS_IsAscii(const char* aString);
NS_COM_GLUE bool NS_IsAscii(const char* aString, PRUint32 aLength); NS_COM_GLUE bool NS_IsAscii(const char* aString, PRUint32 aLength);
NS_COM_GLUE void NS_MakeRandomString(char *buf, PRInt32 bufLen);
#define FF '\f' #define FF '\f'
#define TAB '\t' #define TAB '\t'