- (NSString *)escape:(NSObject *)value {
if (value == nil)
return nil;
NSString *escapedValue = nil;
if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSMutableString class]]) {
NSString *valueString = (NSString *) value;
char *theEscapedValue = sqlite3_mprintf("'%q'", [valueString UTF8String]);
escapedValue = [NSString stringWithUTF8String:(const char *)theEscapedValue];
sqlite3_free(theEscapedValue);
} else
escapedValue = [NSString stringWithFormat:@"%@", value];
return escapedValue;
}Objective-C: A Function For Escaping Values Before Inserting Into SQLite
Get a List Of Tables In An SQLite Database
Here is how to do it:
SELECT * FROM sqlite_master WHERE type='table';
How Not To Write Code
I’m working on a website and it’s absolutely awful, almost all the design choices are bad so I will compile a list here as I encounter them:
1 – Don’t have fields in your database like “extra1″, “extra2″ or “extra3″ you probably need to refactor and rethink your design…
2 – Don’t have a global object and access and manipulate it from deep inside your code, here for example there is a $tpl variable which – wrongly – is called $class_tpl and it’s global, the authors are calling a pager function to create an array for rendering a pager and after they call they manipulate the $class_tpl a bit by adding paging data to it and you would think that was it, but guess what? In their pager function they again access $class_tpl and manipulate a bit more!!!
3 – Don’t ever, ever, ever, ever query your database from within your templates, never…
4 – Don’t die or exit out of functions, return error messages…
5 – Don’t output out of function either, no “echo $blah;” in a function, they should return the text string rather than printing it…
6 – Don’t ever, ever, ever repeat yourself, if you are too lazy to write a function or rethink that portion of your code, then you suck as a programmer… To be honest, I do suck sometimes, but I try my very best
C: Determining Size Of a Malloced C Pointer At Runtime
There is no direct/cross-platform way of doing this but you could do something cool, you could do what malloc does… (Although malloc stores more data than this)
Say you want to allocate a chunk that is 100 bytes and you want to somehow attach this size to it, so you can check the size anytime later on, what you do is this: you allocate 100 bytes + sizeof(size_t) and then store the size of this chunk which is 100 at the beginning of the chunk, then you return the address of the chunk starting right after the 100:
struct chunk_header { size_t size; }; void *my_malloc(size_t size) { size_t header_size = sizeof(struct chunk_header); size_t alloc_size = header_size + size; void *chunk = malloc(alloc_size); struct chunk_header *header; if (!chunk) return NULL; header = (struct chunk_header *) chunk; header->size = alloc_size; /* Or just size, it's up to you... */ return (char *) chunk + header_size; /* char * hack, go look it up... */ }
Later on, you can check the size like so:
size_t header_size = sizeof(struct chunk_header) void *chunk = (char *) p - header_size; struct chunk_header *header = (struct chunk_header *) chunk; printf("This chunk of memory is: %d bytes...\n", (int) header->size);
Is The New GPU In Macbook Pro 13-inch Slower
Decide for yourself:
| Test | New: Intel HD 3000 | Old: NVIDIA GeForce 320M |
| 3DMark 2001SE | 18051 | 16219 |
| 3DMark 03 | 11406.5 | 10494 |
| 3DMark 05 | 8695.8 | 7168.3 |
| 3DMark 06 | 5053 | 4346.5 |
| PCMark Vantage | 6365 | 3816 |
| Cinebench R10 | 6574.3 | 3015.3 |
| 3DMark Vantage | 1280.8 | 1543.4 |
| Windows 7 Experience Index – Graphics | 6 | 5.2 |
| Windows 7 Experience Index – Gaming graphics | 6.3 | 6 |
| Cinebench R11.5 – OpenGL 64Bit | 10.4 | 11.3 |
This is aside from the big CPU upgrade…
Taken from:
http://www.notebookcheck.net/Intel-HD-Graphics-3000.37948.0.html
&
http://www.notebookcheck.net/NVIDIA-GeForce-320M.28701.0.html
Xcode: Embedding Lua In iPhone Apps
This is very simple, even though I can’t find so many resource regarding this online; follow these steps:
1- Download the latest version of Lua from: http://www.lua.org/ftp/
2- Obviously unpack it and find the “src” folder.
3- Delete the files: “lua.c”, “luac.c” and “print.c”
4- Grab the “src” file and drop it in your project’s left pane, under your projects’s name along with “Classes”, “Resources” etc.
5- Rename this newly added “src” to “lua”
Now you should be able to follow these types of tutorials:
http://www.ibm.com/developerworks/opensource/library/l-embed-lua/index.html
Hope this works for you!
C: Set An Entire Array Of Pointers To NULL
Apparently you can’t use memset() to do this:
memset(the_pointer, '0', sizeof(the array));
This will set all the bits in the array to zero but 0 is not guaranteed to be the representation of a NULL pointer so this might be true:
if (the_pointer_array[10] != NULL) {}
You must loop through and set them one by one…
Importing GeoWorldMap Database Into MySQL Using PHP
First download the database from:
http://www.geobytes.com/freeservices.htm
Then, create a folder in your server where you can access it via a browser and upload the files in there, then save this script as import.php and upload it into the same folder and then point your browser to this PHP script you just uploaded. Note that you must edit the script and add your own database name/username/password to it before uploading.
<?php $db_info = array( 'host' => 'localhost', 'name' => 'EDIT ME', 'user' => 'EDIT ME', 'pass' => 'EDIT ME' ); error_reporting(E_ALL); ob_end_clean(); function out($string) { echo $string; flush(); } out("<pre>"); function map_fields($table, $row, $index) { if ($index == 0) return NULL; $retval = array(); switch ($table) { case 'country': $retval = array( 'id' => $row[0], 'name' => $row[1] ); break; case 'region': $retval = array( 'id' => $row[0], 'country_id' => $row[1], 'name' => $row[2] ); break; case 'city': $retval = array( 'id' => $row[0], 'country_id' => $row[1], 'region_id' => $row[2], 'name' => $row[3] ); break; default: exit("Fatal: Called map_fields with unsupported table: $table\n\n"); } return $retval; } function do_mysql_query($query) { if (!mysql_query($query)) exit("\n\nFatal: do_mysql_query failed with MySQL error: " .mysql_error() ."\n---------------------\nAnd query: $query\n\n"); } function create_table($table) { out("Creating table: $table..."); do_mysql_query(" DROP TABLE IF EXISTS codehead_$table "); $query = ""; switch ($table) { case 'country': $query = " CREATE TABLE codehead_country ( id INT NOT NULL PRIMARY KEY, name VARCHAR(100), index (name) ) "; break; case 'region': $query = " CREATE TABLE codehead_region ( id INT NOT NULL PRIMARY KEY, country_id INT NOT NULL, name VARCHAR(100), index (name), index (country_id, name) ) "; break; case 'city': $query = " CREATE TABLE codehead_city ( id INT NOT NULL PRIMARY KEY, country_id INT NOT NULL, region_id INT NOT NULL, name VARCHAR(100), index (name), index (region_id, name), index (country_id, region_id, name) ) "; break; default: exit("Fatal: Called create_table with unsupported table: $table\n\n"); } do_mysql_query($query); out("Done!\n"); } function empty_table($table) { do_mysql_query("DELETE FROM codehead_$table"); } function insert_row_into_table($table, $row) { $query = ""; switch ($table) { case 'country': $id = intval($row['id']); $name = mysql_real_escape_string($row['name']); $query = " INSERT INTO codehead_country VALUES ( $id, '$name' ) "; break; case 'region': $id = intval($row['id']); $country_id = intval($row['country_id']); $name = mysql_real_escape_string($row['name']); $query = " INSERT INTO codehead_region VALUES ( $id, $country_id, '$name' ) "; break; case 'city': $id = intval($row['id']); $country_id = intval($row['country_id']); $region_id = intval($row['region_id']); $name = mysql_real_escape_string($row['name']); $query = " INSERT INTO codehead_city VALUES ( $id, $country_id, $region_id, '$name' ) "; break; default: exit("Fatal: Called insert_row_into_db with unsupported table: $table\n\n"); } do_mysql_query($query); } $base_dir = dirname(__FILE__); $tables = array( $base_dir .'/Countries.txt' => 'country', $base_dir .'/Regions.txt' => 'region', $base_dir .'/Cities.txt' => 'city' ); $db = mysql_connect($db_info['host'], $db_info['user'], $db_info['pass']); if (!$db) exit("Fatal: Couldn't connect to MySQL...\n\n"); if (!mysql_selectdb($db_info['name'])) exit("Fatal: Couldn't select database...\n\n"); foreach ($tables as $file => $table) { if (($fp = fopen($file, 'r')) !== false) { create_table($table); out("Inserting rows into table: $table"); $index = 0; while (($data = fgetcsv($fp, 10000)) !== false) { if ($index == 0) { ++$index; continue; } $row = map_fields($table, $data, $index); insert_row_into_table($table, $row); ++$index; out("."); } fclose($fp); out("Done!\n"); } else exit("Fatal: Couldn't open file: $file\n\n"); } out("All done!\n\n"); mysql_close($db); ?>
OpenGL ES: Unbinding Vertex Buffer Objects – VBOs
I couldn’t find this mentioned anywhere online, but the solution is very easy:
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
I hope this helps!
NSNotificationCenter Event Name List
I don’t know why I can’t find anything about this online but here are some of the events that the application sends out through NSNotificationCenter:
UIApplicationDidFinishLaunchingNotification
UIApplicationDidBecomeActiveNotification
UIApplicationWillResignActiveNotification
UIApplicationDidReceiveMemoryWarningNotification
UIApplicationWillTerminateNotification
UIApplicationSignificantTimeChangeNotification
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIApplicationStatusBarOrientationUserInfoKey
UIApplicationWillChangeStatusBarFrameNotification
UIApplicationDidChangeStatusBarFrameNotification
UIApplicationStatusBarFrameUserInfoKey
Use them like:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod) name:UIApplicationWillResignActiveNotification object:nil];
It’s obvious what they are but if you have a question post a comment…
I'm a programmer at 