// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses // without any warranty. // by Gregory Pakosz (@gpakosz) // https://github.com/gpakosz/whereami // in case you want to #include "whereami.c" in a larger compilation unit #if !defined(WHEREAMI_H) #include #endif #ifdef __cplusplus extern "C" { #endif #if defined(__linux__) || defined(__CYGWIN__) #undef _DEFAULT_SOURCE #define _DEFAULT_SOURCE #elif defined(__APPLE__) #undef _DARWIN_C_SOURCE #define _DARWIN_C_SOURCE #define _DARWIN_BETTER_REALPATH #endif #if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC) #include #endif #if !defined(WAI_MALLOC) #define WAI_MALLOC(size) malloc(size) #endif #if !defined(WAI_FREE) #define WAI_FREE(p) free(p) #endif #if !defined(WAI_REALLOC) #define WAI_REALLOC(p, size) realloc(p, size) #endif #ifndef WAI_NOINLINE #if defined(_MSC_VER) #define WAI_NOINLINE __declspec(noinline) #elif defined(__GNUC__) #define WAI_NOINLINE __attribute__((noinline)) #else #error unsupported compiler #endif #endif #if defined(_MSC_VER) #define WAI_RETURN_ADDRESS() _ReturnAddress() #elif defined(__GNUC__) #define WAI_RETURN_ADDRESS() \ __builtin_extract_return_addr(__builtin_return_address(0)) #else #error unsupported compiler #endif #if defined(_WIN32) #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #if defined(_MSC_VER) #pragma warning(push, 3) #endif #include #include #if defined(_MSC_VER) #pragma warning(pop) #endif #include static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length) { wchar_t buffer1[MAX_PATH]; wchar_t buffer2[MAX_PATH]; wchar_t* path = NULL; int length = -1; bool ok; for (ok = false; !ok; ok = true) { DWORD size; int length_, length__; size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0])); if (size == 0) break; else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0]))) { DWORD size_ = size; do { wchar_t* path_; path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2); if (!path_) break; size_ *= 2; path = path_; size = GetModuleFileNameW(module, path, size_); } while (size == size_); if (size == size_) break; } else path = buffer1; if (!_wfullpath(buffer2, path, MAX_PATH)) break; length_ = (int)wcslen(buffer2); length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, out, capacity, NULL, NULL); if (length__ == 0) length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL); if (length__ == 0) break; if (length__ <= capacity && dirname_length) { int i; for (i = length__ - 1; i >= 0; --i) { if (out[i] == '\\') { *dirname_length = i; break; } } } length = length__; } if (path != buffer1) WAI_FREE(path); return ok ? length : -1; } WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getExecutablePath)( char* out, int capacity, int* dirname_length) { return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length); } WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) { HMODULE module; int length = -1; #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable : 4054) #endif if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module)) #if defined(_MSC_VER) #pragma warning(pop) #endif { length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length); } return length; } #elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || \ defined(WAI_USE_PROC_SELF_EXE) #include #include #include #if defined(__linux__) #include #else #include #endif #ifndef __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS #endif #include #include #if !defined(WAI_PROC_SELF_EXE) #if defined(__sun) #define WAI_PROC_SELF_EXE "/proc/self/path/a.out" #else #define WAI_PROC_SELF_EXE "/proc/self/exe" #endif #endif WAI_FUNCSPEC int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) { char buffer[PATH_MAX]; char* resolved = NULL; int length = -1; bool ok; for (ok = false; !ok; ok = true) { resolved = realpath(WAI_PROC_SELF_EXE, buffer); if (!resolved) break; length = (int)strlen(resolved); if (length <= capacity) { memcpy(out, resolved, length); if (dirname_length) { int i; for (i = length - 1; i >= 0; --i) { if (out[i] == '/') { *dirname_length = i; break; } } } } } return ok ? length : -1; } #if !defined(WAI_PROC_SELF_MAPS_RETRY) #define WAI_PROC_SELF_MAPS_RETRY 5 #endif #if !defined(WAI_PROC_SELF_MAPS) #if defined(__sun) #define WAI_PROC_SELF_MAPS "/proc/self/map" #else #define WAI_PROC_SELF_MAPS "/proc/self/maps" #endif #endif #if defined(__ANDROID__) || defined(ANDROID) #include #include #include #endif #include WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) { int length = -1; FILE* maps = NULL; for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r) { maps = fopen(WAI_PROC_SELF_MAPS, "r"); if (!maps) break; for (;;) { char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX]; uint64_t low, high; char perms[5]; uint64_t offset; uint32_t major, minor; char path[PATH_MAX]; uint32_t inode; if (!fgets(buffer, sizeof(buffer), maps)) break; if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8) { uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS(); if (low <= addr && addr <= high) { char* resolved; resolved = realpath(path, buffer); if (!resolved) break; length = (int)strlen(resolved); #if defined(__ANDROID__) || defined(ANDROID) if (length > 4 && buffer[length - 1] == 'k' && buffer[length - 2]
/*
 * cocos2d for iPhone: http://www.cocos2d-iphone.org
 *
 * Copyright (C) 2010 Lam Pham
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


#import "CCActionProgressTimer.h"

#define kProgressTimerCast CCProgressTimer*

@implementation CCProgressTo
+(id) actionWithDuration: (ccTime) t percent: (float) v
{
	return [[[ self alloc] initWithDuration: t percent: v] autorelease];
}

-(id) initWithDuration: (ccTime) t percent: (float) v
{
	if( (self=[super initWithDuration: t] ) )
		to_ = v;
	
	return self;
}

-(id) copyWithZone: (NSZone*) zone
{
	CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration:duration_ percent:to_];
	return copy;
}

-(void) startWithTarget:(id) aTarget;
{
	[super startWithTarget:aTarget];
	from_ = [(kProgressTimerCast)target_ percentage];
	
	// XXX: Is this correct ?
	// Adding it to support CCRepeat
	if( from_ == 100)
		from_ = 0;
}

-(void) update: (ccTime) t
{
	[(kProgressTimerCast)target_ setPercentage: from_ + ( to_ - from_ ) * t];
}
@end

@implementation CCProgressFromTo
+(id) actionWithDuration: (ccTime) t from:(float)fromPercentage to:(float) toPercentage
{
	return [[[self alloc] initWithDuration: t from: fromPercentage to: toPercentage] autorelease];
}

-(id) initWithDuration: (ccTime) t from:(float)fromPercentage to:(float) toPercentage
{
	if( (self=[super initWithDuration: t] ) ){
		to_ = toPercentage;
		from_ = fromPercentage;
	}
	return self;
}

-(id) copyWithZone: (NSZone*) zone
{
	CCAction *copy = [[[self class] allocWithZone: zone] initWithDuration:duration_ from:from_ to:to_];
	return copy;
}

- (CCActionInterval *) reverse
{
	return [[self class] actionWithDuration:duration_ from:to_ to:from_];
}

-(void) startWithTarget:(id) aTarget;
{
	[super startWithTarget:aTarget];
}

-(void) update: (ccTime) t
{
	[(kProgressTimerCast)target_ setPercentage: from_ + ( to_ - from_ ) * t];
}
@end