<commit_before>/*
 *  nuiNativeResource.cpp
 *  nui3
 *
 *  Created by <NAME> on 20/11/07.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

#include "nui.h"
#include "nuiNativeResource.h"


#ifdef _WIN32_
# include "nglIMemory.h"
#endif // _WIN32_
#if (defined _CARBON_) || (defined _UIKIT_) || (defined _COCOA_)
extern "C"
{
#include <mach-o/dyld.h>
#include <mach-o/ldsyms.h>
}


static CFBundleRef _CFXBundleCreateFromImageName(CFAllocatorRef allocator, const char* image_name);
static CFBundleRef _CFXBundleCreateFromImageName(CFAllocatorRef allocator, const char* image_name)
{
  CFURLRef myBundleExecutableURL = CFURLCreateFromFileSystemRepresentation(allocator, (const unsigned char*)image_name, strlen (image_name), false);
  if (myBundleExecutableURL == 0)
    return 0;
  
  CFURLRef myBundleContentsMacOSURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleExecutableURL); // Delete Versions/Current/Executable
  CFRelease(myBundleExecutableURL);
  if (myBundleContentsMacOSURL == 0)
    return 0;
  
  CFURLRef myBundleContentsURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleContentsMacOSURL); // Delete Current
  CFRelease(myBundleContentsMacOSURL);
  if (myBundleContentsURL == 0)
    return 0;
  
  CFURLRef theBundleURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleContentsURL); // Delete Versions
  CFRelease(myBundleContentsURL);
  if (theBundleURL == 0)
    return 0;
  
  CFBundleRef result = CFBundleCreate(allocator, theBundleURL);
  CFRelease(theBundleURL);
  
  return result;
}

#include <dlfcn.h>
static nglPath GetResourcePath()
{
  Dl_info inf;
  dladdr((void*)GetResourcePath, &inf);
  
  const char* imagename = inf.dli_fname;
  /* get the bundle of a header */
  CFBundleRef bundle = NULL;
  if (imagename)
    bundle = _CFXBundleCreateFromImageName (NULL, imagename);
  
  if (!bundle)
    bundle = CFBundleGetMainBundle();
  
  CFURLRef resURLref = CFBundleCopyResourcesDirectoryURL(bundle);
  char buffer[2048];
  Boolean res = CFURLGetFileSystemRepresentation(resURLref, true, (UInt8*)buffer, 2048);
  CFRelease(resURLref);
  NGL_ASSERT(res);
  
  return nglPath(nglString(buffer));
}
#endif // _CARBON_
#ifdef _LINUX_
static nglPath GetResourcePath()
{
	nglString ResourcePathName(getenv("NUI_RESOURCE_PATH"));
	if (ResourcePathName.IsEmpty())
		ResourcePathName = nglString("/usr/share/");
	if( ResourcePathName.GetLastChar() == _T('/'))
		ResourcePathName += App->GetName();
	else
		ResourcePathName += _T('/')+App->GetName();
	NGL_OUT(_T("NUI_RESOURCE_PATH: %ls\n"), ResourcePathName.GetChars());
	return nglPath(ResourcePathName);
}
#endif //_LINUX_


nuiNativeResource::nuiNativeResource(const nglPath& rPath)
: nglIStream(),
  mpIStream(NULL)
{
  mValid = false;
#ifdef _WIN32_
  nglString str(rPath.GetPathName());
  str.TrimLeft(_T('/'));
  str.ToUpper();
  HRSRC Resource = FindResource(App->GetHInstance(), str.GetChars(), _T("NUI_RESOURCE"));
  
  if (Resource == NULL)
  {
    return;
  }
  
  DWORD size = SizeofResource(App->GetHInstance(), Resource);
  HGLOBAL GlobalHandle = LoadResource(App->GetHInstance(), Resource);
  
  if (GlobalHandle == NULL)
  {
    return;
  }
  
  void* pBuffer = LockResource(GlobalHandle);
  
  uint32 datasize = size;
  char* pData = new char[datasize];
  memcpy(pData, pBuffer, size);
  
  // Init the nglIMemory we inherit from...
  mpIStream = new nglIMemory((char*)pData, datasize);

  UnlockResource(GlobalHandle);
  
  mValid = true;
#endif
#if defined _CARBON_ || defined _UIKIT_ || defined _COCOA_ || defined _LINUX_
  nglPath resourcePath(GetResourcePath());
  resourcePath += rPath;
  
  if (resourcePath.Exists())
  {
    mpIStream = resourcePath.OpenRead();
    if (!mpIStream)
      NGL_OUT(_T("nuiNativeResource opening failed with path '%ls'\n"), resourcePath.GetChars());
    NGL_ASSERT(mpIStream);
    mValid = true;
  }
#endif // _CARBON_ || _UIKIT_ || _COCOA_ || _LINUX_
}

nuiNativeResource::~nuiNativeResource()
{
  delete mpIStream;
}
<commit_msg>quick fix for getting the bundle path on iphoneOS<commit_after>/*
 *  nuiNativeResource.cpp
 *  nui3
 *
 *  Created by <NAME> on 20/11/07.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

#include "nui.h"
#include "nuiNativeResource.h"


#ifdef _WIN32_
# include "nglIMemory.h"
#endif // _WIN32_
#if (defined _CARBON_) || (defined _UIKIT_) || (defined _COCOA_)
extern "C"
{
#include <mach-o/dyld.h>
#include <mach-o/ldsyms.h>
}


static CFBundleRef _CFXBundleCreateFromImageName(CFAllocatorRef allocator, const char* image_name);
static CFBundleRef _CFXBundleCreateFromImageName(CFAllocatorRef allocator, const char* image_name)
{
  CFURLRef myBundleExecutableURL = CFURLCreateFromFileSystemRepresentation(allocator, (const unsigned char*)image_name, strlen (image_name), false);
  if (myBundleExecutableURL == 0)
    return 0;
  
  CFURLRef myBundleContentsMacOSURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleExecutableURL); // Delete Versions/Current/Executable
  CFRelease(myBundleExecutableURL);
  if (myBundleContentsMacOSURL == 0)
    return 0;
  
  CFURLRef myBundleContentsURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleContentsMacOSURL); // Delete Current
  CFRelease(myBundleContentsMacOSURL);
  if (myBundleContentsURL == 0)
    return 0;
  
  CFURLRef theBundleURL = CFURLCreateCopyDeletingLastPathComponent(allocator, myBundleContentsURL); // Delete Versions
  CFRelease(myBundleContentsURL);
  if (theBundleURL == 0)
    return 0;
  
  CFBundleRef result = CFBundleCreate(allocator, theBundleURL);
  CFRelease(theBundleURL);
  
  return result;
}

#include <dlfcn.h>
static nglPath GetResourcePath()
{
  CFBundleRef bundle = NULL;
  
#ifndef _UIKIT_
  Dl_info inf;
  dladdr((void*)GetResourcePath, &inf);

  const char* imagename = inf.dli_fname;
  /* get the bundle of a header */
  if (imagename)
    bundle = _CFXBundleCreateFromImageName (NULL, imagename);
#endif

  if (!bundle)
    bundle = CFBundleGetMainBundle();
  
  CFURLRef resURLref = CFBundleCopyResourcesDirectoryURL(bundle);
  char buffer[2048];
  Boolean res = CFURLGetFileSystemRepresentation(resURLref, true, (UInt8*)buffer, 2048);
  CFRelease(resURLref);
  NGL_ASSERT(res);
  
  return nglPath(nglString(buffer));
}
#endif // _CARBON_
#ifdef _LINUX_
static nglPath GetResourcePath()
{
	nglString ResourcePathName(getenv("NUI_RESOURCE_PATH"));
	if (ResourcePathName.IsEmpty())
		ResourcePathName = nglString("/usr/share/");
	if( ResourcePathName.GetLastChar() == _T('/'))
		ResourcePathName += App->GetName();
	else
		ResourcePathName += _T('/')+App->GetName();
	NGL_OUT(_T("NUI_RESOURCE_PATH: %ls\n"), ResourcePathName.GetChars());
	return nglPath(ResourcePathName);
}
#endif //_LINUX_


nuiNativeResource::nuiNativeResource(const nglPath& rPath)
: nglIStream(),
  mpIStream(NULL)
{
  mValid = false;
#ifdef _WIN32_
  nglString str(rPath.GetPathName());
  str.TrimLeft(_T('/'));
  str.ToUpper();
  HRSRC Resource = FindResource(App->GetHInstance(), str.GetChars(), _T("NUI_RESOURCE"));
  
  if (Resource == NULL)
  {
    return;
  }
  
  DWORD size = SizeofResource(App->GetHInstance(), Resource);
  HGLOBAL GlobalHandle = LoadResource(App->GetHInstance(), Resource);
  
  if (GlobalHandle == NULL)
  {
    return;
  }
  
  void* pBuffer = LockResource(GlobalHandle);
  
  uint32 datasize = size;
  char* pData = new char[datasize];
  memcpy(pData, pBuffer, size);
  
  // Init the nglIMemory we inherit from...
  mpIStream = new nglIMemory((char*)pData, datasize);

  UnlockResource(GlobalHandle);
  
  mValid = true;
#endif
#if defined _CARBON_ || defined _UIKIT_ || defined _COCOA_ || defined _LINUX_
  nglPath resourcePath(GetResourcePath());
  resourcePath += rPath;

  if (resourcePath.Exists())
  {
    mpIStream = resourcePath.OpenRead();
    if (!mpIStream)
      NGL_OUT(_T("nuiNativeResource opening failed with path '%ls'\n"), resourcePath.GetChars());
    NGL_ASSERT(mpIStream);
    mValid = true;
  }
#endif // _CARBON_ || _UIKIT_ || _COCOA_ || _LINUX_
}

nuiNativeResource::~nuiNativeResource()
{
  delete mpIStream;
}

{'fraction_non_alphanumeric': 0.12011863568956994, 'fraction_numerical': 0.008897676717745922, 'mean_word_length': 4.990377498149519, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7034230', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2810, 'n_words': 573}