Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
nsNameValuePairDB Class Reference

#include <nsNameValuePairDB.h>

List of all members.

Public Member Functions

 nsNameValuePairDB ()
 ~nsNameValuePairDB ()
PRBool HadError ()
PRBool GetNextGroup (const char **aType)
PRBool GetNextGroup (const char **aType, const char *aName)
PRBool GetNextGroup (const char **aType, const char *aName, int aLen)
PRInt32 GetNextElement (const char **aName, const char **aValue)
PRInt32 GetNextElement (const char **aName, const char **aValue, char *aBuffer, PRUint32 aBufferLen)
PRBool OpenForRead (const nsACString &aCatalogName)
PRBool OpenTmpForWrite (const nsACString &aCatalogName)
PRBool PutElement (const char *aName, const char *aValue)
PRBool PutEndGroup (const char *aType)
PRBool PutStartGroup (const char *aType)
PRBool RenameTmp (const char *aCatalogName)

Protected Member Functions

PRBool CheckHeader ()

Protected Attributes

PRUint16 mMajorNum
PRUint16 mMinorNum
PRUint16 mMaintenanceNum
FILEmFile
char mBuf [FC_BUF_LEN]
PRInt32 mCurrentGroup
PRPackedBool mAtEndOfGroup
PRPackedBool mAtEndOfCatalog
PRPackedBool mError

Detailed Description

Definition at line 57 of file nsNameValuePairDB.h.


Constructor & Destructor Documentation

Definition at line 318 of file nsNameValuePairDB.cpp.

{
  if (mFile) {
    fclose(mFile);
    mFile = nsnull;
  }
}

Here is the call graph for this function:


Member Function Documentation

Definition at line 60 of file nsNameValuePairDB.cpp.

{
  const char *name, *value;
  int num, major, minor, maintenance;
  PRBool foundVersion = PR_FALSE;

  if (!mFile)
    return PR_FALSE;

  if (fseek(mFile, 0L, SEEK_SET) != 0)
    return PR_FALSE;
  mCurrentGroup = 0;
  mAtEndOfGroup = PR_FALSE;
  while (GetNextElement(&name, &value) > 0) {
    if (*name == '\0') // ignore comments
      continue;
    if (strcmp(name, "Version")==0) {
      foundVersion = PR_TRUE;
      num = sscanf(value, "%d.%d.%d", &major, &minor, &maintenance);
      if (num != 3) {
        NVPDB_PRINTF(("failed to parse version number (%s)", value));
        return PR_FALSE;
      }

      // NVPDB_VERSION_MAJOR
      // It is presumed that major versions are not backwards compatibile.
      if (major != NVPDB_VERSION_MAJOR) {
        NVPDB_PRINTF(("version major %d != %d", major, NVPDB_VERSION_MAJOR));
        return PR_FALSE;
      }

      // NVPDB_VERSION_MINOR
      // It is presumed that minor versions are backwards compatible
      // but will have additional features.
      // Put any tests related to minor versions here.

      // NVPDB_VERSION_MAINTENANCE
      // It is presumed that maintenance versions are backwards compatible,
      // have no new features, but can have bug fixes.
      // Put any tests related to maintenance versions here.

      mMajorNum = major;
      mMinorNum = minor;
      mMaintenanceNum = maintenance;
    }
  }

  return foundVersion;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRInt32 nsNameValuePairDB::GetNextElement ( const char **  aName,
const char **  aValue 
)

Definition at line 122 of file nsNameValuePairDB.cpp.

{
  return GetNextElement(aName, aValue, mBuf, sizeof(mBuf));
}

Here is the caller graph for this function:

PRInt32 nsNameValuePairDB::GetNextElement ( const char **  aName,
const char **  aValue,
char *  aBuffer,
PRUint32  aBufferLen 
)

Definition at line 138 of file nsNameValuePairDB.cpp.

{
  char *line, *name, *value;
  unsigned int num;
  int len;
  unsigned int groupNum;

  *aName  = "";
  *aValue = "";

  if (aBufferLen < NVPDB_MIN_BUFLEN) {
    return NVPDB_BUFFER_TOO_SMALL;
  }

  if (mAtEndOfGroup) {
    return NVPDB_END_OF_GROUP;
  }

  //
  // Get a line
  //
  line = fgets(aBuffer, aBufferLen, mFile);
  if (!line) {
    if (feof(mFile)) { // end of file
      mAtEndOfGroup = PR_TRUE;
      mAtEndOfCatalog = PR_TRUE;
      return NVPDB_END_OF_FILE;
    }
    return NVPDB_FILE_IO_ERROR;
  }

  //
  // Check we got a complete line
  //
  len = strlen(line);
  NS_ASSERTION(len!=0, "an empty string is invalid");
  if (len == 0)
    return NVPDB_GARBLED_LINE;
  if (line[len-1] != '\n') {
    len++; // space for the line terminator
    while (1) {
      int val = getc(mFile);
      if (val == EOF)
        return -len;
      len++;
      if (val == '\n')
        return -len;
    }
  }
  len--;
  line[len] = '\0';
  //NVPDB_PRINTF(("line = (%s)", line));

  //
  // Check the group number
  //
  num = sscanf(line, "%u", &groupNum);
  if ((num != 1) || (groupNum != (unsigned)mCurrentGroup))
    return NVPDB_END_OF_GROUP;

  //
  // Get the name
  //
  name = strchr(line, ' ');
  if ((!name) || (name[1]=='\0'))
    return NVPDB_GARBLED_LINE;
  name++;

  //
  // If it is a comment 
  //   return a blank name (strlen(*aName)==0)
  //   return the comment in the value field
  //
  if (*name == '#') {
    *aValue = name;
    return 1;
  }

  //
  // Get the value
  //
  value = strchr(name, '=');
  if (!value)
    return NVPDB_GARBLED_LINE;
  *value = '\0';
  value++;

  //
  // Check for end of group
  //
  if (strcmp(name,"end")==0) {
    mAtEndOfGroup = PR_TRUE;
    return NVPDB_END_OF_GROUP;
  }

  //
  // Got the name and value
  //
  *aName = name;
  *aValue = value;
  return 1;
}

Here is the call graph for this function:

Definition at line 243 of file nsNameValuePairDB.cpp.

{
  return GetNextGroup(aType, nsnull, 0);
}

Here is the caller graph for this function:

PRBool nsNameValuePairDB::GetNextGroup ( const char **  aType,
const char *  aName 
)

Definition at line 249 of file nsNameValuePairDB.cpp.

{
  return GetNextGroup(aType, aName, strlen(aName));
}

Here is the call graph for this function:

PRBool nsNameValuePairDB::GetNextGroup ( const char **  aType,
const char *  aName,
int  aLen 
)

Definition at line 255 of file nsNameValuePairDB.cpp.

{
  const char *name, *value;
  long pos = 0;

  *aType = "";

  if (mAtEndOfCatalog)
    return PR_FALSE;

  //
  // Move to end of current Group
  //
  while (GetNextElement(&name, &value) > 0) 
    continue;
  mCurrentGroup++;
  mAtEndOfGroup = PR_FALSE;
  // save current pos in case this in not the desired type 
  // and we need to backup
  if (aName)
    pos = ftell(mFile);

  // check if there are more Groups
  if (GetNextElement(&name, &value) <= 0) {
    mAtEndOfGroup = PR_TRUE;
    mAtEndOfCatalog = PR_TRUE;
    return PR_FALSE;
  }
  if (strcmp(name,"begin"))
    goto GetNext_Error;

  // check if this is the desired type
  if (aName) {
    if (strncmp(value,aName,aLen)) {
      fseek(mFile, pos, SEEK_SET);
      mCurrentGroup--;
      mAtEndOfGroup = PR_TRUE;
      return PR_FALSE;
    }
  }

  *aType = value;
  return PR_TRUE;

GetNext_Error:
  mError = PR_TRUE;
  NVPDB_PRINTF(("GetNext_Error"));
  return PR_FALSE;
}

Here is the call graph for this function:

Definition at line 62 of file nsNameValuePairDB.h.

{ return mError; };
PRBool nsNameValuePairDB::OpenForRead ( const nsACString &  aCatalogName)

Definition at line 327 of file nsNameValuePairDB.cpp.

{
  nsresult result;

  nsCOMPtr<nsILocalFile> local_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID,
                                                        &result);
  if (NS_FAILED(result))
    goto error_return;

  local_file->InitWithNativePath(aCatalogName);
  local_file->OpenANSIFileDesc("r", &mFile);
  if (mFile && CheckHeader())
    return PR_TRUE;

error_return:
  mError = PR_TRUE;
  NVPDB_PRINTF(("OpenForRead error"));
  return PR_FALSE;
}

Here is the call graph for this function:

PRBool nsNameValuePairDB::OpenTmpForWrite ( const nsACString &  aCatalogName)

Definition at line 348 of file nsNameValuePairDB.cpp.

{
  nsresult result;
  nsCOMPtr<nsILocalFile> local_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID,
                                                        &result);
  if (NS_FAILED(result))
    return PR_FALSE;
  local_file->InitWithNativePath(aCatalogName + NS_LITERAL_CSTRING(".tmp"));
  local_file->OpenANSIFileDesc("w+", &mFile);
  if (mFile == nsnull)
    return PR_FALSE;

  // Write the header
  mAtEndOfGroup = PR_TRUE;
  mCurrentGroup = -1;
  PutStartGroup("Header");
  char buf[64];
  PutElement("", "########################################");
  PutElement("", "#                                      #");
  PutElement("", "#          Name Value Pair DB          #");
  PutElement("", "#                                      #");
  PutElement("", "#   This is a program generated file   #");
  PutElement("", "#                                      #");
  PutElement("", "#             Do not edit              #");
  PutElement("", "#                                      #");
  PutElement("", "########################################");
  PR_snprintf(buf, sizeof(buf), "%d.%d.%d", NVPDB_VERSION_MAJOR,
                NVPDB_VERSION_MINOR, NVPDB_VERSION_MAINTENANCE);
  PutElement("Version", buf);
  PutEndGroup("Header");

  return PR_TRUE;
}

Here is the call graph for this function:

PRBool nsNameValuePairDB::PutElement ( const char *  aName,
const char *  aValue 
)

Definition at line 383 of file nsNameValuePairDB.cpp.

{
  if (mAtEndOfGroup) {
    mError = PR_TRUE;
    NVPDB_PRINTF(("PutElement_Error"));
    return PR_FALSE;
  }

  if ((!*aName) && (*aValue == '#'))
    fprintf(mFile, "%u %s\n", mCurrentGroup, aValue);
  else
    fprintf(mFile, "%u %s=%s\n", mCurrentGroup, aName, aValue);
#ifdef DEBUG
  fflush(mFile);
#endif
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 402 of file nsNameValuePairDB.cpp.

{
  if (mAtEndOfGroup) {
    mError = PR_TRUE;
    NVPDB_PRINTF(("PutEndGroup_Error"));
    return PR_FALSE;
  }

  mAtEndOfGroup = PR_TRUE;
  fprintf(mFile, "%u end=%s\n", mCurrentGroup, aType);
#ifdef DEBUG
  fflush(mFile);
#endif
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 419 of file nsNameValuePairDB.cpp.

{
  if (!mAtEndOfGroup) {
    mError = PR_TRUE;
    NVPDB_PRINTF(("PutStartGroup_Error"));
#ifdef DEBUG
    fflush(mFile);
#endif
    return PR_FALSE;
  }

  mAtEndOfGroup = PR_FALSE;
  mCurrentGroup++;
  fprintf(mFile, "%u begin=%s\n", mCurrentGroup, aType);
#ifdef DEBUG
  fflush(mFile);
#endif
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsNameValuePairDB::RenameTmp ( const char *  aCatalogName)

Definition at line 440 of file nsNameValuePairDB.cpp.

{
  nsresult rv;
  nsCOMPtr<nsILocalFile> dir;
  PRBool exists = PR_FALSE;
  nsCAutoString old_name(aCatalogName);
  nsDependentCString current_name(aCatalogName);
  nsCAutoString tmp_name(aCatalogName);
  nsCAutoString old_name_tail;
  nsCAutoString current_name_tail;
  nsCOMPtr<nsILocalFile> old_file;
  nsCOMPtr<nsILocalFile> current_file;
  nsCOMPtr<nsILocalFile> tmp_file;
  nsCAutoString parent_dir;
  nsCAutoString parent_path;
  nsCAutoString cur_path;

  //
  // Split the parent dir and file name
  //
  PRInt32 slash = 0, last_slash = -1;
  nsCAutoString fontDirName(aCatalogName);
  // RFindChar not coded so do it by hand
  while ((slash=fontDirName.FindChar('/', slash))>=0) {
    last_slash = slash;
    slash++;
  }
  if (last_slash < 0)
    goto Rename_Error;

  fontDirName.Left(parent_dir, last_slash);
  dir = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    goto Rename_Error;
  dir->InitWithNativePath(parent_dir);
  dir->GetNativePath(parent_path);

  if (!mAtEndOfGroup || mError)
    goto Rename_Error;

  //
  // check that we have a tmp copy
  //
  tmp_name.Append(".tmp");
  tmp_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    goto Rename_Error;
  tmp_file->InitWithNativePath(tmp_name);
  tmp_file->Exists(&exists);
  if (!exists)
    goto Rename_Error;

  //
  // get rid of any old copy
  //
  old_name.Append(".old");
  old_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    goto Rename_Error;
  old_file->InitWithNativePath(old_name);

  //
  // Check we have a current copy
  //
  current_file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    goto Rename_Error;
  current_file->InitWithNativePath(current_name);
  current_file->Exists(&exists);
  if (exists) {
    //
    // Rename the current copy to old
    //
    current_file->GetNativePath(cur_path);
    old_name.Right(old_name_tail, old_name.Length() - last_slash - 1);
    rv = current_file->MoveToNative(dir, old_name_tail);
    if (NS_FAILED(rv))
      goto Rename_Error;
  }

  //
  // Rename the tmp to current
  //
  current_name_tail = Substring(current_name, last_slash+1,
                                current_name.Length() - (last_slash + 1));
  rv = tmp_file->MoveToNative(dir, current_name_tail);
  if (NS_FAILED(rv))
    goto Rename_Error;

  //
  // remove the previous copy
  //
  if (exists) {
    old_file->Remove(PR_FALSE);
  }

  return PR_TRUE;

Rename_Error:
  mError = PR_TRUE;
  NVPDB_PRINTF(("Rename_Error"));
  return PR_FALSE;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 88 of file nsNameValuePairDB.h.

Definition at line 87 of file nsNameValuePairDB.h.

Definition at line 85 of file nsNameValuePairDB.h.

Definition at line 86 of file nsNameValuePairDB.h.

Definition at line 89 of file nsNameValuePairDB.h.

Definition at line 84 of file nsNameValuePairDB.h.

Definition at line 83 of file nsNameValuePairDB.h.

Definition at line 81 of file nsNameValuePairDB.h.

Definition at line 82 of file nsNameValuePairDB.h.


The documentation for this class was generated from the following files: