move StringHash to nsMsgUtils.cpp, and use NS_MsgHashIfNecessary() in all

the ns*URI2Path() calls in the ns*Utils.cpp files to hash the file
names if necessary.
also, don't use fprintf().
This commit is contained in:
sspitzer%netscape.com 1999-06-08 05:08:31 +00:00
parent 2537f3bb24
commit 82354feb6e
7 changed files with 83 additions and 53 deletions

View File

@ -219,3 +219,58 @@ nsresult NS_MsgGetUntranslatedPriorityName (nsMsgPriority p, nsString2 *outName)
return NS_OK;
}
/* this used to be XP_StringHash2 from xp_hash.c */
/* phong's linear congruential hash */
static PRUint32 StringHash(const char *ubuf)
{
unsigned char * buf = (unsigned char*) ubuf;
PRUint32 h=1;
while(*buf) {
h = 0x63c63cd9*h + 0x9c39c33d + (int32)*buf;
buf++;
}
return h;
}
nsresult NS_MsgHashIfNecessary(nsString &name)
{
#if defined(XP_WIN16) || defined(XP_OS2)
const PRUint32 MAX_LEN = 8;
#elif defined(XP_MAC)
// mac sucks. 32 has to cover name + sbdSep.
const PRUint32 MAX_LEN = 28;
#elif defined(XP_UNIX) || defined(XP_PC)
const PRUint32 MAX_LEN = 55;
#else
#error need_to_define_your_max_filename_length
#endif
nsAutoString str(name, eOneByte);
#ifdef DEBUG_sspitzer
printf("in: %s\n",str.GetBuffer());
#endif
// Given a name, use either that name, if it fits on our
// filesystem, or a hashified version of it, if the name is too
// long to fit.
char hashedname[MAX_LEN + 1];
PRBool needshash = PL_strlen(str.GetBuffer()) > MAX_LEN;
#if defined(XP_WIN16) || defined(XP_OS2)
if (!needshash) {
needshash = PL_strchr(str.GetBuffer(), '.') != NULL ||
PL_strchr(str.GetBuffer(), ':') != NULL;
}
#endif
PL_strncpy(hashedname, str.GetBuffer(), MAX_LEN + 1);
if (needshash) {
PR_snprintf(hashedname + MAX_LEN - 8, 9, "%08lx",
(unsigned long) StringHash(str.GetBuffer()));
}
name = hashedname;
#ifdef DEBUG_sspitzer
printf("out: %s\n",hashedname);
#endif
return NS_OK;
}

View File

@ -58,3 +58,5 @@ NS_MSG_BASE nsresult NS_NewMessageFromMsgHdrEnumerator(nsIEnumerator *srcEnumera
NS_MSG_BASE nsresult NS_MsgGetPriorityFromString(const char *priority, nsMsgPriority *outPriority);
NS_MSG_BASE nsresult NS_MsgGetUntranslatedPriorityName (nsMsgPriority p, nsString2 *outName);
NS_MSG_BASE nsresult NS_MsgHashIfNecessary(nsString &name);

View File

@ -50,31 +50,7 @@ void nsNewsSummarySpec::CreateSummaryFileName()
{
char *leafName = GetLeafName();
#if defined(XP_WIN16) || defined(XP_OS2)
const PRUint32 MAX_LEN = 8;
#elif defined(XP_MAC)
const PRUint32 MAX_LEN = 25;
#else
const PRUint32 MAX_LEN = 55;
#endif
// Given a name, use either that name, if it fits on our
// filesystem, or a hashified version of it, if the name is too
// long to fit.
char hashedname[MAX_LEN + 1];
PRBool needshash = PL_strlen(leafName) > MAX_LEN;
#if defined(XP_WIN16) || defined(XP_OS2)
if (!needshash) {
needshash = PL_strchr(leafName, '.') != NULL ||
PL_strchr(leafName, ':') != NULL;
}
#endif
PL_strncpy(hashedname, leafName, MAX_LEN + 1);
if (needshash) {
PR_snprintf(hashedname + MAX_LEN - 8, 9, "%08lx",
(unsigned long) StringHash(leafName));
}
nsAutoString fullLeafName(hashedname, MAX_LEN + 1, eOneByte);
nsAutoString fullLeafName(leafName, PL_strlen(leafName), eOneByte);
// Append .msf (message summary file)
fullLeafName += ".msf";
@ -82,18 +58,3 @@ void nsNewsSummarySpec::CreateSummaryFileName()
SetLeafName(fullLeafName.GetBuffer());
PL_strfree(leafName);
}
/* this used to be XP_StringHash2 from xp_hash.c */
/* phong's linear congruential hash */
PRUint32
nsNewsSummarySpec::StringHash(const char *ubuf)
{
unsigned char * buf = (unsigned char*) ubuf;
PRUint32 h=1;
while(*buf)
{
h = 0x63c63cd9*h + 0x9c39c33d + (int32)*buf;
buf++;
}
return h;
}

View File

@ -39,9 +39,6 @@ public:
protected:
void CreateSummaryFileName();
private:
/* Phong's linear congruential hash */
PRUint32 StringHash(const char *ubuf);
};
#endif
#endif /* _nsNewsSummarySpec_H */

View File

@ -28,6 +28,8 @@
#include "nsIImapIncomingServer.h"
#include "nsMsgBaseCID.h"
#include "nsMsgUtils.h"
static NS_DEFINE_CID(kMsgMailSessionCID, NS_MSGMAILSESSION_CID);
nsresult
@ -77,7 +79,7 @@ nsImapURI2Path(const char* rootURI, const char* uriStr, nsFileSpec& pathResult)
sep += PR_GetDirectorySeparator();
nsAutoString sbdSep;
/* sspitzer: is this ok for mail and news? */
rv = nsGetMailFolderSeparator(sbdSep);
if (NS_FAILED(rv))
return rv;
@ -140,13 +142,16 @@ nsImapURI2Path(const char* rootURI, const char* uriStr, nsFileSpec& pathResult)
{
parentName.Right(leafName, parentName.Length() - dirEnd -1);
parentName.Truncate(dirEnd);
NS_MsgHashIfNecessary(parentName);
parentName += sbdSep;
pathResult += parentName;
parentName = leafName;
dirEnd = parentName.Find('/');
}
if (leafName != "")
pathResult += leafName;
if (leafName != "") {
NS_MsgHashIfNecessary(leafName);
pathResult += leafName;
}
}
return NS_OK;

View File

@ -28,6 +28,8 @@
#include "nsIPop3IncomingServer.h"
#include "nsMsgBaseCID.h"
#include "nsMsgUtils.h"
static NS_DEFINE_CID(kMsgMailSessionCID, NS_MSGMAILSESSION_CID);
@ -60,7 +62,7 @@ nsGetMailboxRoot(const char *hostname, nsFileSpec &result)
#ifdef DEBUG_alecf
if (!serverSupports)
fprintf(stderr, "Huh, serverSupports returned nsnull\n");
printf("Huh, serverSupports returned nsnull\n");
#endif
// if there are no pop servers, how did we get here?
@ -149,10 +151,15 @@ nsLocalURI2Path(const char* rootURI, const char* uriStr,
token = nsCRT::strtok(newStr, "/", &newStr);
// check if we're the last entry
if (token)
if (token) {
NS_MsgHashIfNecessary(dir);
dir += sbdSep; // no, we're not, so append .sbd
pathResult += dir;
pathResult += dir;
}
else {
NS_MsgHashIfNecessary(dir);
pathResult += dir;
}
}
PL_strfree(temp);
}

View File

@ -26,6 +26,7 @@
#include "nsIMsgIncomingServer.h"
#include "nsINntpIncomingServer.h"
#include "nsMsgBaseCID.h"
#include "nsMsgUtils.h"
static NS_DEFINE_CID(kMsgMailSessionCID, NS_MSGMAILSESSION_CID);
@ -53,9 +54,9 @@ nsGetNewsRoot(const char *hostname, nsFileSpec &result)
#ifdef DEBUG_NEWS
if (hosts->Count() <= 0)
fprintf(stderr, "Augh, no nntp server named %s?\n", hostname);
printf("Augh, no nntp server named %s?\n", hostname);
if (!serverSupports)
fprintf(stderr, "Huh, serverSupports returned nsnull\n");
printf("Huh, serverSupports returned nsnull\n");
#endif
// if there are no nntp servers, how did we get here?
@ -184,6 +185,7 @@ nsNewsURI2Path(const char* rootURI, const char* uriStr, nsFileSpec& pathResult)
// can't do pathResult += "host-"; pathresult += hostname;
// because += on a nsFileSpec inserts a separator
// so we'd end up with host-/hostname and not host-hostname
NS_MsgHashIfNecessary(alteredHost);
pathResult += alteredHost;
// create pathResult if it doesn't exist
@ -193,6 +195,7 @@ nsNewsURI2Path(const char* rootURI, const char* uriStr, nsFileSpec& pathResult)
pathResult.CreateDir();
if (newsgroup != "") {
NS_MsgHashIfNecessary(newsgroup);
pathResult += newsgroup;
}