PHP parse_ini_advanced($filename)

22 03 2015

So, just to kick off.

I had this little problem with the parse_ini_file, in that it is a little limited.
It just doesn’t agree creating anything but first and second-level arrays, and I needed a bit more flexibility, so what does one do?

Go hunting, as you do.. Google is your friend, but this time, it was a bit of a let-down.
I wanted to be able to type

[some:sub]
a.b = 123

and have that turned into the equivalent of:

$conf[‘some’][‘sub’][‘a’][‘b’] = 123 ;

So, there were some stuff out there, but all involved multiple functions, classes, or were incomplete in function, or, using eval to get there.
It has to be simpler, methinked…
A little bit later, i got something that seems to do the trick, and does it in one go, and actually looks somewhat neat and tidy.

As I realized that there’s others having the same issue, I though, why not share the code…
If you use it, please give me some credit – a thank you would be nice, just to know it is being used =)

It’s a drop-in replacement for the parse_ini_file, just don’t feed it the “true” as second argument, or it won’t be happy.

<?php
// (C) 2015 Chris Sprucefield
// Advanced PHP .INI file reader. 
// 
// Sections such as [ abc:def ] will be put into $var['abc']['def'] = values
// Names such as a.b.c and a:b:c will be expanded into sublevels. 
// See reference at the end of the file. 

$conf = parse_ini_advanced("test.ini") ;
print_r($conf) ;

function parse_ini_advanced($file, $conf=array())
{
    if ($conf===array() && $file != "") { $conf = parse_ini_file ($file,true) ; }
    
    // Walk the list of first-level vars. 
    foreach($conf as $key => $val)
    {
        // Match : and . as level separators. 
        if (preg_match('/[:.]+/',$key))
        {
            $list = preg_split('/[:.]+/',$key) ;
            $ptr = &$conf ;
            foreach($list as $name)
            {
                $name = trim($name) ; 
                // Attempting to put in in same var as two data types, is not gonna happen. 
                // Cause the already set string to become a recursion, but don't break execution.
                // such as a.b = 1
                //         a.b[] = ... will point to itself. 
                if (isset($ptr[$name]) && !is_array($ptr[$name]))
                    { $ptr[$name] = array(&$ptr[$name]) ; }
                // If not set, create it... 
                if (!isset($ptr[$name]))
                    { $ptr[$name] = array() ; }

                $ptr = &$ptr[$name] ;
            }
            // Leaf node 
            // If value is an array, insert as a repeated array (a.b[])
            if (isset($ptr) && !is_array($ptr))
            {
                $ptr = array($ptr) ;
                $ptr[] = $val ; 
            } else
            {
                // If the value is an array, check up the names on that one for having dots. 
                $ptr = (is_array($val)) ? array_merge($ptr,(array) parse_ini_advanced("", $val)) : $val ;
            }
            
            unset($conf[$key]) ;
        } else
        {
            // Otherwise, test the subarray for the same things but for second-level. 
            if (is_array($conf[$key]))
                { $conf[$key] = (array) parse_ini_advanced("", $conf[$key]) ; }
        }
    }
    return($conf) ; 
}

/*
    Reference test.ini file. 
    
root_a = 1
root_b = 2

[First : Second : Third]   
    fst_a = 1
    fst_b = 2 
    fst_sub1.sub2.sub3 = 3

[First.Second]   
    fs_a = 99
    fs_b.c[] = 1 
    fs_b.c[] = 2 
    fs_b.c[] = 3 
    fs_b.c[] = 4 
    fs_b.c[] = 5 
    fs_d:e = 10
    fs_sub1.sub2.sub3 = 100

[First]
    f_a = 1
    f_b = 2 
    f_sub1.sub2.sub3 = 3


[SomeOther]
    so_a = 1
    so_b = 2
    so_sub1.sub2.sub3 = 3    
-------------------------------------------------------    
This is parsed as:
-------------------------------------------------------
 
Array
(
    [root_a] => 1
    [root_b] => 2
    [First] => Array
        (
            [f_a] => 1
            [f_b] => 2
            [Second] => Array
                (
                    [Third] => Array
                        (
                            [fst_a] => 1
                            [fst_b] => 2
                            [fst_sub1] => Array
                                (
                                    [sub2] => Array
                                        (
                                            [sub3] => 3
                                        )

                                )

                        )

                    [fs_a] => 99
                    [fs_b] => Array
                        (
                            [c] => Array
                                (
                                    [0] => 1
                                    [1] => 2
                                    [2] => 3
                                    [3] => 4
                                    [4] => 5
                                )

                        )

                    [fs_d] => Array
                        (
                            [e] => 10
                        )

                    [fs_sub1] => Array
                        (
                            [sub2] => Array
                                (
                                    [sub3] => 100
                                )

                        )

                )

            [f_sub1] => Array
                (
                    [sub2] => Array
                        (
                            [sub3] => 3
                        )

                )

        )

    [SomeOther] => Array
        (
            [so_a] => 1
            [so_b] => 2
            [so_sub1] => Array
                (
                    [sub2] => Array
                        (
                            [sub3] => 3
                        )

                )

        )

)

*/




Right, Time to start blogging again…

22 03 2015

It’s been some time now since I last wrote on the blog, but someone thought I should start again, so…

So, I will…

Let’s kick off with a bit of a useful stuff for PHP, that i had been looking for some time myself.

I’ll start writing about all and nothing again, from time to time, but i will try to keep it a bit more frequent this time.

 





Recession and ripoff.

30 04 2012

In a recession, I suppose that as people and business does get more and more desperate to make that extra buck, that will make the difference between a positive bottom line, or a break even or even a loss.

However, when you stoop as low as deliberately charging the wrong prices, and when called up on it, look at you as If you were some idiot with the audacity to ask them to correct it, and when pushed, fix half the problem, leaving you even more fuming, while demanding they fix the rest.
Sometimes, it’s not the money, but more the principle of the whole thing, and especially so after leaving a good tip.

I went in to this restaurant called “Made in Belfast” with my wife, had a lunch, for which they have a lunch menu with a lower price.
After we left, we realized that we had been overcharged, paying the dinner menu prices.
When challenging them, they only paid back half the difference and tried to keep the receipt, which I had to demand back, and I had to repeat the procedure.
After talking to others, others seems to have experienced this as well, but never bothered to challenge them on it.

Another day of making this extra buck, is to skimp on what you deliver.
We later went to Cafe Vaudeville in Belfast, and had a dinner.
My wife had a chicken with potato gratin, and I had a sirloin steak.
While there were nothing majorly wrong with the food itself, it was pretty bland and non-tasting.
The portion control had taken its severe toll, leaving little on the plate.
The £20 steak, was a relatively sad little skimpy bit of sirlion in the middle of the plank, with a tiny bowl of sauce, a small bowl containing a few decorative salad leafs, and 8 “triple fried” (whatever the the point is with that) fries in a small cup.
The chicken wasn’t any better, with a tiny amount of gratin on one edge of the plate, and some chicken on the other end, and some green cooked up goop in the middle.

Taken these experiences, i can only assume that it is now par for course to alienate your customers, taking as much as you can in one go, only to lose them as returning customers (and getting bad press about it, as a result…).

Made in Belfast and Cafe Vaudeville, I hope it was worth it, losing us as returning customers.
Made in Belfast – what’s worth more, the £4 you tried to scam off, and making noises about it when calling you up on it?
Cafe Vaudeville – Not serving value for money, is a costly mistake these days. Hope you too find it worthwile, and we were not the only ones unhappy with your offerings. for a £20 meal, I really don’t expect to leave hungry, that I have to go find something else as well.

I could have complained on location, yes, but that wouldnot have helped any of you, as after we’ve gone, it would have been business as usual, and none of you would have been none the wiser to the methods.

 

As to the proprietors?
Before you start screaming blue murder and telling me that I am telling porkies, and can not be…
This is something you should have thought about, before you tried it.
These are all genuine, first-hand experiences, on the 28th of April, 2012.





Who likes fireworks?

30 10 2011

Here’s a short snippet from the Belfast fireworks on the 30th of October 2011.

Enjoy!
http://www.youtube.com/watch?v=Z8EL_kRJcsI





Mozilla’s new rapid-release…

2 10 2011

Just having to vent some steam…

I never thought i’d say this, but this new rapid release scheme, is beginning to get to me, with what now feels like an update every other day, for what is in most cases, absolute trivial and minor updates, that would be far better off being gathered up in a bigger release.

In fact, it is getting so annoying now, that even me, one of the most avid FF fans, are considering alternatives.

If any of the guys at Mozilla came across this, which is extremely unlikely, and not that i think they would care anyway…

A release cycle of a year, is too long. what now feels like a few days inbetween, is just a major fail, and a quarterly update,  would be about right.

A lot of business users are moving away from Mozilla, and the reason for this, is one single reason – the rapid release scheme, as nobody has the time or stamina to keep up with mozilla, screening and deploying a new update all the time.

Even as a private user, very politely expressed, getting extremely annoyed with it, and trust me, this didn’t come easy.

Mozilla, change your ways, or you will have lost yet another user…
Do you really think that the recent drop in popularity and the introduction of your new rapid release scheme is a pure coincidence?

Think again.





A little private message =)

8 07 2011

Ruairi, Thank’s for reading the blog, and I wish you very good luck with your new venture!





Samsung Galaxy tab – boot animations.

9 06 2011

Custom boot animation on Samsung Galaxy tab?

When I got my Galaxy tab, among the first thing I did, was to root it, simply because I wanted to “root around” and see what was there.
Later on, I went one step further, and installed the Gingerbread (2.3.3) on it, and i had been looking at having a custom boot animation on the thing, as i wanted to “brand it” my own way.
I found the Samsung 2.3.3 release from Samfirmware.com and installed it on the tab using Odin.

Much to my dismay, I had found that Samsung uses a custom format – .QMG, which literally nobody knows what it is, apart from Samsung and their suppliers.

However, shame on giving up.
After looking for solutions on the net, and ending up with zilch, I decided to have a closer look at the init.rc script that brings up the system, and it did give me a few ideas.
I started off with testing the commands manually, after connecting to the tab with a SSH client (install sshdroid – you want to do this, as you need to send CTRL C to break out of the boot animation while testing), and I found a simple solution to get it working.

Yes, you CAN have a bog standard boot animation on your tab!

[EDIT] Now, this MIGHT work on the 2.2 version as well, but i have not tried.  [/EDIT]

First – Your device MUST be rooted.

Secondly – the disclaimer –
You do all or any of the things below, at your own risk. If you mess it up, don’t blame me, and don’t say I didn’t warn you.
If you mess anything up, or something else decides to break, the brick is yours to keep – you did it – not me =)

Seriously, worst case scenario would be having to do a few adb lines or to re-flash a few files on the tab, and it will be working again.

So, let’s get started and get custom boot animations working on the thing!

First off, we need to get root acess, so just do, once connected to the ssh server;

su

and allow the shell superuser access.

Following this, we want to make the system partition writeable.

mount -o remount rw /system

Then, we want to place ourselves in the correct folder.

cd /system/media

Now, to get rid of the Samsung boot branding,  we need to rename a few files, you might want to keep them on the tab, just in case you want them back..

mv bootani.qmg bootani.qmg_X
mv bootstill.qmg bootstill.qmg_X
mv samsungani.qmg samsungani.qmg_X

Next, we need to drag in the boot animation you want, and have placed on the SD card (internal memory).

cp /sdcard/bootanimation.zip bootanimation.zip

Now, if you want, you can test it by typing “bootanimation“.

To get out of it, as it will loop indefinitely, you have to press CTRL C.
If you want to change the boot animation, just copy in another one, and test it as above.

Once you are happy, do the following.

mount -o remount ro /system

Now, it is time to view it – restart your tab, and the Samsung boot animation should be gone, and your new shiny standard format boot animation be in place instead!

How to make your own boot animation file??

Simples!

1. Create PNG images for your animation.
Make sure they are the same size as your devices screen.

2. Rename those PNG images starting with a 5 digit number for the first frame and ascending for each consecutive frame.
It is preferred that the pictures are saved as 8-bit PNG.
Create a folder named “part0″ and an optional folder named “part1″.
Place your animation images in the “part0″ folder.

The images in part1 folder should start where the last image in part0 folder has ended.

For example the original boot animation in your droid phone has images from 480_427_00000.PNG to 480_427_00075.PNG in part0 folder and 480_427_00076.PNG to 480_427_00091.PNG in part1 folder.

The images in part0 folder is played once, and the images in part1 is played in a looped manner.

3. Create a custom “desc.txt” and add the following in it.

480 427 30p 1 0 part0p 0 0 part1

Here’s what the parameters means:

480 Stretched width of the animation.
427 Stretched height of the animation.
30p This controls the speed of the animation (both parts).
1        Defines how many times the part0 is looped.
0        Not used.
part0p – folder containing the first animation.
0        Number of times the second folder is looped. (0 = infinite)
0        Not used.
part1 – folder containing the second animation.

4. Place part0, part1 and desc.txt in a folder renamed as ‘bootanimation’.
Zip the folder (using an archiving utility such as 7Zip).
Set the ‘Compression level’ setting to ‘store’ (which means no compression), before you archive.
Compression will make it unreadable for your android.

5. Copy bootanimation.zip file from your PC to your SDCard root.

6.  Mount file system read/write

mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system

7. Now copy the bootanimation.zip file from your SDcard to your phone’s memory.

cp /sdcard/bootanimation.zip /system/media/bootanimation.zip

Enjoy your custom boot look!








Follow

Get every new post delivered to your Inbox.