2009-01-29

Master Boot Record

/** * From Solaris x86, /usr/include/dktp/fdisk.h */ struct fdisk_partition { unsigned char bootid; /* bootable? 0=no, 128=yes */ unsigned char beghead; /* beginning head number */ unsigned char begsect; /* beginning sector number */ unsigned char begcyl; /* 10 bit nmbr, with high 2 bits put in begsect */ unsigned char systid; /* Operating System type indicator code */ unsigned char endhead; /* ending head number */ unsigned char endsect; /* ending sector number */ unsigned char endcyl; /* also a 10 bit nmbr, with same high 2 bit trick */ int relsect; /* first sector relative to start of disk */ int numsect; /* number of sectors in partition */ }; struct master_boot_record { char bootinst[446]; /* space to hold actual boot code */ char parts[4 * sizeof (struct fdisk_partition_table)]; ushort signature; /* set to 0xAA55 to indicate PC MBR format */ }; /** * From util-linux-2.12r/fdisk/fdisk.h */ #define sector(s) ((s) & 0x3f) #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2)) struct partition { unsigned char boot_ind; /* 0x80 - active */ unsigned char head; /* starting head */ unsigned char sector; /* starting sector */ unsigned char cyl; /* starting cylinder */ unsigned char sys_ind; /* What partition type */ unsigned char end_head; /* end head */ unsigned char end_sector; /* end sector */ unsigned char end_cyl; /* end cylinder */ unsigned char start4[4]; /* starting sector counting from 0 */ unsigned char size4[4]; /* nr of sectors in partition */ } PACKED;

2008-07-01

convention

LAST UPDATED: 2008-07-03

Shortcuts: C C++ Java PHP Python Ruby

Ruby:
# The name of a class name is a noun.
# Camel case is used.
# (Every word starts with an upper case letter.)
class RectangularShape
    
    # The name of a class constant if a noun.
    # The name must start with an upper case letter
    #   as required by the language.
    MAXIMUM_WIDTH = 1024
    MAXIMUM_HEIGHT = 768
    
    # The name of a class variable is a noun.
    # The name must start with "@@"
    #   as required by the language.
    # Lower case letters are used.
    # Every word is separated by an underscore.
    @@instance_count = 0
    
    # The name of a class accessor method
    #   has the same name as the class variable.
    def self.instance_count
        @@instance_count
    end
    
    # Constructors must be named "initialize"
    #   as required by the language.
    def initialize(x, y, w, h)
        @instance_id = @@instance_count++
        @x_coordinate = x
        @y_coordinate = y
        @width = w
        @height = h
    end
    
    # The name of an instance accessor method
    #   has the same name as the instance variable.
    def instance_id
        # The name of an instance variable is a noun.
        # The name must start with "@"
        #   as required by the language.
        # Lower case letters are used.
        # Every word is separated by an underscore.
        @instance_id
    end
    
    def x_coordinate
        @x_coordinate
    end
    
    def y_coordinate
        @y_coordinate
    end
    
    def width
        @width
    end
    
    def height
        @height
    end
    
    # The name of a method starts with a verb.
    # Lower case letters are used.
    # Every word is separated by an underscore.
    def move_to(x, y)
        @x_coordinate = x
        @y_coordinate = y
    end
    
    def contains?(x1, y1)
        # The name of a local variables starts
        #   with a lower case letter or an underscore.
        # Short forms or single letter names are used.
        # Every word is optionally delimited by an underscore.
        retval = (x1 >= @x_coordinate) &&
                 (x1 < @x_coordinate + @width) &&
                 (y1 >= @y_coordinate) &&
                 (y1 < @y_coordinate + @height)
    end
end

# The name of a global variable is a noun.
# Lower case letters are used.
# Every word is separated by an underscore.
$debug_mode = 0

variable

Ruby:
# All Ruby variables hold objects.
# Therefore, integers are objects.
a = 1

# Parallel assignment
b, c, d = 2, 3, 4

constant

Ruby:
# Ruby constants begin with a capital letter.

# This is a constant.
# This style is recommended.
# Upper case delimited by underscore distinguishes
#   a constants from a class.
MY_CONSTANT = 1

# This is also a constant.
# Camel case is used in this example.
MyConstant = 2

2008-06-25

if-elseif-else

LAST UPDATED: 2008-07-03

Shortcuts: C C++ Java PHP Python Ruby

C:
if (1 + 1 != 2) {
    printf("1 + 1 is not 2.\n");
} else if (1 + 1 == 2) {
    printf("1 + 1 is 2.\n");
} else {
    printf("This will NOT be printed.\n");
}
C++:
if (1 + 1 != 2) {
    cout << "1 + 1 is not 2." << endl;
} else if (1 + 1 == 2) {
    cout << "1 + 1 is 2." << endl;
} else {
    cout << "This will NOT be printed." << endl;
}
Java:
if (1 + 1 != 2) {
    System.out.println("1 + 1 is not 2.");
} else if (1 + 1 == 2) {
    System.out.println("1 + 1 is 2.");
} else {
    System.out.println("This will NOT be printed.");
}
PHP:
# if-elseif-else (normal syntax)
if (1 + 1 != 2) {
    echo '1 + 1 is not 2.', "\n";
} elseif (1 + 1 == 2) {
    echo '1 + 1 is 2.', "\n";
} else {
    echo 'This will NOT be printed.', "\n";
}

# if-elseif-else (alternate syntax)
if (1 + 1 != 2):
    echo '1 + 1 is not 2.', "\n";
elseif (1 + 1 == 2):
    echo '1 + 1 is 2.', "\n";
else:
    echo 'This will NOT be printed.', "\n";
endif;
Python:
if 1 + 1 != 2:
    print '1 + 1 is not 2.'
elif 1 + 1 == 2:
    print '1 + 1 is 2.'
else
    print 'This will NOT be printed.'
Ruby:
# if-elseif-else (normal syntax)
if 1 + 1 != 2 then
    puts '1 + 1 is not 2.'
elsif 1 + 1 == 2:
    puts '1 + 1 is 2.'
else
    puts 'This will NOT be printed.'
end

# if-elseif-else (without "then")
if 1 + 1 != 2
    puts '1 + 1 is not 2.'
elsif 1 + 1 == 2:
    puts '1 + 1 is 2.'
else
    puts 'This will NOT be printed.'
end

# if-elseif-else (prefixed syntax)
if 1 + 1 != 2: puts '1 + 1 is not 2.'
elsif 1 + 1 == 2: puts '1 + 1 is 2.'
else: puts 'This will NOT be printed.'
end

# if (prefixed syntax)
if 1 + 1 == 2: puts '1 + 1 is 2.' end

# if (suffixed syntax)
puts '1 + 1 is 2.' if 1 + 1 == 2

for-loop

LAST UPDATED: 2008-07-03

Shortcuts: C C++ Java PHP Python Ruby

C:
/* Prints 0, 1, 2, ... 9 */
int i;
for (i = 0; i < 10; ++i) {
    printf("%d\n", i);
}
C++:
// Prints 0, 1, 2, ... 9
for (int i = 0; i < 10; ++i) {
    cout << i << endl;
}
Java:
// for-loop
// Prints 0, 1, 2, ... 9
for (int i = 0; i < 10; ++i) {
    System.out.println(i);
}

// foreach-loop (since Java 5.0)
// This works for arrays and classes that implement Iterable.
String[] a = {"Alpha", "Beta", "Gamma", "Delta"};
for (String elem : a) {
    System.out.println(elem);
}
PHP:
# for-loop (normal syntax)
for ($i = 0; $i < 10; ++$i) {
    echo $i, "\n";
}

# for-loop (alternate syntax)
for ($i = 0; $i < 10; ++$i):
    echo $i, "\n";
endfor;

# foreach-loop (since PHP 4)
# In PHP 4, this only works for arrays.
# In PHP 5, this also works for iterating objects.

# foreach-loop (normal syntax)
$a = array('Alpha', 'Beta', 'Gamma', 'Delta');
foreach ($a as $elem) {
    echo $elem, "\n";
}

# foreach-loop (alternate syntax)
$a = array('Alpha', 'Beta', 'Gamma', 'Delta');
foreach ($a as $elem):
    echo $elem, "\n";
endforeach;

# foreach-loop (normal syntax)
$a = array(
    'A' => 'Alpha',
    'B' => 'Beta',
    'C' => 'Gamma',
    'D' => 'Delta'
);
foreach ($a as $key => $value) {
    echo $key, "\t", $value, "\n";
}

# foreach-loop (alternate syntax)
$a = array(
    'A' => 'Alpha',
    'B' => 'Beta',
    'C' => 'Gamma',
    'D' => 'Delta'
);
foreach ($a as $key => $value):
    echo $key, "\t", $value, "\n";
endforeach;
Python:
# for-loop (with range(), ascending)
# Prints 0, 1, 2, ... 9
for i in range(10):
    print i

# for-loop (with range(), ascending)
# Prints 0, 1, 2, ... 9
for i in range(0, 10):
    print i

# for-loop (with range(), descending)
# Prints 10, 9, 8, ... 1
for i in range(10, 0, -1):
    print i

# foreach-loop
a = ['Alpha', 'Beta', 'Gamma', 'Delta']
for elem in a:
    print elem
Ruby:
# for-loop (inclusive range)
# Prints 0, 1, 2, ... 9
for i in 0..9 do
    puts i
end

# for-loop (exclusive range)
# Prints 0, 1, 2, ... 9
for i in 0...10 do
    puts i
end

# for-loop (with upto())
# Prints 0, 1, 2, ... 9
0.upto(9) do |i|
    puts i
end

# for-loop (with downto())
# Prints 10, 9, 8, ... 1
10.upto(1) do |i|
    puts i
end

# for-loop (brace style)
# Prints 10, 9, 8, ... 1
# "{}" style to write "do ... end"
10.upto(1) { |i|
    puts i
}

Comments

LAST UPDATED: 2008-06-25

Shortcuts: C C++ Java PHP Python Ruby

C:
/*
C style
multiple line
comment
*/
C++:
// C++ style single line comment

/*
C style
multiple line
comment
*/
Java:
// C++ style single line comment

/*
C style
multiple line
comment
*/

/**
 * Javadoc comment
 */
PHP:
// C++ style single line comment

# Perl style single line comment

/*
C style
multiple line
comment
*/
Python:
# Perl style single line comment

# Below is an encoding comment:
# -*- coding: iso-8859-15 -*-
Ruby:
# Perl style single line comment

=begin
Ruby style
multiple line
comment
=end