Removing £ signs from a file using sed

December 2nd, 2008

Hi

If you’ve ever tried to use ’sed’ to remove the UK pound sign (£) from a file you may have struggled.  We were bashing our head against the wall until we tried this little trick :

 sed -e 's/'`echo -e "\xA3"`'//g' input.txt > output.txt

Note the important difference between the single-quote and the ‘back-tick’ around the echo.

We had been trying to use the pound sign directly in the terminal window but it just wasn’t matching for some reason.  We then used a Hex Editor  to find the raw code that was being used to represent the pound sign in the file and used that instead and voila!, it works.

 

Building an OCCI project using XCode 3.1

October 2nd, 2008

With the release of the Instant Client for Mac OS X you can now build OCCI based applications targetting modern Macs using Leopard on Intel.  However, there is very little help available for building these apps using XCode, and if you hit the same problem we did (see below) then perhaps this post may be helpful.

Firstly download the Instant Client.  You will need the ‘Basic’ and ‘SDK’ packages.  We have unzipped them into ‘/instantclient_10_2′ on the root of the hard disk.  The ‘SDK’ should unzip into a folder within this one. The OCCI headers (and others) should then be in this path : ‘/instantclient_10_2/sdk/include’.  You can of course change this to suit your needs but this is what the walkthrough below is based on.

Before getting ’stuck in’ though you will need to create a couple if ‘links’ so that GCC can link to the OCCI libraries.  You can do this using a ‘Run Script’ in the project but I find it easier just to do this as a ‘one off’ now.  

  $ ln /instantclient_10_2/libocci.dylib.10.1 /instantclient_10_2/libocci.dylib
  $ ln /instantclient_10_2/libclntsh.dylib.10.1 /instantclient_10_2/libclntsh.dylib

So let's get started!

Firstly, create a new 'C++ Tool' project using XCode :

and give it a name, in this case, ‘occi’ :

You will then have your project open in XCode :

Next, double click on the item under ‘Targets’, in this case ‘occi’ to bring up the Target Info window.  Switch to the ‘Build’ tab :

Change the following settings (note the ‘Search’ box in the top right to help you find the settings ):

Other Linker Flags : -locci -lclntsh

Header Search Paths : /instantclient_10_2/sdk/include

Library Search Paths : /instantclient_10_2

Next, open your ‘main.cpp’ file and try this sample code :

#include <iostream>

/* Oracle */
#include <occi.h>

/* namespaces */
using namespace std;
using namespace oracle::occi;

/* Globals */
Environment * env;
Connection * conn;
 
int main(int argc, char * argv)
{
  // Connect
  env = Environment::createEnvironment(Environment::OBJECT);
  conn = env->createConnection("scott", "tiger", "//lcoalhost:1521/xe");
  Statement *stmt = conn->createStatement("SELECT COUNT(*) FROM TAB");
  ResultSet *rs=stmt->executeQuery();
  rs->next();
  string ntabs=rs->getString(1);
  cout << "Number of tables " << ntabs << endl;  
  conn->terminateStatement(stmt);
  // Close connection etc
  env->terminateConnection(conn);
  Environment::terminateEnvironment(env);  
  return 0;
}

Don’t forget to change the connection details to the relevant values for your setup.

Next, navigate to the ‘Executables’ folder of your project and double click on ‘occi’ to bring up the Executable Info page.  Change to the ‘Arguments’ tab and add an environment variable for DYLD_LIBRARY_PATH :

Set the value to ‘/instantclient_10_2′.

You should then be able to ‘Build and Go’.   You may want to set a breakpoint or open the Console (’Run -> Console’ from the XCode menu) so you can see the results :

And that should be it!   However…..

If you do something a little more advanced, which in our case meant using ‘ott’ (supplied as part of the ‘SDK’ package for the Instant Client) and building OCCI code based on the Oracle Spatial type (SDO_GEOMETRY) then you will probably get this error at link time :

Undefined symbols:  "oracle::occi::setVector(oracle::occi::AnyData&, 
                    __gnu_debug_def::vector<oracle::occi::Number, 
                    std::allocator<oracle::occi::Number> > const&)", referenced from:
      _main in occi.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

After much digging the problem is with a couple of Preprocessor macros defined by default in XCode, _GLIBCXX_DEBUG=1 and _GLIBCXX_DEBUG_PEDANTIC=1.

Go into the ‘Build Settings’ for the target as described above, find the Preprocessor Macros entry and remove the two macro definitions.  Note that setting them to 0 does not seem to work.

You should then be good to go!  

Things we like about the Mac

September 17th, 2008

As you may have gleaned from other posts we do a lot of development using Macs, specifically MacBooks and iMacs.  We might develop products that run on Linux, Windows or Solaris but that doesn’t stop us using our Macs to actually do the work!  

So why Macs?  Well, we had a laptop in a while back with Vista on it and it was universally loathed.  Everything we tried to do on it, being ‘power users’, was either blocked, slow or just downright cumbersome.  Frequent lock ups and crashes just drove us mad.  As for waking from sleep, it was more like waking the dead!  We’d been tempted to ‘switch‘ for a while and having this laptop just made the decision for us.

Reasons we like the Mac include :

The Operating System, Mac OS X, ‘just works’ and doesn’t get in the way and slow things down.  An O/S, more than any other aspect of the computer, is a tool and we believe that you should hardly know that it is there as it seamlessly let’s you go about your job.  

Leopard

Useful features in the ‘Finder‘ (the Mac OS X equivalent to Windows Explorer) such as ‘Quick Look‘ and ‘Cover Flow’ really help us find the file we’re looking for, particularly office-type documents, without needing to open up the app itself.  From a personal perspective, I love the way I can create commonly used shortcuts in the sidebar that appear everywhere including Open and Save dialogs.  This is a real time saver when working on specific projects and needing frequent access to the folder.

Being developers of course the fact that Mac OS is built on Unix just rocks.  Has Windows got ‘head’ and ‘tail’ yet?!  That’s just laughable that if I have 2 gb text file I want to look at the last line of I need to open the whole thing in Windows.  Plus of course the full complement of tools including ‘vi’, ’sed’, ‘tr’ etc just make us so productive.

Time Machine is also a big boon.  We all know we should do more backups, in fact we frequently preach to friends and family about it, yet are probably the worst culprits!  Time Machine makes it so painless to not only backup, but restore as well.  All built in, and with plenty of USB drives out there for £50 a dirt cheap solution too.

Time Machine

Of course we’d be naive to think that we can leave Windows behind entirely, if only for testing and supporting clients.  Support for the ‘Windows World’ is good and the following add-ons make it a pleasure :

We do a lot of communicating using Skype.  Of course Skype exists for Mac, and I can even use my SkypePhone too!  (click here for the VoIP Driver I used).

We all spend quite a bit of time using ‘office’ products to create documents and spreadsheets. You can use Microsoft Office and get 100% (or near enough!) compatibility with your Windows colleagues but there are other choices.  iWork ‘08 is great (we particularly like Numbers) and it’s very affordable too.  There’s also NeoOffice, a Mac OS port of Open Office that also works well.  We use iWork and NeoOffice and haven’t found anything we can’t open yet.

We do a lot of work with Oracle and the Mac has pretty good support from Oracle too.  SQL Developer is a free download and works as well as it’s Windows cousin.  There’s also the Instant Client so we now have full access to ’sqlplus’ too.  Maybe we’ll even get a version of the database one day as well. ;-)

SQL Developer

Professional development tools such as Zend Studio meant we flawlessly transferred our PHP development effort across with zero downtime, and supporting Open Source products such as uDig and pgAdmin have meant our GIS and PostgreSQL work was uninterrupted also.

Also, from a physical point of view the MacBook is great.  It’s small, light and very well built.  The track-pad works really well and battery life is fantastic.  Doing standard tasks such as browsing, ssh, SQL access etc and it will last for about 5 hours.  The magnetic lid is a surprisingly pleasant change from fiddly catches, and plugging in an external monitor works flawlessly and gives you a a wonderful wide desktop; great for development!

Plus there’s some of the other ‘fun’ stuff such as Google Earth and Real Player too, so it really is a home from home.

And finally, of course, are the little touches that just make a Mac, well, a Mac!  Such things as right-clicking on a piece of text ad choosing ‘Search in Google’, the way Mail automatically picks out addresses and let’s me show them in Google Maps and all the great things in Mac OS such as iPhoto, iTunes and others.

So, if you’re thinking about the ’switch’, then don’t be afraid, go for it!  You’ll be glad you did.

 

 

 

 

 

Mac OS X TextEdit doesn’t display NULLs!

September 16th, 2008

We just had a real hair-puller of a problem so thought we’d blog about it here just in case someone else sees something similar.  We had been sent a text file (.sql) of SQL Server ‘CREATE TABLE’ statements that we were manually converting to Oracle syntax just using TextEdit (OK, I know there are better editors but it was a small job and TextEdit was fine).

Our first problems occurred trying to edit the file when it appeared like the Mac’s keyboard was broken.  We kept having to press the delete and cursor keys repeatedly to move around make the edits.  This seemed weird but we did the normal stuff like rebooting, updating to the latest OS X, swapping USB ports, checking the console etc but nothing looked amiss so kind of ‘lived with it’ as it wasn’t a big deal.

Then we decided to run the script via SQLDeveloper and immediately noticed that our file wasn’t syntax highlighted.  Weird?!   Trying various other editors (BBEdit, TextMate, TextPad on Windows) all showed the same problem – no syntax highlighting.  Then we noticed in TextMate that there were what appeared be spaces between every character.  Further examination using a hex editor showed that there was a NULL character (\0) in between every single character of the file!!

No wonder we had to press delete twice to do anything in TextEdit, and no wonder our syntax wasn’t highlighted!

The next problem was how to remove the NULLs which is not as easy as it sounds.  However a variation on the blog entry below using ‘tr’ saved the day and now it is all much as it should be :

$ tr -d '\0' < tables.sql > tables2.sql

Hope this helps someone! 

UPDATE : we have now switched to TextMate for all text editing.  We love the fact we can run Shell scripts and SQL scripts directly from the environment using ‘bundles’.  For those switching from TextPad on Windows, TextMate is a pretty good choice.

Converting tab delimited data to CSV data

July 29th, 2008

Here’s a little trick we discovered for converting tab delimited text to comma separated (CSV).  You’ll need access to a Unix, Linux or Mac OS X machine as it relies on the ‘tr’ command line not present on Windows. 

To convert the file simply :

$ tr '\t' ',' < file.tab > file.csv

where ‘file.tab’ is your tab-delimited file and ‘file.csv’ is the file to write to.

And you’re done!

 

ApEx Select list with Redirect confirm change

July 17th, 2008

We recently had a situation that required a little bit of head scratching.  We had a page with a Select List with Redirect that was used to populate some form fields based on the contents of the Select List.  The users could change the values in the form and save them, but if they changed the Select List before using the ‘Save’ button then the changes would be lost.  What we wanted to do was add a confirmation message to make sure the user saved the details before changing the Select List.

Unfortunately you can’t put an ‘onchange’ handler in the HTML Form Element Attributes as ApEx is using onchange to do the redirect. Any code we put in is simply ignored.

Instead, we need to mimic what ApEx does, and then put in our code.  Firstly define some JavaScript in the page header :

<script type="Text/JavaScript">
var startVal = &P68_PERSON_ID.;
function checkSave(p) {
    if (confirm('Have you saved details for this person?')) {
       location.href='f?p=&APP_ID.:68:&SESSION.::NO::P68_PERSON_ID:'+
                  p.options[p.selectedIndex].value;
    } else {
       for (i=0;i<p.options.length;i++) {
          if (p.options[i].value == startVal) {
             p.options[i].selected = true;
          } else {
             p.options[i].selected = false;
          }
       }
    }
}
</script> 

This code is from page ‘68′ in our application, and the Select List is called P68_PERSON_ID.   You’ll need to change these to something appropriate for your app. 

The JavaScript saves the initial value set when the page loads here :

var startVal = &P68_PERSON_ID.;

If the user confirms they have saved the details, then the window’s location is changed and the selected item used as a basis for the new page.  If the user cancels we need to reset the selected item back, as by now the browser has changed it for us (thanks!).

Next, change the Select List with Redirect to just a normal Select List, then, add an onchange handler to the the Form Element Attributes to call the JavaScript :

Now, when the user changes the value in the list box they get prompted to check they have saved the details, which prevents them changing the page data inadvertently.

Using Ajax to populate one field based on another in an ApEx form

June 20th, 2008

Often it can be useful to populate one field on a form based on the content of another in a live and dynamic fashion. Luckily this is easily achieved using the Javascript API supplied as part of ApEx 3.1.

This post illustrates an example where we wanted to lookup a value (called IMD Rank) based on the Postcode entered in another field. Users can either enter it manually, or click on a ‘Lookup’ button next to the field.

Firstly, define your process that will calculate the required value. This is most easily achieved by creating an Application Process (under Shared Components) which will run ‘on demand’.

This PL/SQL is just retrieving a value (RANKOFIMD) from a table based on a page item (P27_P9_POSTCODE). Note the ‘htp.prn()’ function call which will actually ’send’ the result to our page.

Next, define the JavaScript required to execute the process and return a value.  We’ll be displaying the returned value in the page item called P27_P16_IMDRANK. Add this script to the HTML Header section of your page.

Note : it is OK to append this script to any code which may already be in this field.

Next, add a button to invoke the JavaScript. I just added a standard HTML button after the element :

Clicking on the Lookup button now populates the field based on the value retrieved by the GetIMD application process.

Editting ApEx theme images using Mac OS X and WebDAV on XE

June 6th, 2008

Recently we had to make a couple of minor tweaks to a couple of ApEx theme images stored in an XE installation.  At first we scratched heads a little but then found a helpful post on the ApEx forum which made it much easier.  All we needed to do was work out how to do this using Mac OS X Leopard.  Luckily, it’s also very easy!

Firstly open a Finder winder then choose Go -> Connect to Server from the Finder Menu :

Connect to Server

Enter the server name and port that the embedded HTTP gateway is listening on (8080 by default) and click on Connect.  You will then need a database user name that has permission to modify the ApEx schema (FLOWS_*).  A DBA user name or the FLOWS_* user name and password should work fine :

WebDAV File System Authentication

With this done, click OK and logon to the database. A new Finder window will then appear showing the ‘root’ of the database WebDAV file system :

You can now navigate through the tree to find the image in question by following through i -> themes -> theme X :

WebDAV theme path

I haven’t yet worked out how to edit the images ‘in place’ but it’s easy enough to copy the required images out to a folder on your hard disk, edit them, and then copy them back into the appropriate folder.

Change the background colour of an ApEx page

June 4th, 2008

The simplest way to change the background colour of an ApEx page is to edit the <body> template using the Application Builder.

Login to ApEx and navigate to your application.  Click on ‘Shared Components’, and then ‘Templates’ to view the templates for the current application theme.

ApEx templates

Navigate down to find the appropriate page template that you are using (tip: the ‘References’ column holds a count of the number of uses for a template, so if you’re not sure look for something greater than zero).  When you’ve found your template, click on it to edit the HTML elements of the template :

Edit template HTML header

Here you can see we’re using the ’style’ tag to set the background colour (using background-color) to the hex colour #46C6BA.  Standard HTML colour names such as ‘white’, ‘blue’ etc can also be used here.

New ApEx book published

June 3rd, 2008

We just spotted this new ApEx book on Amazon entitled “Pro Oracle Application Express” :

It’s not released yet but looks good, and is written by two giants of the ApEx scene, John Scott and Scott Spendolini, so we’ve placed our order already!