Array of strings in C
A friend of mine asked how to make an array of char*s. He had trouble finding it online, so here's how I did it:
#include
int main() {
// Declare the array that holds our strings
char* dst[5];
// Usage example
char a[] = "world";
char b[] = "Hello";
char c[] = " ";
char d[] = "!!!";
dst[0] = b;
dst[1] = a;
dst[2] = d;
dst[3] = c;
printf("%s", dst[0]);
printf("%s", dst[3]);
printf("%s", dst[1]);
printf("%s", dst[2]);
// We're done!
return 0;
}
Enjoy.
Posted January 21, 2012. No comments.
Remove one element by index from JavaScript array
I was hunting around for this and eventually found it, but it seemed slightly hard to find this specific case...so here's how:
myArray.splice(myIndex, 1);
Enjoy.
Posted January 14, 2012. No comments.
A couple of resources from my 3rd semester
A couple of assignments in my 3rd semester had me producing things that I think the web might be able to use. Here they are:
- EvanSpec, a unit testing thinger for C++ that aims to be really simple. One
#includeand a bunch of functions in amain()— that's it. Hardly robust, but perhaps useful. - Physics 240 final formulas sheet — My E&M physics class allowed two pages of formulas for the final. I crammed a bunch of stuff onto these two pages. Look it over, though, and make sure I didn't miss anything!
Posted January 11, 2012. No comments.
No Simplenote tagging interface in Chrome
Use the Stylebot Chrome extension and then apply my style to disable the tagging interface in Chrome.
I don't use tags, so I made this extension to hide them in the UI. Makes a simple UI even simpler!
Posted January 9, 2012. No comments.
Load JavaScript asynchronously with ScriptInclude
When I code in browser-based JavaScript, I often miss the #include statements of C and other languages. I decided I'd implement it in JavaScript!
ScriptInclude is a little JavaScript library that allows you to include files. It works like this:
ScriptInclude.include('backbone.js', 'jquery.js', function() {
// Backbone and jQuery are loaded in, let's go for it
});
There are a couple of other things that you can do with it; you can see those in the readme on GitHub.
There are a number of package managers for JavaScript already. I can't claim that ScriptInclude is the best, but it's really simple and that's where I hope it shines.
Go take a look! Let me know what you think.
Posted October 18, 2011. No comments.
“Traditional” for loops in CoffeeScript
CoffeeScript's for-in loops iterate over all of the elements in an array. I wanted to do a "traditional" loop, where I went through the loop by number. CoffeeScript has no way to do this using its for statement, but you can do it:
i = 0
while (i <= 10)
console.log("#{i} is a great number.")
i += 1
This just requires that you remember that for is just syntactic sugar for while.
Posted October 11, 2011. 3 comments.
How to disable copy-paste on your website
Somebody asked me if I could figure out how to disable copy-paste on their website. I did some sleuthing and figured out that the internet really has no idea; here's the most comprehensive way to do it that I found.
In short, for the non-technical
You can't do it perfectly, but here's how you can deter some people.
- Add some CSS to your page.
- Add some JavaScript to your page.
- Add
unselectable="on"property to each HTML tag.
Here's an example page:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
-webkit-user-select: none;
-khtml-user-drag: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
</style>
<script type="text/javascript">
document.ondragstart = function() { return false };
document.onselectstart = function() { return false };
</script>
</head>
<body unselectable="on">
<h1 unselectable="on">You can't select this!</h1>
<p unselectable="on">You can't select this either. <a href="http://evanhahn.com/" unselectable="on">You can't even select <i unselectable="on">this!</i></a></p>
</body>
</html>
If you have any problems at all with this, I'd love to help. Pop an email to me@evanhahn.com and we can get your site un-selected.
CSS for non-IE browsers
There's a CSS property called user-select that lets you do this with CSS (awesome!). Because it's not compatible, it's got browser-specific versions of each tag for each browser. Adding the following to your CSS will make the entire page unselectable:
body {
-webkit-user-select: none;
-khtml-user-drag: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
I'm not entirely clear on the compatibility, but here's what I can piece together:
- Chrome: Compatible with all versions because all versions are built on WebKit.
- Safari: According to the Safari documentation,
-webkit-user-selectis compatible with Safari 3+, and it was called-khtml-user-dragin Safari 2. - Firefox: Compatible with Firefox 1+; this page says it's compatible with Netscape 6.0+, which was apparently built on Firefox 0.6.
- Opera: No idea.
- IE: Even IE9 doesn't support any of these.
JavaScript events for IE (and Safari)
There's a JavaScript event called onselectstart, which is apparently compatible with IE4+ and all versions of Safari, but no other browsers. To use those, pop them in your JavaScript.
document.ondragstart = function() { return false };
document.onselectstart = function() { return false };
This will fill in the gaps between IE4 and IE5.5, assuming you implement the above IE solution.
Foolproof IE 5.5+ way
Internet Explorer (as usual) has its own way of doing things which is very annoying to implement, but it works 100% of the time (as far as I can tell). It's an HTML attribute they introduced: unselectable. In IE, here's how you make an element unselectable:
<div unselectable="on">This text is unselectable by IE users!</div>
That's all good and dandy, but it's got one major issue: the property isn't inherited. If your parent is unselectable, you are selectable by default. For example:
<p unselectable="on">
This text is unselectable by IE users! Unfortunately, <b>this bold stuff IS selectable</b>.
</p>
So, in order to make unselectable work, you have to make sure to apply it on every element that you don't want to be selected. A bit of a pain.
A JavaScript solution to this tediousness
You could, in theory, use JavaScript to go through each HTML element and apply the unselectable property. However, this is only compatible with IE5.5 and up. There's a JavaScript solution that's faster, shorter, and compatible with IE4 and up. It's in the above section, "JavaScript events for IE".
Phew!
After a lot of research, I think I've compiled all the ways you could possibly suppress copy-paste. If you know of other ways to do this, please let me know!
(PS: If you want to copy-paste things from my website, go right ahead! As long as you give credit under the Creative Commons Attribution License, it's all yours. And if it's code, it's free for any use, no credit needed.)
Posted August 26, 2011. 2 comments.
My personal stats page
This one is pretty random, but I designed myself an RPG-style stats page. This is in preparation for an idea I've been kicking around.
Posted August 22, 2011. No comments.
Newline necessary at the end of JavaScript files?
In short: When in doubt, end your JavaScript files with a newline.
I committed some JavaScript code to GitHub. In the commit page, GitHub said "No newline at end of file". I did some research -- why are things this way?
It comes from the C standard where you're tasked to end all files with a newline (and you're not allowed to escape that newline with a \). It makes sense; when the preprocessor concatenates files (or whatever the hell it does), a newline prevents two instructions from piling up on the same line.
There are cases in JavaScript where it can be an issue as well, but not as many. For example, this is perfectly valid JavaScript, which could happen in file concatenation:
return 12; }var x = 10;
But don't worry -- bad things can still happen. For example, I once had a file that ended in a comment. It broke everything when I combined them. This could happen, and it's no good:
x = 0; // x is now 0function doStuff() {
Or you could forget to end your file with a semicolon (or a }). JavaScript won't be able to figure this one out:
x = 0function doStuff() {
So, to conclude: When in doubt, end your JavaScript files with newlines. If the last line is an instruction that has an obvious end (ends in a ; or a }), you don't have to end it in with a newline. If the last line is a comment or has no ending, you should put a newline. And if you only have one JavaScript file, you don't have to worry at all.
Posted August 11, 2011. 1 comment.
How to code Tic-Tac-Toe (and a Lua implementation)
After installing Lua, I had to write Tic-Tac-Toe in Lua for a job interview awhile ago. It seems like a trivial technical problem, but it's not actually that easy. Here's how I did it (which is not the only method):
The board is represented by a 2D table of spaces. They are empty to start (nil in Lua's case). When you play, you put an "x" or an "o" into the table. The board is used to keep track of piece locations and to display them. It does not calculate wins.
The board also has "regions." A region is a place where a player may win (horizontal, vertical, or diagonal). It holds pointers to the board table. Player 1 is represented by +, and Player 2 by —. Each piece in the region increments or decrements the checking of the region. Basically, two X's returns as 2. Two O's returns as -2. 3 or -3 is a winning region.
My Lua implementation is available right here and is free license. I hope it helps you tic-tac-toe programmers! I'm sure there are billions of you.
Posted August 10, 2011. No comments.
« older