Tag Archive for textmate

objects into array

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

source

rand64

#include "stdlib.h"
#include "stdint.h"

uint64_t rand64()
{
	// Combin 4 parts of low 16-bit of each rand()
	uint64_t R0 = (uint64_t)random() << 48;
	uint64_t R1 = (uint64_t)random() << 48 >> 16;
	uint64_t R2 = (uint64_t)random() << 48 >> 32;
	uint64_t R3 = (uint64_t)random() << 48 >> 48;
	return R0 | R1 | R2 | R3;
}

#define SEGS 100
#define TIMES 50000

// Show segments
int main (int argc, char const *argv[])
{
	const uint64_t SEG = UINT64_MAX / SEGS;
	int f[SEGS] = { 0 };
	for(int i = 0; i < TIMES; ++i)
	{
		uint64_t r = rand64();
		f[r / SEG]++;
	}
	for (int i = 0; i < SEGS; ++i)
	{
		printf("#%2d: (%20llu ~ %20llu) %dn", i, i * SEG, (i + 1) * SEG - 1, f[i]);
	}
	return 0;
}

source

deblank

// Remove multiple space characters in a string.
// Ansi string and UTF-8 only!

void deblank(char* string)
{
	char* s = string;
	char* c = string;
	do
	{
		while (*c == ' ') c++;
		*s++ = *c;
		if (*c == ' ') break;
		if (*++c == ' ')
		{ 
			*s++ = ' ';
			c++;
		}
	} while (1);
}

int main()
{
	char s[] = "abc def  ghj   klmn.";
	deblank(s);
	printf("%sn", s);
	return 0;
}

source

substr.c

// Extract a substring from an existing string, return the length of the substring
// dst: receive the substring
// src: the source string to extract from
// start: extract start with the 'src' + 'start' char, zero based; no bound check
// len: length of the substring; may be shorter than expected.
// Ansi string only!

int substr(char* dst, char* src, int start, int len)
{
	char* d = dst;
	char* s = src + start;
	while (len-- > 0)
	{
		if (*s == ' ')
		{
			*d = '';
			return d - dst; 
		}
		*d++ = *s++;
	}
	*d = ' ';
	return d - dst;
}

int main()
{
	char s[1024] = { ' ' };
	printf("%dt%sn", substr(s, "0123456789", 3, 10), s);
}

source

convert int to binary in c

#define WIDTH 32

const char *int_to_binary ( int x ) 
{ 
	static char b[WIDTH + 1] = {0};
	for (int z = WIDTH; z > 0; z--) 
	{ 
		b[z - 1] = ((1 & x) == 1) ? '1' : '0'; 
		x >>= 1; 
	}
	return b; 
}

int main() 
{ 
	unsigned int i = 0; 
	i = ~i; 
	printf("%s
", int_to_binary(i)); 
	return 0; 
}

source

jQuery dynamic loader – load jquery using pure js javascript if jQ is undefined or does not exist

// jQuery dynamic loader - load jQuery using pure javascript js
// if jQuery is undefined or does not exist, then load it

if ( !window.jQuery ) {
	var jqscript = document.createElement('script');
	jqscript.setAttribute("type", "text/javascript");
	jqscript.setAttribute("src", "<a href="http://code.jquery.com/jquery.min.js");" >http://code.jquery.com/jquery.min.js");</a>
	document.getElementsByTagName("head")[0].appendChild(jqscript);
};

source

javascript function within a function to repeat itself js jquery jq loop looping

// animation functions
		zlpFX = {
			show1: function() {
				$('div#mainBackground2').fadeOut(3000);
				$('div#transMoving').animate({
					paddingLeft: "180px",
					paddingTop: "20px"
				},
				4000).find('h3').fadeOut(2000);
				setTimeout("zlpFX.show2()", 14000);
			},
			show2: function() {
				$('div#mainBackground2').fadeIn(4000);
				$('div#transMoving').animate({
					paddingLeft: '220px',
					paddingTop: '75px'
				},
				4500).find('h3').fadeIn(6000);
				setTimeout("zlpFX.show1()", 14000);
			}
		};




		// now start the animation functions
		setTimeout("zlpFX.show1()", 11000);

source

Detect Ajax

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	/* special ajax here */
	die($content);
}

source

Generate thumbnail with ImageTool

ImageTool imgTool = new ImageTool()
imgTool.load($1.getBytes())
imgTool.thumbnail(260)
return imgTool.getBytes("JPEG")

source

MOOTOOLS JS – Clear input field onClick onFocus on click focus active

// 201004291250 - Mootools clear input on focus, return default value if no data entered
// FROM:  Clear text field with mootools on focus. - MooTools Users | Google Groups <a href="http://bit.ly/cHPev3" >http://bit.ly/cHPev3</a>

/* 
	I've got this snippet that will clear the value on focus and if the value of 
	the focus is '' it will put the default value again in the input. You will 
	need the default value to be on an attribute of the input, like alt or 
	anyother (if you don't bother about validation). 
	
	Fábio Miranda Costa 
	Engenheiro de Computação 
	<a href="http://meiocodigo.com" >http://meiocodigo.com</a> 

*/

// FUNCTION: 

Element.implement({ 
    clearFocusResetBlur: function(attr){ 
        var valueString = this.get(attr); 
        this.addEvents({ 
            'focus': function(){ 
                if( this.get('value') == valueString ) this.set('value',''); 
            }, 
            'blur': function(){ 
                if( this.get('value') == "" ) this.set('value',valueString); 
            } 
        }); 
    } 
}); 


// Usage
// HTML: 
// <input id="input_id" value="some value" alt="default value" /> 


// JS: 
$('.jform form .cleardefault').set('alt','value').clearFocusResetBlur('alt');

source