Example #1
0
void DictLookup(Dict *in, char *key, int *DataTypeOut, void **ptrout)
 {
  int hash;
  DictItem *ptr;

  if (in==NULL) { *ptrout=NULL; return; }

#define DICTLOOKUP_TEST \
   if (strcmp(ptr->key, key) == 0) \
    { \
     if (DataTypeOut != NULL) *DataTypeOut = ptr->DataType; \
     *ptrout = ptr->data; \
     return; \
    }

  // Check hash table
  hash = DictHash(key, in->HashSize);
  ptr  = in->HashTable[hash];
  if (ptr==NULL) { *ptrout=NULL; return; }
  DICTLOOKUP_TEST;

  // Hash table clash; need to exhaustively search dictionary
  ptr = in->first;
  while (ptr != NULL)
   {
    DICTLOOKUP_TEST
    else if (StrCmpNoCase(ptr->key, key) > 0) break;
    ptr = ptr->next;
   }
  *ptrout = NULL;
  return;
 }
Example #2
0
File: image.c Project: Israel-/jwm
/** Load an image from the specified file. */
ImageNode *LoadImage(const char *fileName, int rwidth, int rheight,
                     char preserveAspect)
{
   unsigned i;
   unsigned name_length;
   ImageNode *result = NULL;

   /* Make sure we have a reasonable file name. */
   if(!fileName) {
      return result;
   }
   name_length = strlen(fileName);
   if(JUNLIKELY(name_length == 0)) {
      return result;
   }

   /* Make sure the file exists. */
   if(access(fileName, R_OK) < 0) {
      return result;
   }

   /* First we attempt to use the extension to determine the type
    * to avoid trying all loaders. */
   for(i = 0; i < IMAGE_LOADER_COUNT; i++) {
      const char *ext = IMAGE_LOADERS[i].extension;
      const unsigned ext_length = strlen(ext);
      if(JLIKELY(name_length >= ext_length)) {
         const unsigned offset = name_length - ext_length;
         if(!StrCmpNoCase(&fileName[offset], ext)) {
            const ImageLoader loader = IMAGE_LOADERS[i].loader;
            result = (loader)(fileName, rwidth, rheight, preserveAspect);
            if(JLIKELY(result)) {
               return result;
            }
            break;
         }
      }
   }

   /* We were unable to load by extension, so try everything. */
   for(i = 0; i < IMAGE_LOADER_COUNT; i++) {
      const ImageLoader loader = IMAGE_LOADERS[i].loader;
      result = (loader)(fileName, rwidth, rheight, preserveAspect);
      if(result) {
         /* We were able to load the image, so it must have either the
          * wrong extension or an extension we don't recognize. */
         Warning(_("unrecognized extension for \"%s\", expected \"%s\""),
                 fileName, IMAGE_LOADERS[i].extension);
         return result;
      }
   }

   /* No image could be loaded. */
   return result;
}
Example #3
0
void DictAppendPtrCpy(Dict *in, char *key, void *item, int size, int DataType)
 {
  DictItem *ptr=NULL, *ptrnew=NULL, *prev=NULL;
  char     *newstr;
  int       cmp = -1, hash;

  ptr = in->first;
  while (ptr != NULL)
   {
    if ( ((cmp = StrCmpNoCase(ptr->key, key)) > 0) || ((cmp = strcmp(ptr->key, key)) == 0) ) break;
    prev = ptr;
    ptr  = ptr->next;
   }
  if (cmp == 0) // Overwrite an existing entry in dictionary
   {
    if ((size != ptr->DataSize) || (ptr->MallocedByUs == 0))
     {
      ptr->data     = (void *)lt_malloc_incontext(size , in->memory_context);
      ptr->DataSize = size;
      ptr->MallocedByUs = 1;
     }
    memcpy(ptr->data , item, size);
    ptr->DataType = DataType;
    ptrnew = ptr;
   }
  else
   {
    ptrnew           = (DictItem *)lt_malloc_incontext(sizeof(DictItem), in->memory_context);
    if (ptrnew==NULL) return;
    ptrnew->prev     = prev;
    ptrnew->next     = ptr;
    ptrnew->key      = (char *)lt_malloc_incontext((strlen(key)+1)*sizeof(char), in->memory_context);
    if (ptrnew->key==NULL) return;
    strcpy(ptrnew->key, key);
    ptrnew->data     = (void *)lt_malloc_incontext(size, in->memory_context);
    if (ptrnew->data==NULL) return;
    memcpy(ptrnew->data, item, size);
    ptrnew->MallocedByUs = 1;
    ptrnew->copyable = 1;
    ptrnew->DataType = DataType;
    ptrnew->DataSize = size;
    if (prev == NULL) in->first = ptrnew; else prev->next = ptrnew;
    if (ptr  == NULL) in->last  = ptrnew; else ptr ->prev = ptrnew;
    in->length++;

    hash = DictHash(key, in->HashSize);
    in->HashTable[hash] = ptrnew;
   }
  if ((ptrnew->DataType == DATATYPE_VALUE) && (((value *)ptrnew->data)->string != NULL))
   {
    newstr = (char *)lt_malloc_incontext(strlen(((value *)ptrnew->data)->string)+1, in->memory_context); // Copy strings in string values
    strcpy(newstr, ((value *)ptrnew->data)->string);
    ((value *)ptrnew->data)->string = newstr;
   }
 }
Example #4
0
void DictAppendPtr(Dict *in, char *key, void *item, int size, int copyable, int DataType)
 {
  DictItem *ptr=NULL, *ptrnew=NULL, *prev=NULL;
  int       cmp = -1, hash;

  ptr = in->first;
  while (ptr != NULL)
   {
    if ( ((cmp = StrCmpNoCase(ptr->key, key)) > 0) || ((cmp = strcmp(ptr->key, key)) == 0) ) break;
    prev = ptr;
    ptr  = ptr->next;
   }
  if (cmp == 0) // Overwrite an existing entry in dictionary
   {
    ptr->data     = item;
    ptr->DataSize = size;
    ptr->DataType = DataType;
    ptr->copyable = copyable;
    ptr->MallocedByUs = 0;
   }
  else
   {
    ptrnew           = (DictItem *)lt_malloc_incontext(sizeof(DictItem), in->memory_context);
    if (ptrnew==NULL) return;
    ptrnew->prev     = prev;
    ptrnew->next     = ptr;
    ptrnew->key      = (char *)lt_malloc_incontext((strlen(key)+1)*sizeof(char), in->memory_context);
    if (ptrnew->key==NULL) return;
    strcpy(ptrnew->key, key);
    ptrnew->data     = item;
    ptrnew->MallocedByUs = 0;
    ptrnew->DataType = DataType;
    ptrnew->copyable = copyable;
    ptrnew->DataSize = size;
    if (prev == NULL) in->first = ptrnew; else prev->next = ptrnew;
    if (ptr  == NULL) in->last  = ptrnew; else ptr ->prev = ptrnew;
    in->length++;

    hash = DictHash(key, in->HashSize);
    in->HashTable[hash] = ptrnew;
   }
 }
Example #5
0
int  DictRemoveKey(Dict *in, char *key)
 {
  int hash;
  DictItem *ptr;

  if (in==NULL) return -1;

  // Check hash table
  hash = DictHash(key, in->HashSize);
  ptr  = in->HashTable[hash];
  if (ptr==NULL) return -1;
  if (strcmp(ptr->key, key)==0) { _DictRemoveEngine(in, ptr); return 0; }

  // Hash table clash; need to exhaustively search dictionary
  ptr = in->first;
  while (ptr != NULL)
   {
    if (strcmp(ptr->key, key)==0) { _DictRemoveEngine(in, ptr); return 0; }
    else if (StrCmpNoCase(ptr->key, key) > 0) break;
    ptr = ptr->next;
   }
  return -1;
 }
Example #6
0
File: image.c Project: Miteam/jwm
/** Load an image from the specified file. */
ImageNode *LoadImage(const char *fileName, int width, int height,
                     char preserveAspect)
{
   unsigned nameLength;
   ImageNode *result = NULL;
   if(!fileName) {
      return result;
   }

   nameLength = strlen(fileName);
   if(JUNLIKELY(nameLength == 0)) {
      return result;
   }

   /* Attempt to load the file as a PNG image. */
#ifdef USE_PNG
   if(nameLength >= 4
      && !StrCmpNoCase(&fileName[nameLength - 4], ".png")) {
      result = LoadPNGImage(fileName);
      if(result) {
         return result;
      }
   }
#endif

   /* Attempt to load the file as a JPEG image. */
#ifdef USE_JPEG
   if(   (nameLength >= 4
            && !StrCmpNoCase(&fileName[nameLength - 4], ".jpg"))
      || (nameLength >= 5
            && !StrCmpNoCase(&fileName[nameLength - 5], ".jpeg"))) {
      result = LoadJPEGImage(fileName, width, height);
      if(result) {
         return result;
      }
   }
#endif

   /* Attempt to load the file as an SVG image. */
#if defined USE_RSVG || defined USE_NANOSVG 
   if(nameLength >= 4
      && !StrCmpNoCase(&fileName[nameLength - 4], ".svg")) {
      result = LoadSVGImage(fileName, width, height, preserveAspect);
      if(result) {
         return result;
      }
   }
#endif

   /* Attempt to load the file as an XPM image. */
#ifdef USE_XPM
   if(nameLength >= 4
      && !StrCmpNoCase(&fileName[nameLength - 4], ".xpm")) {
      result = LoadXPMImage(fileName);
      if(result) {
         return result;
      }
   }
#endif

   /* Attempt to load the file as an XBM image. */
#ifdef USE_XBM
   if(nameLength >= 4
      && !StrCmpNoCase(&fileName[nameLength - 4], ".xbm")) {
      result = LoadXBMImage(fileName);
      if(result) {
         return result;
      }
   }
#endif

   /* Attempt to load a PNG, JPEG, GIf, TGA, BMP, PNM, PSD or PIC image. */
#ifdef USE_STB_IMAGE
   result = LoadstbImage(fileName);
#endif

   return result;

}