Find files that contain some text

A short one liner to find all files in a directory tree that contain some text.

grep -Rl “text to searchdirectory

The text to search may be a regular expression as accepted by grep. The directory is the root of the tree you want to search.

Hope this is found helpful.

Comments

Fool Me Once

So I’m at the bus stop when suddenly someone calls for my attention. There is a man, gently dangling a set of keys in front of him. He has a request, some money for a bus. Reason: his car has left him stranded and needs a way to get somewhere, home I suppose. I’m in a good mood, so I help him. Not with spare change, mind you, but with the full fare. I made the mistake of thinking I’d put myself in his position and wrongfully felt pity.
That last part may have sounded cold-hearted, but bear with me. There is more.
I get a ride home most days, and those that I don’t I take a different bus than that time because I’m going to school most days now. As such, it wasn’t until recently that I had to wait at that particular bus stop again. It should be no surprise from the tone of this rant/post/what-have-you what happened then. The same guy, in the same clothes, with the same dangling keys shows up. Not the best memory that guy. I couldn’t find anything to say, and I think the way I looked at him washed over like so much empty air. So curious that his car would break down in the same area, isn’t it?
Now, I’m not completely against begging. For some people it’s the only thing they can do. Here in my country there aren’t many programs to help people with no resources to get a start in life. Old people, children, and the people with special needs often have no access to government services or slip into legal limbo when the requirements to apply come up. Even healthy men and women may have little choice when a lack of skills and education makes getting a job, any job, almost impossible. So what am I’m getting at? That person makes it more difficult to open one’s heart to those in need because he uses deception to take advantage of the scarce goodness and pity that can be found in people nowadays. Worse even, I think it makes it even scarcer.
He should be ashamed of himself. No big chance of someone like that reading a random online blog, much less feeling sorry for being such an [connection interrupted]

Comments

A Fitting Scale

Fitting one rectangle inside another rectangle of different proportions and/or size, in Java of course:

import java.awt.Dimension;
 
// Other stuff, like class declarations and such
 
public static Dimension fitRectInRect(Dimension src, Dimension dst) {
	Dimension result = new Dimension(dst);
	double srcRatio = src.width / (double) src.height;
	double dstRatio = dst.width / (double) dst.height;
	if (srcRatio < dstRatio) {
		result.width = (int) (dst.width * srcRatio);
	}
	else if (srcRatio > dstRatio) {
		result.height = (int) (dst.height / srcRatio);
	}
	return result;
}

This function would take two objects representing a rectangular area each. The first is the size of your original rectangle and the second is the maximum area it can occupy. The return value is the largest rectangle with the same proportions as the source that fits inside the maximum area.
Dimension is used for convenience only, any other container for a width and height can be used, like an SWT Point. The function could be modified to accept other objects or the primitive values of the width and height themselves.

Comments

Add all items in an array to a collection

So I wanted to add all the items in an array to a collection in Java easily. A look into the util package documentation offered a solution which I now share.

yourCollection.addAll(java.util.Arrays.asList(yourArray));

The collection can be any object from the collections framework (List, Set, Queue) and yourArray is the array with the elements to be added.
If you are using generics, the component type of the array has to be a assignment compatible with the type of the collection. In other words, if your list is something like List<CharSequence>, then you can add an array of type StringBuffer[], String[], or CharSequence[], but not Long[].

Comments

Image Resolution

A new year. So many chances to write the year wrong because of habit. Not yet though, so I guess I’m getting the hang of it :P .

Besides the occasional numeric misspelling, it’s customary to set goals of things to do or change during the year. Yes, new year’s resolutions they are and this year I have only one I want to try my hand at. I shall set it henceforth:

During the course of the year 2009, I resolve to make at least one (1) piece of fan art for every web comic I have made a habit of reading.

Which ones those are? To be true I have very lax order on how I keep my bookmarks. I did at one point make a PHP app. that used a database to store the names, urls, and update schedule of each one. It was a very large list, unwieldy at times. Many of those comics had a sad H for hiatus in their schedule. That usually meant they had died :( .

I’ll make the list here, and update when I remember another one. I’ll link to the ones alive (open in new window). They all go to the first page if possible.

Make sure you have lots of spare time if you plan on reading the full archives for some of these. Most of them have been around for a long time.

Comments

A Comment on Comments

A mighty fine day it is today. The sun is shining bright, not a cloud in the sky. Ignore the slight smog and it’s a perfect day. Then you start to hack some code. But what the h-e double hockey sticks does it do?

Now, properly commented code allows you to just scan through a file and you immediately see what it does. No need to trace functions in your head or a handy scribble filled piece of paper (like yours truly). A few lines read in a 200 lines per file and everything is clear. If it’s not working then that’s a whole different affair, but at least you know what it’s supposed to do.

It’s a habit. It is also a courtesy to those working in the same project. It is, in fact a necessity. And it is not that difficult. Write a function, make a comment. A particular conditional spans many lines or has complex conditions? Write a comment. Long blocks of nested statements (loops, conditionals) end, say which one ended please.

Who are comments for? Well, I like to say it is for those who don’t know what the code does, including yourself a few weeks from the time you write it. So you are doing a favour to yourself as well. There’s no excuse to slack off either. Even if a deadline is looming and you must type ‘em fast and furiously, there is a very good chance that someone will come back to it. Code written in express mode is most prone to mistakes after all, and one line of

# this function goes fetch data to x file

is not likely to make you late, right?

There’s another type of quick, in place, code documentation I was almost forgot: naming. That’s the naming in variable naming, method/function naming, any word you use to refer to some object or action in the code. If it is the area in a web page that contains the log-in fields, it’s the loginPane, loginDiv, loginWhatever, you get the idea. But what exactly are Panel1, Section42, and Button6? I have no idea. I would have a very good idea of what buttonSubmit and textPassword would do. In fact the search function of any half decent text editor can be used to search for password and one of the matches would be the password field. Who would search for TextField45? Sensible name selection helps everyone.

I can’t tag all the blame of bad naming on the developer or designer, though. Sometimes some IDEs make it too easy to just place elements in a drag-n-drop fashion and assigns names in the same fashion as the previous paragraph’s examples (I’m looking at you Microsoft Visual xx series). So be careful with those, especially if one person places widgets and another one codes them separately.

It’s simple and quick, so no excuses. Do it for yourself, your peers, do it for the children. Use comments, otherwise the chicken will get you.

Comments