Technical

C++ Quickies #I : Long live the STL

0

Its been quite some time (16+ months) I’ve been out of college and started using STL. Trust me, I didn’t know much about STL in college, though I used std::string & std::vector everywhere I could. Now, it seems like I couldn’t (wouldn’t ? ) write a program without using STL at all. A few snippets that I really found useful (and simple) and I regularly use where ever possible.

To output the elements (all / subset) in a container (lets say vector), separated by a delimiter, say new line:

#include <algorithm>

std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, "\n"));

Want to sort an array? No problem … :)


#include <algorithm>

std::sort(array, array + lengthOfArray);

Read from the standard input into a vector until the end-of-stream?


#include <algorithm>
#include <iterator>
#include <vector>

typedef std::istream_iterator<int> istream_iterator_int;
std::vector<int> v;
istream_iterator_int start (std::cin);
istream_iterator_int end;
std::back_insert_iterator<vector<int> > dest (v);
std::copy (start, end, dest);

Count the no. of instances an object is found?


#include <algorithm>

size_t count = std::count(v.begin(), v.end(), 42); // Returns no. of elements in v with value 42.

And let me inform you… This is just a grain of what STL can do…. :)

References:

http://www.cs.brown.edu/~jak/proglang/cpp/stltut/tut.html

http://www.cplusplus.com/reference/algorithm/

Learning Android: Part I

1

I will use this post to talk about my ‘Getting familiar with Android’ learning. As announced in the last post, I’ve started learning Android, but not even at half-pace as I wanted to.

Anyways, let me tell what I’m doing in learning. I’m using ADT on Eclipse Helios and using the Emulator for testing purposes. Currently focusing on the samples present along with Android SDK. Gone through the Application Fundamentals available along with the SDK.

Interesting Things:

  • Most importantly, its good to see myself learning the things I’m interested in from the start (FYI, I always have that thing for Web & Mobile Development)
  • Very clear & highly helpful documentation (and, also downloadable). And the docs are developer-focused & also lays emphasis on good practices at every step, like i18n, using resources instead of hard-coded strings etc. etc..
  • Separation of layout of the app and the application logic (supports XML files for layout). I get the feeling this is inspired from Flex (mxml & as), but I don’t know.
  • Provides a very good mechanisms for interoperability between various applications. Simply said, I can use the capabilities of other applications installed, and also I can expose my app’s capabilities to other apps. Also, Android search also integrates good with any application’s search.
  • Application Development is done in Java, which is said to be a very developer-friendly. I can utilize this to brush my skills in java, as I have other plans to work in Java in some not-so-far future.

Annoying Things:

  • Tabs are NOT simple to use. I’d prefer a way to declare tabs and its contents in the XML itself.
  • In TabHost, mandatory referencing of elements to’ tabs’ & ‘tabcontent’ doesn’t look like a good programming feature.
  • Missing the tabs & tabcontent id referencing in the TabHost would give no error / warning by Compiler, but fails at Runtime, without any proper information (even while debugging)
  • Emulator takes very long time to start. So, its preferred not to close the emulator after every run. (start along with eclipse and close it before closing eclipse)
  • Emulator keyboard shortcuts – Not too easy to remember (like, Ctrl + F12 for Toggle Portrait / Landscape modes). I’d have preferred if the emulator itself has a help button, to show the shortcuts.
  • Nothing to do with Android, but the bug in Helios (Eclipse Web tools plug-in) has become a pain, forcing me not to us any features while editing resources. Bug – https://bugs.eclipse.org/bugs/show-bug.cgi?id=318108
  • Again, may not be related to ADT (frankly, I don’t know), but the auto-complete context is very slow, taking 30-60 seconds (which is tooooo long, btw). Here again, I’m forced to disable Auto-Activation of context Assist at ‘Window -> Preferences -> Java -> Editor -> Context Assist -> Enable Auto Activation’
  • I  can’t run the application when my focus is on layout files (on resource files, for that matter). I always need to switch to a java file, to run (Ctrl + F11) the application
  • The layout filenames cannot contain uppercase letters. If it contains, the error shown doesn’t help in identifying it

Unclear points for me:

  • In Android XML, for TabHost, @android:id/tabhost works ( i.e; referencing other element with tabhost id ). How can access this element from my Activity class? (since, no reference is created in R.id )
  • Is XML compiled ? Or interpreted at Runtime? If compiled, how can I see the generated Java (?) files? (something similar to -keep compiler option in Flash Builder)
  • And many other things (like, working of Intent, i18n, logging), but these are somethings I need to learn as I dive further in android..

Tasks for coming days:

  • Finish the Tutorials in the resources, and then go to the samples folder available with the SDK.
  • Try to make a couple of simple *usable* apps, so that I get enough practice with the SDK APIs

Also, attaching the projects HelloAndroid.zip and HelloViews.zip I made during my learning.

Wish me that I gain pace in learning Android over the next few weeks…

I had a problem

0

Symptoms:

  • Language: C++, Operating System: Windows
  • Target Audience: Programmers
  • Sub Area: Reading ASCII / UTF-8 files from the disk using C++ streams.
  • Trouble: How to handle files whose file names contains non-ASCII characters?? (coz ifstream takes a ASCII file path :-( )

Solution:

  • Use _wfopen_s(), the safe version of _wfopen(), which takes a wide-character file name & gives a FILE pointer i.e; FILE*.
  • Use the obtained FILE* to create a stream, like ifstream in(fp);
  • We used wide-character file name & are reading the file byte-by-byte (actually, its char). DONE !! :-)

BTW, I got help from here ! Hope this saves  time for someone out there…

Doing things the HTML way

0

Hi,

Some points that come into handy while developing in HTML:

  • Always use XHTML tags. meaning that, your HTML should be well-formed XML (closing  & ending tags should be present). Like, for example, use <br/> instead of <br>
  • Try to use XHTML 1.0 strict as much as possible. But, this mode doesn’t support target attribute on anchor (<a>) links. If you need to specify ‘target’ on links, they you should be using XHTML 1.0 transitional doctype.
  • Always specify the doctype of your HTML page. Also just run the validator test available at w3c.org to know where are deviating from the standards specified. And fix as much as possible to make it closer to standards.
  • IE doesn’t support *.ico [Icons] files as Images in tags. So, make sure that your images are NOT ico types.
  • Are you developing site in non-English language? Then, knowing about Dynamic Fonts is a must before implementation. Try to find more about it from Google ( I have no experience with them to tell you :-( )
  • A common bad practice among beginners: Uploading a large image to the server (like, 1024 * 768) & then set required image & height(200 * 150)  in HTML. This would cause unnecessary bandwidth waste which can be easily avoided by uploading the resized image of the desired dimensions.
  • You should learn about ‘Alternate Style-sheets’ if you wanna provide multiple themes to the webuser. Basic usage of this can be found at: http://javascript.about.com/library/blswitch.htm
  • One important, yet highly ignored practise, is providing a Print specific stylesheet for the page. This is possible by specifying media=”print” on the stylesheet link, like <link type=”text/css” rel=”stylesheet” href=”/css/print.css” media=”print” />. And, let all the unneccesary data be hidden in this css file (like, ads, navigational menu, header, search bar etc.). Keep the print to minimum.
  • It would also look good if we provide the end-user a ‘Print Preview’ from inside our page itself. This isn’t any magic, its similar to multi theming concept as said above.

Yes, I understand these are very basics, but don’t we know that Basics are the building blocks of any application?

Update: The Print Preview concept is borrowed from
http://www.alistapart.com/d/printtopreview/example.html.
Click on ‘Print this Page’ on the LHS of the page & see that page layout changes, giving the user the Print Preview Experience

GTalk: Display Images Cache

0

In my last article, I talked about the way of obtaining a user’s display image in Yahoo! Messenger & Google Talk, over the internet through http protocol (the browser way). In this case, it is the browser’s decision about the cache to be used for those images. But, the IM client Applications(googletalk.exe, YahooMessenger.exe) are stand-alone applications. And we do not expect them to fetch the display image on the fly over the wire from the internet, whenever the image is to be displayed. The most simple, obvious but yet powerful solution is  to maintain the cache of images on the disk.

Now, my point of this article is, what is this cache location of images, so that I can easily navigate over to there and pick a friend’s avatar from there..  :-) Well on Windows Systems, the display images cache directory for GTalk is:

C:\Documents and Settings\<windows username>\Local Settings\Application Data\Google\Google Talk\avatars

The images saved in there are PNG files with transparent background, so you can open them with basic image application [maybe, you need to change the application extension to .png]. Do you observe there are two images for each id  there ? One image is 32 x32 [ends with .online.avatar] and the other is 96 x 96 px image [ends with .original.avatar].

You can find much more information about GTalk at http://www.customizetalk.com. This site has some useful information all about the GTalk Themes Development, GTalk Bots Development and many other interesting [and not complicated at the same time] stuff.

Its fun as well as  interesting to find how simple a powerful application can be. Sticking to development basics :)

Update: Find some interesting Google Talk emoticons & their string equivalents here : http://tkhere.blogspot.com/2007/12/brand-new-google-chat-emoticons-no-one.html

Go to Top