Back to index

lightning-sunbird  0.9+nobinonly
Functions
csutil.cpp File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "csutil.hxx"
#include "nsCOMPtr.h"
#include "nsServiceManagerUtils.h"
#include "nsIUnicodeEncoder.h"
#include "nsIUnicodeDecoder.h"
#include "nsICaseConversion.h"
#include "nsICharsetConverterManager.h"
#include "nsUnicharUtilCIID.h"
#include "nsUnicharUtils.h"

Go to the source code of this file.

Functions

static NS_DEFINE_CID (kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID)
static NS_DEFINE_CID (kUnicharUtilCID, NS_UNICHARUTIL_CID)
char * mystrsep (char **stringp, const char delim)
char * mystrdup (const char *s)
void mychomp (char *s)
char * myrevstrdup (const char *s)
int isSubset (const char *s1, const char *s2)
int isRevSubset (const char *s1, const char *end_of_s2, int len)
void mkallcap (char *p, const struct cs_info *csconv)
void mkallsmall (char *p, const struct cs_info *csconv)
void mkinitcap (char *p, const struct cs_info *csconv)
struct cs_infoget_current_cs (const char *es)
const char * get_default_enc (const char *lang)

Function Documentation

struct cs_info* get_current_cs ( const char *  es) [read]

Definition at line 192 of file csutil.cpp.

                                                 {
  struct cs_info *ccs;

  nsCOMPtr<nsIUnicodeEncoder> encoder; 
  nsCOMPtr<nsIUnicodeDecoder> decoder; 
  nsCOMPtr<nsICaseConversion> caseConv;

  nsresult rv;
  nsCOMPtr<nsICharsetConverterManager> ccm = do_GetService(kCharsetConverterManagerCID, &rv);
  if (NS_FAILED(rv))
    return nsnull;

  rv = ccm->GetUnicodeEncoder(es, getter_AddRefs(encoder));
  if (encoder && NS_SUCCEEDED(rv))
    encoder->SetOutputErrorBehavior(encoder->kOnError_Replace, nsnull, '?');
  if (NS_FAILED(rv))
    return nsnull;
  rv = ccm->GetUnicodeDecoder(es, getter_AddRefs(decoder));

  caseConv = do_GetService(kUnicharUtilCID, &rv);
  if (NS_FAILED(rv))
    return nsnull;

  ccs = (struct cs_info *) malloc(256 * sizeof(cs_info));

  PRInt32 charLength = 256;
  PRInt32 uniLength = 512;
  char *source = (char *)malloc(charLength * sizeof(char));
  PRUnichar *uni = (PRUnichar *)malloc(uniLength * sizeof(PRUnichar));
  char *lower = (char *)malloc(charLength * sizeof(char));
  char *upper = (char *)malloc(charLength * sizeof(char));

  // Create a long string of all chars.
  unsigned int i;
  for (i = 0x00; i <= 0xff ; ++i) {
    source[i] = i;
  }

  // Convert this long string to unicode
  rv = decoder->Convert(source, &charLength, uni, &uniLength);

  // Do case conversion stuff, and convert back.
  caseConv->ToUpper(uni, uni, uniLength);
  encoder->Convert(uni, &uniLength, upper, &charLength);

  uniLength = 512;
  charLength = 256;
  rv = decoder->Convert(source, &charLength, uni, &uniLength);
  caseConv->ToLower(uni, uni, uniLength);
  encoder->Convert(uni, &uniLength, lower, &charLength);

  // Store
  for (i = 0x00; i <= 0xff ; ++i) {
    ccs[i].cupper = upper[i];
    ccs[i].clower = lower[i];
    
    if (ccs[i].clower != (unsigned char)i)
      ccs[i].ccase = true;
    else
      ccs[i].ccase = false;
      
  }

  free(source);
  free(uni);
  free(lower);
  free(upper);

  return ccs;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* get_default_enc ( const char *  lang)

Definition at line 287 of file csutil.cpp.

                                                {
  int n = sizeof(lang2enc) / sizeof(lang2enc[0]);
  for (int i = 0; i < n; i++) {
    if (strcmp(lang,lang2enc[i].lang) == 0) {
      return lang2enc[i].def_enc;
    }
  }
  return NULL;
}
int isRevSubset ( const char *  s1,
const char *  end_of_s2,
int  len 
)

Definition at line 116 of file csutil.cpp.

{
  while( (len > 0) && *s1 && (*s1 == *end_of_s2) ) {
    s1++;
    end_of_s2--;
    len --;
  }
  return (*s1 == '\0');
}

Here is the caller graph for this function:

int isSubset ( const char *  s1,
const char *  s2 
)

Definition at line 105 of file csutil.cpp.

{
  while( *s1 && (*s1 == *s2) ) {
    s1++;
    s2++;
  }
  return (*s1 == '\0');
}

Here is the caller graph for this function:

void mkallcap ( char *  p,
const struct cs_info csconv 
)

Definition at line 163 of file csutil.cpp.

{
  while (*p != '\0') { 
    *p = csconv[((unsigned char) *p)].cupper;
    p++;
  }
}

Here is the caller graph for this function:

void mkallsmall ( char *  p,
const struct cs_info csconv 
)

Definition at line 173 of file csutil.cpp.

{
  while (*p != '\0') { 
    *p = csconv[((unsigned char) *p)].clower;
    p++;
  }
}

Here is the caller graph for this function:

void mkinitcap ( char *  p,
const struct cs_info csconv 
)

Definition at line 183 of file csutil.cpp.

{
  if (*p != '\0') *p = csconv[((unsigned char)*p)].cupper;
}

Here is the caller graph for this function:

void mychomp ( char *  s)

Definition at line 66 of file csutil.cpp.

{
  int k = strlen(s);
  if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0';
  if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0';
}

Here is the caller graph for this function:

char* myrevstrdup ( const char *  s)

Definition at line 75 of file csutil.cpp.

{
    char * d = NULL;
    if (s) {
       int sl = strlen(s);
       d = (char *) malloc((sl+1) * sizeof(char));
       if (d) {
        const char * p = s + sl - 1;
         char * q = d;
         while (p >= s) *q++ = *p--;
         *q = '\0';
       }
    }
    return d; 
}

Here is the caller graph for this function:

char* mystrdup ( const char *  s)

Definition at line 53 of file csutil.cpp.

{
  char * d = NULL;
  if (s) {
     int sl = strlen(s);
     d = (char *) malloc(((sl+1) * sizeof(char)));
     if (d) memcpy(d,s,((sl+1)*sizeof(char)));
  }
  return d;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* mystrsep ( char **  stringp,
const char  delim 
)

Definition at line 26 of file csutil.cpp.

{
  char * rv = NULL;
  char * mp = *stringp;
  int n = strlen(mp);
  if (n > 0) {
     char * dp = (char *)memchr(mp,(int)((unsigned char)delim),n);
     if (dp) {
        *stringp = dp+1;
        int nc = (int)((unsigned long)dp - (unsigned long)mp); 
        rv = (char *) malloc(nc+1);
        memcpy(rv,mp,nc);
        *(rv+nc) = '\0';
        return rv;
     } else {
       rv = (char *) malloc(n+1);
       memcpy(rv, mp, n);
       *(rv+n) = '\0';
       *stringp = mp + n;
       return rv;
     }
  }
  return NULL;
}
static NS_DEFINE_CID ( kCharsetConverterManagerCID  ,
NS_ICHARSETCONVERTERMANAGER_CID   
) [static]
static NS_DEFINE_CID ( kUnicharUtilCID  ,
NS_UNICHARUTIL_CID   
) [static]