Dec 21, 2007

C pointer FAQ :D

Of course, pointers are not limited to ints. It's quite common to use pointers to other types, especially char. Here is the innards of the mystrcmp function we saw in a previous chapter, rewritten to use pointers. (mystrcmp, you may recall, compares two strings, character by character.)

    // p1, p2 point to the address of the first character in str1, str2 respectively
    char *p1 = &str1[0], *p2 = &str2[0];

    while(1)
        {
            // compare the values of str1[x], str[x] at the position x. Not equal
            if(*p1 != *p2)
                return *p1 - *p2;
            // we reached the end of both str1, str2 -> they are equal.
            if(*p1 == '\0' || *p2 == '\0')
                return 0;
            // p1++ and p2++ do the increment of p1 and p2.
            // i.e., the next characters in str1 ( str[x + 1] and str2 ( str2[ x + 1]
            p1++;
            p2++;
        }


As another example, here is the strcpy (string copy) loop from a previous chapter, rewritten to use pointers:

    char *dp = &dest[0], *sp = &src[0];
    while(*sp != '\0')
        // *dp++ means *(dp++), i.e., the value of the pointer next to dp.
        // to access the address of the next pointer of dp, use (*dp)++
        *dp++ = *sp++;
    *dp = '\0';

(One question that comes up is whether the expression *dp++ increments p or what it points to. The answer is that it increments p. To increment what p points to, you can use (*dp)++.)

ref: http://www.eskimo.com/~scs/cclass/notes/sx10b.html

Read and write png files bytes by bytes

#include 
#include 
#include 

#define WIDTH  (3)
#define HEIGHT (3)

int main()
{
    unsigned char   **image;                                // image[HEIGHT][WIDTH]の形式です
    int             i, j;

    image = (png_bytepp)malloc(HEIGHT * sizeof(png_bytep)); // 以下3行は2次元配列を確保します
    for (j = 0; j < HEIGHT; j++)
            image[j] = (png_bytep)malloc(WIDTH * sizeof(png_byte));


    for (i = 0; i < WIDTH; i++) {                           // 以下5行は単純なテストパターンを作ります
            for (j = 0; j < HEIGHT; j++) {
                    image[j][i] = (unsigned char)i;
            }
    }


    unsigned char *image2;

    image2 = (png_bytep) malloc( HEIGHT * WIDTH * sizeof(png_byte));

    for (i = 0; i < WIDTH; i++) {                           // 以下5行は単純なテストパターンを作ります
            for (j = 0; j < HEIGHT; j++) {
                    //printf("%d ", image[j][i]);
                    image2[j * WIDTH + i] = image[j][i];
                    printf("%d ", image2[j * WIDTH + i]);

            }
    }
    // TODO: write image2 to png stream
    // http://www.jah.ne.jp/~naoyuki/Writings/MngPng.html
    return 0;
}

Solaris 10: vim 7.1 color

Solaris 10 vim71 color
wget vim-7.1.tar.gz
tar xzvf vim-7.1.tar.gz
cd vim71
./configure --prefix=$HOME CC=gcc
make; make install
echo "syntax on" > .vimrc
export TERM=sun-color ( or echo "export TERM=sun-color" >> $HOME/.bash_profile

or TERM=xterm
or TERM=xtermc

Dec 20, 2007

Solars 10: Graphics Magick 1.10 installation

/bin/bash ../../libtool --silent   --mode=link g++  -D_REENTRANT -pthreads -version-info 1:3:0 -L/usr/sfw/lib -L/usr/lib -o libGraphicsMagick++.la -rpath /export/home/vuhung/lib Blob.lo BlobRef.lo CoderInfo.lo Color.lo Drawable.lo Exception.lo Functions.lo Geometry.lo Image.lo ImageRef.lo Montage.lo Options.lo Pixels.lo STL.lo Thread.lo TypeMetric.lo ../../magick/libGraphicsMagick.la
libtool: link: `/usr/sfw/lib/libstdc++.la' is not a valid libtool archive
make[3]: *** [libGraphicsMagick++.la] Error 1
make[3]: Leaving directory `/export/home/vuhung/usr/src/GraphicsMagick-1.1.10/Magick++/lib'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/export/home/vuhung/usr/src/GraphicsMagick-1.1.10/Magick++/lib'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/export/home/vuhung/usr/src/GraphicsMagick-1.1.10/Magick++'
make: *** [all-recursive] Error 1



232  ~/bin/tar xjvf GraphicsMagick-1.1.10.tar.bz2
233  cd GraphicsMagick-1.1.10
257  ./configure --prefix=$HOME CC=gcc CXX=g++  --enable-ccmalloc -without-perl
258  ~/bin/make
269  make install

bash-3.00# diff /usr/sfw/lib/libstdc++.la /usr/sfw/lib/libstdc++.la.original.2007-12-19.vh
1,32d0
< # libstdc++.la - a libtool library file
< # Generated by ltmain.sh - GNU libtool 1.4a-GCC3.0 (1.641.2.256 2001/05/28 20:09:07 with GCC-local changes)
< #
< # Please DO NOT delete this file!
< # It is necessary for linking the library.
<
< # The name that we can dlopen(3).
< dlname='libstdc++.so.6'
<
< # Names of this library.
< library_names='libstdc++.so.6.0.3 libstdc++.so.6 libstdc++.so'
<
< # The name of the static archive.
< old_library='libstdc++.a'
<
< # Libraries that this one depends upon.
< dependency_libs='-lc -lm -L/usr/sfw/lib -lgcc_s'
<
< # Version information for libstdc++.
< current=6
< age=0
< revision=3
<
< # Is this an already installed library?
< installed=yes
<
< # Files to dlopen/dlpreopen
< dlopen=''
< dlpreopen=''
<
< # Directory that this library needs to be installed in:
< libdir='/usr/sfw/lib'

bash-3.00# cat /etc/release
                     Solaris 10 6/06 s10s_u2wos_09a SPARC
         Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
                      Use is subject to license terms.
                           Assembled 09 June 2006
                         Preinstall P/N 259-4616-01
                            Built 13 August 2006
bash-3.00# uname -a
SunOS soltest01 5.10 Generic_118833-22 sun4u sparc SUNW,Sun-Fire-V215
bash-3.00#
http://forum.java.sun.com/thread.jspa?threadID=5073150

Open Solaris 10 pkgrm pkgadd

-> pkgrm pkgname then -> pkgadd -d pkgnameFULL bash-3.00# pkgadd -d libgcc-3.4.6-sol10-sparc-local The following packages are available: 1 SMClgcc346 libgcc (sparc) 3.4.6 Select package(s) you wish to process (or 'all' to process all packages). (default: all) [?,??,q]: aa ERROR: Entry does not match available menu selection. Enter the number of the menu item you wish to select, or the token which is associated with the menu item, or a partial string which uniquely identifies the token for the menu item. Enter ?? to reprint the menu. Select package(s) you wish to process (or 'all' to process all packages). (default: all) [?,??,q]: all 中のパッケージインスタンス を処理中です。 libgcc(sparc) 3.4.6 FSF をパッケージのベースディレクトリとして使用します。 ## パッケージ情報を処理中です。 ## システム情報を処理中です。 5 個のパッケージパス名がすでに正しくインストールされています。 ## ディスク領域の要件を確認中です。 ## すでにインストール済みのパッケージとの重複を確認中です。 次のファイルは、すでにシステムにインストールされていますが、 現在、他 のパッケージが使用中です: /usr/local/lib/libg2c.so.0.0.0 /usr/local/lib/libgcc_s.so.1 /usr/local/lib/libstdc++.so.6.0.3 これらの重複しているファイルをインストールしますか [y,n,?,q] y ## setuid/setgid を行うプログラムを検査中です。 libgcc を としてインストール中です。 ## 1/1 部分をインストールしています。 /usr/local/lib/libg2c.so.0.0.0 /usr/local/lib/libgcc_s.so.1 /usr/local/lib/libstdc++.so.6.0.3 [ クラス を検査しています ] のインストールに成功しました。 bash-3.00# pkgadd -d gcc-3.4.6-sol10-sparc-local The following packages are available: 1 SMCgcc gcc (sparc) 3.4.6 Select package(s) you wish to process (or 'all' to process all packages). (default: all) [?,??,q]: all 中のパッケージインスタンス を処理中です。 gcc(sparc) 3.4.6 現在の管理上では、 パッケージに固有なインスタンスを作成する必 要があります。ただし、同じシステム上で 1 度にサポートできる最大数の パッケージインスがすでに存在しています。 システムは変更されていません。