Esempio n. 1
0
/**
 *  Check whether a class with a certain name exists
 *  @param  classname
 *  @param  len
 *  @param  autoload
 *  @return bool
 */
bool class_exists(const char *classname, size_t len, bool autoload) 
{
    // we need the tsrm_ls variable
    TSRMLS_FETCH();

    // we're going to load a class-entry
    zend_class_entry **ce;

    // should we autoload the class?
    if (autoload) 
    {
        // no auto-load
        if (SUCCESS != zend_lookup_class(classname, len, &ce TSRMLS_CC)) return false;

        // the found "class" could also be an interface or trait, which we do no want
        return ((*ce)->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0;
    }
    else
    {
        // starting slashes can be ignored
        if (len > 0 && classname[0] == '\\') { classname++; len--; }
        
        // all classes are in lowercase in the hash, so we make 
        // a temporary buffer for storing the lowercase class name
        // (is this smart? memory allocation is expensive!)
        std::unique_ptr<char[]> lc_name(new char[len + 1]);
        
        // copy the name to lowercase, but ignore the starting slash (if there is one)
        zend_str_tolower_copy(lc_name.get(), classname, len);

        // see if there is a class with this name
        if (SUCCESS != zend_hash_find(EG(class_table), lc_name.get(), len + 1, (void **) &ce)) return false;
        
        // the found "class" could also be an interface or trait, which we do no want
        return !(((*ce)->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT)) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
    }
}
Esempio n. 2
0
/* wstring getAcceptLangFromLocale (in wstring aLocale); */
NS_IMETHODIMP
nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_retval)
{
  nsDependentString lc_name(aLocale);
  if (lc_name.Length() <=0) {
#ifdef DEBUG
    printf("nsAcceptLang::GetAcceptLangFromLocale: aLocale is empty!");
#endif
    // TODO: don't return; instead, use system locale: lc_name=...
    return NS_ERROR_FAILURE;
  }

  nsresult res;

	nsCOMPtr<nsIStringBundleService> sBundleService = 
	         do_GetService(kStringBundleServiceCID, &res);
 	if (NS_FAILED(res) || (nsnull == sBundleService)) {
    return NS_ERROR_FAILURE;
  }

  nsCOMPtr<nsIStringBundle> bundle;
#if 1
  res = sBundleService->CreateBundle("resource://gre/res/language.properties",
                                     getter_AddRefs(bundle));
#else
  res = sBundleService->CreateBundle("chrome://global/locale/languageNames.properties",
                                     getter_AddRefs(bundle));
#endif
  PRUnichar *ptrv = nsnull;
  nsAutoString  lc_tmp(aLocale);
  NS_NAMED_LITERAL_STRING(sAccept, ".accept");
  NS_NAMED_LITERAL_STRING(sTrue, "true");

  ToLowerCase(lc_tmp);
  lc_tmp.Append(sAccept);
  if (NS_OK == (res = bundle->GetStringFromName(lc_tmp.get(), &ptrv))) {
    if (sTrue.Equals(ptrv)) {
      // valid name already
      *_retval = ToNewUnicode(lc_name);
      return res;
    }
  }

  /* not in languageNames.properties; lang only?
   */
  PRInt32  dash = lc_tmp.FindChar('-');
  nsAutoString lang;
  nsAutoString country;
  if (dash > 0) {
    /* lang-country
     */
    PRInt32 count = 0;
    count = lc_tmp.Left(lang, dash);
    count = lc_tmp.Right(country, (lc_tmp.Length()-dash-1));
    /* ja-JP -> ja*/
  }
  else {
    /* ja ?? en-JP 
       en-JP ->ja (how about product locale or syste, locale ???)
       ja-EN ->ja
    */
    lang = lc_name;
  }
  
  // lang always in lower case; don't convert
  *_retval = ToNewUnicode(lang);
  lang.Append(sAccept);
  if (NS_OK == (res = bundle->GetStringFromName(lang.get(), &ptrv))) {

    if (sTrue.Equals(ptrv)) {
      /* lang is accepted */
      return res;
    }
  }

  /* unsupported lang */
  *_retval = nsnull;
  return NS_ERROR_FAILURE;
}