NSIS: StrContains function – Find the index of a sub string.

Recently I was out of internet and I needed to implement a function that would search if a String was part of another String for an NSIS installer.
Here’s my rendition of such a function. Once I came back online I found out about StrStr, but here’s another option that gives you as output the starting index on the bigger string (aka Haystack) if the smaller string (aka Needle) is found. Otherwise, it returns -1.

[code]
; ————————————————
; StrContains
; Returns starting index of where a string is contained inside another string (case sensitive)
; Usage: !insertmacro StrContains ${HayStack} ${Needle} ${Output}
;
; Parameters:
; HayStack – The string that could contain the substring you’re looking for
; Needle – The string you are looking for
; Output – -1 if the needle is not in the haystack, or the offset where it exists.

Var HaystackOffset ;current offset on haystack to copy NeedleLength characters into HayStackBuffer
Var HaystackLength ;size of the big string
Var MaxHaystackOffset ;maximum offset we can reach
Var NeedleLength ;length of string we’re looking for
Var HaystackBuffer ;substring we get on each iteration

!macro StrContains Haystack Needle Output
;we’ll be repeating this operation until we’ve reached
;HayStackLength – NeedleLength

;Get the lengths of the strings.
StrLen $HaystackLength ${Haystack}
StrLen $NeedleLength ${Needle}
StrCpy $HaystackOffset 0

;Determine what’s the maximum offset we can use to search for the substring
IntOp $MaxHaystackOffset $HaystackLength – $NeedleLength
IntOp $MaxHaystackOffset $MaxHaystackOffset – 1

;Make sure needle is not bigger than haystack
IntCmp $NeedleLength $HaystackLength LoopStart LoopStart DidNotFindIt

DidNotFindIt:
StrCpy $HaystackOffset -1
Goto Finish

;Start of substring comparison loop
LoopStart:
;copy the substring to a buffer
StrCpy $HaystackBuffer ${Haystack} $NeedleLength $HaystackOffset

;It’s pretty cool if you uncomment this.
;MessageBox MB_ICONINFORMATION|MB_OK "[Loop] Buffer: [$HaystackBuffer] Offset: [$HaystackOffset]"

;If we’re done, we return the current haystackoffset
StrCmpS $HaystackBuffer ${Needle} Finish FigureOutNextStep

;Did we reach the end, or can we move a bit more
FigureOutNextStep:

;Move offset 1 character to the right and see if we can keep going
IntOp $HaystackOffset $HaystackOffset + 1
IntCmp $HaystackOffset $MaxHaystackOffset LoopStart LoopStart DidNotFindIt

Finish:
StrCpy ${Output} $HaystackOffset
!macroend
[/code]

How to resize a VirtualBox fixed size virtual drive (.vdi) on Mac

So you created a disk for your Windows or Linux VirtualBox VM and you made the mistake of not creating the drive as a dynamically expanding storage drive, you chose fixed size.

Now you’re running out of space and all your Google searches point you to stupid posts on the virtualbox.org forums that lead to nowhere, specially if you are a MacOSX user, look no further, you’ve found the solution to your problem on this post.

VirtualBox for Mac comes with a tool called VBoxManage, this tool is all you need to resize your virtual disk.

When you’re using the VBoxManage tool make sure your VM is not running, stop it completely to be safe.

Step 0. Backup your current drive (optional)

I recommend that before you do anything you clone your existing drive, just in case…

You can clone your drive using guess what? yes, the VBoxManage tool. No need to download any external tools.

[bash]$ VboxManage clonehd <path to your original drive> <path of the copy>[/bash]

in my computer it looked exactly like this:

[bash]$ VBoxManage clonehd /Users/gubatron/VirtualBox VMs/windows7-64bit/windows7-64bit.vdi /Users/gubatron/windows7-64bit.cloned.vdi
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%[/bash]

If you don’t know where your .vdi file is, it’s very simple, right Click your VM > Settings > Storage, and mouse over the .vdi to see it’s physical location on your Mac hardrive.


Mouse over your virtual drive if you don’t know its location

Step 1. Resize!

The command to resize is:
[bash]$ VBoxManage modifyhd <path to your vdi> –resize <new size in megabytes>[/bash]

In my case I had a 20GB drive that I wanted to double in size (40GB), a quick Google search for “40GB to megabytes” yields the number 40960, so that’s what I put on my –resize parameter.

This is how it looked for me:
[bash]$ VBoxManage modifyhd /Users/gubatron/VirtualBox VMs/windows7-64bit/windows7-64bit.vdi –resize 40960
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%[/bash]

The ‘resizing’ is almost immediate.

Step 2. (Re)Start VirtualBox

After you’ve physically resized your drive, both VirtualBox and your OS should know about the changes.

At this point, clicking on your VM in the VirtualBox window will show that it still has the old size:

Before: (Note the size of the drive at the bottom of the image)

After doing the resize, shutdown completely your VirtualBox, and launch it again. When you see the information about the storage drives attached to your VM you should see the new disk size.

After: (Note the size of the drive at the bottom of the image)

Step 3. Let your OS know the new logical size of the drive


Your OS is still not aware of the changes, you need to let it know about them

When you start your VM, it won’t know right away what the new size of the disk is, you’ll have let the operating system know that there is additional space it can use.

If your VM is running Windows 7, the way to let the operating system know that it can use the additional space on your drive is very straight forward.

Click on the Windows (Start) Icon on the Bar > Right Click on “Computer” > Manage (You’ll need to be an Administrator)

The “Computer Management” window will open, in it go to:

Storage > Disk Management > Right Click on the partition you want to extend.

Select the option that says “Extend”, a Wizard will open, just hit Next until it ends if you want to use the whole space.

After you’re done, if you refresh your “Computer” on the File explorer the resized drive should show right away

That’s it, enjoy and let me know how it went.

DON’T UPGRADE YOUR VERIZON 4G THUNDERBOLD FIRMWARE, JUST ROOT IT

So I had to work outside today, and as usual I wanted to use my Verizon 4G Android (Thunderbolt) to connect my laptop using the Wi-Fi hotspot feature, as I’ve been doing since I got the phone, it’s been a real life saver many times.

This morning I saw that there was a firmware update, and I gladly accepted it, after that upgrade, now dear Verizon has decided to charge 4G users with $30 A MONTH to use the Hotspot feature that was free on my phone.

Really Verizon…?

So well, the outcome is that, NO, I’m never gonna pay you $30 a month to use my own Wi-Fi card to share my internet connection, but instead I’ve gone and signed up for Clear’s 4G hotspot, which allows me to connect up to 8 computers.

Verizon, I was happy with you until now, you suck balls, Android users are not stupid, this is pure evil. I guess my $250 a month on my line and my family’s lines are not enough for you.

So, if Clear ends up working well for me, what I think I’ll do at the end of the contract will be radical and I hope more and more people will do the same, I’m gonna get rid of my phone line, bye bye stupid 10 digit numbers, bye bye dynamic monthly bills, bye bye Verizon. I’ll just have it connected to my Wi-Fi hotspot, I’ll have faster internet access, and if I need to make a phone call, I’ll use either GTalk or Skype, fuck it. I don’t answer the phone anyway, I’ll send everyone straight to Google Voice.

twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.

Save the following on a .js file, preferably named jquery.twitterAutoLinks.js
[javascript]
/** Auto replace all Twitter nicknames for links */
$.fn.twitterAutoLinks = function() {
return this.each(function() {
var html = $(this).html();
$(this).html(html.replace(/B@([w-]+)/gm, ‘<a href="http://twitter.com/$1" target="_blank">@$1</a>’));
});
};
[/javascript]

Usage

After importing the jquery.twitterAutoLinks.js file (make sure it’s after you’ve imported jquery itself), you’ll use the extension as follows.

[html]
<script type="text/javascript" language="Javascript">

$(document).ready(function() {
//say you’re running a wordpress blog and you want to do this for your posts
$("div.entry").twitterAutoLinks();
});

</script>
[/html]