Programming Notes
nacho4d avatar

Programming Notes

@nacho4d

clang-format

I've been wanting to use clang-format since a while already. It turns out that installation instructions are kind outdated. I had to read various commits in the clang repository to realize clang-format command it was moved from clang to llvm repository.

2015/01/18 Update: Use brew!

Now clang-format is on brew!. Just do:
brew install clang-format

2014/08/06 Update: Use prebuilds!

Now is even simpler, just download the prebuilds for your system, decompress and use them!
# Download
$ curl -O http://llvm.org/releases/3.4.2/clang+llvm-3.4.2-x86_64-apple-darwin10.9.xz
# Decompress
$ tar xvfJ clang+llvm-3.4.2-x86_64-apple-darwin10.9.xz 
# Locate clang-format
$ ./clang+llvm-3.4.2-x86_64-apple-darwin10.9/bin/clang-format --help
Just one thing. I couldn't find the git hook python script available. Still, it is in the source and it does not need to be compiled, just copy th
# Download only (not the entire llvm project) the repository that contains the hook script
$ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
# Voila!
$ find . name git-clang-format
# Now just copy it somewhere into your $PATH

Old approach: Build llvm


We need to compile LLVM! I wish brew provides something like --with-tools or something like --with-clang-format. For now we have to do the entire thing by ourselves. Some instructions were taken from Clang's getting started page:

Getting LLVM + Clang

$ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
$ cd llvm/tools
$ svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
$ cd ../..
$ cd llvm/tools/clang/tools
$ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
$ cd ../../../..
$ cd llvm/projects
$ svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
$ cd ../..

Building LLVM + Clang

$ ./llvm/configure
$ make
In my case, make didn't succeed, it takes forever and it fails somewhere after building clang-format but that is ok. clang-format was built and I wasn't planning to install llvm/clang from source either, I am fine with Xcode/brew stuff :p

Installing clang-format

If it is not clear where makeput all compiled stuff we can always use find:
$ find . -name "clang-format"
./Release+Asserts/bin/clang-format
./tools/clang/tools/clang-format
We need to copy clang-format command from above location to somewhere in our PATH or make an alias. Also we want to install git-clang-format and maybe other scripts.
# copy clang-format command to your $PATH
$ cp ./Release+Asserts/bin/clang-format /somewhere/in/your/PATH

# install (copy) git-clang-format subcommand to your $PATH
$ cd ./tools/clang/tools/clang-format
$ cp git-clang-format ~/.bin

Using clang-format command

Now, lets say we are inside a directory which contains c/cpp/objc sources which we want to reformat. We simply create a .clang-format file which should be of the form:
Key: Value
Key: Value
Key: Value
Key: Value
And then we would run clang-format file.c Some commands to know more about the command and keys available:
# dump all configuration keys
clang-format -dump-config 
clang-format -dump-config  style=webkit

# help
clang-format --help
clang-format --help-list-hidden

# format a file (it will read .clang-format file if available)
clang-format filename.c

# format a file with a explicit style
clang-format -style=google filename.c
Check clang's official style documentation page for more info.

Using it with git

Since we installed git-clang-format file to our PATH, if we are in a git repository, we can do:
git clang-format
And it will reformat all staged code, pretty awesome!

Are you wondering where did clang-format subcommand come from?! Answer: custom git subcommands are simply executables in the PATH and its name should have the "git-" prefix and should not have extension.

Hope this info is helpful to somebody :)

インターネット接続 制限あり、ウィンドウズタブレットでの件

新しい無線ルーター買って電波が強くなった筈だから楽しみだったのに ウィンドウズタブレットのインターネット接続が「制限あり」??!

いろいろ調べたところアダプターのせいらしい、ドライバーを最新にすべきだそうだ。したけど直らない。

また別のサイトの情報によるとアダプターを無効にしてからまた有効にすると直ることがある。試してみたら実際に直っていた!でも一時的だけ、ちょっとするとまた「制限あり」になった。

「問題のトラブルシューティング」をクリックすると同じく一時的に直る。よく見ると問題解決の後に「アダプターのリセット」というステップがあるからおそらくアダプターを無効にしてから有効にすると同じかと思う。

そこで、このブログの情報を見つけて解決しました!アダプターの設定を変更するのだった!!hiro25hiro07様に感謝感謝!

念のためここに転載。

ネットワークアダプタの設定を変更します。

  1. デバイスマネージャーを開きます。
  2. [ネットワーク アダプター]をダブルクリックします。
  3. [Broadcom 802.11abgn Wireless SDIO Adapter]をダブルクリックします。
  4. プロパティーが表示されますので、[詳細設定]タブをクリックします。
  5. 表示された[詳細設定]タブから   以下の項目の設定変更を行ってください。
    • [20/40 Coexistance]の[Auto]を[Enable]に変更します。
    • [40MHz Intolerant]の[Disabled]を[Enable]に変更します。
    • [802.11n Preamble]の[Auto]を[Mixed Mode]に変更します。
    • [Afterburner]の[Disabled]を[Enable]に変更します。
    • [Bandwidth Capability]の[11a:20/40;11bg:20MHz]を[11a/b/g:20/40MHz]に変更します。
    • 以上で、操作は終了です。


あと、やっぱりドライバーのアップデートした方が良さそうだ、 Acerうんち全然アップデートしないからSony様からパクりました。 とりあえず、 DriverIdenfier.comからv5.93.98.4ダウンロードできた。 93.97.113でもだめでしたし、その一個前もだめだったし。
他のメーカーはちゃんとアップデートバージョンでしているのに、Acerはだしていない。。。ひどい。

参考

Tar

I wish this was a tar tutorial, however there are plenty of them already. Maybe some people find it useful too, specially when downloading, opening tar balls... :)
A ".tar" file is a collection of files within a single file in an uncompressed form. If the file is a ".tar.gz" ( usually called tarball) or ".tgz" file then it is a collection of files in a compressed form.
To compress a file you could create the tar file with the z option. Or alternatively you could create the file with any other tool and then use gzip to compress it.

Some Tar Options

  • c : create a new archive
  • d : delete from the archive
  • r : append files to the end of the archive
  • t : list contents
  • u : update (append files if newer)
  • x : extract
  • f : file?
  • v : verbose
  • z : gzip

Uncomprezing files:

# target file is a zipped tar ball
$ tar -zxvf file.tar.gz
# target file is simply a tar ball
$ tar -xvf file.tar

Listing contents of the file

$ tar -tvf myfile.tar

Compressing files

# tar contents of folder foo in foo.tar
$ tar -cvvf foo.tar foo/
That is all, maybe I add more options as needed.

Installing Emacs from the source

This VM I am in is a bit old and somewhow apt-get is not pulling the latest packages... so I need it to get it manually.
As many other open source software built with make: Get a release, from http://ftp.jaist.ac.jp/pub/GNU/emacs/ for example, then compile and install.
$ wget http://ftp.jaist.ac.jp/pub/GNU/emacs/emacs-24.3.tar.gz
$ tar -xzvf emacs-24.3.tar.gz
$ cd emacs-24.3
$ less INSTALL; # Just take a glance of the install notes
$ ./configure; # Check the output is not something weird
$ make; # No errors should appear here
$ sudo make install; # Gets moved to somewhere in the PATH
If in the way perl complains about LANG, etc variables:
 perl: warning: Setting locale failed.
 perl: warning: Please check that your locale settings:
  LANGUAGE = (unset),
  LC_ALL = (unset),
  LANG = "en_US.utf8"
    are supported and installed on your system.
 perl: warning: Falling back to the standard locale ("C").
Follow instructions: Perl warning Setting locale failed in Debian
$ export LANGUAGE=en_US.UTF-8
$ export LANG=en_US.UTF-8
$ export LC_ALL=en_US.UTF-8
$ sudo /usr/sbin/locale-gen en_US.UTF-8
$ sudo /usr/sbin/dpkg-reconfigure locales
# now choose the appropriate language from the list to generate
Finally re-run from the last step, install in this case:
$ sudo make install

SQL stuff

I am just a beginner in SQL, so I write some trivial stuff here. Lets say I have some datum in my table of logs:
SELECT *, stime - time AS delay FROM logs;
+------+------+-------+-------+
| usid | time | stime | delay |
+------+------+-------+-------+
|    1 |   10 |    10 |     0 |
|    1 |   10 |    12 |     2 |
|    1 |   15 |    17 |     2 |
|    1 |   13 |    15 |     2 |
|    1 |   18 |    21 |     3 |
|    1 |   19 |    22 |     3 |
|    1 |   20 |    25 |     5 |
|    1 |   21 |    26 |     5 |
|    1 |   22 |    26 |     4 |
|    1 |   23 |    26 |     3 |
+------+------+-------+-------+
Where:
  • usid is some random id
  • time is record time in the client side
  • stime is record time in the server side
  • delay is simply the diff of server time and client side


Some exercises:

Count the number of records with a particular delay:
SELECT stime - time AS delay,
       COUNT(*) FROM logs
GROUP BY stime - time;
+-------+----------+
| delay | count(*) |
+-------+----------+
|     0 |        1 |
|     2 |        3 |
|     3 |        3 |
|     4 |        1 |
|     5 |        2 |
+-------+----------+
Too much detail (too granulated), so we want to group 2 and 3 delay into 2, 4 and 5 into 4, etc. To make sure I am doing the right calculations, here is the delay and rounded delay:
SELECT *,
       stime - time AS delay,
       FLOOR((stime - time) / 2) * 2 AS rounded_delay
FROM   logs;
+------+------+-------+-------+---------------+
| stid | time | ctime | delay | rounded_delay |
+------+------+-------+-------+---------------+
|    1 |   10 |    10 |     0 |             0 |
|    1 |   10 |    12 |     2 |             2 |
|    1 |   15 |    17 |     2 |             2 |
|    1 |   13 |    15 |     2 |             2 |
|    1 |   18 |    21 |     3 |             2 |
|    1 |   19 |    22 |     3 |             2 |
|    1 |   20 |    25 |     5 |             4 |
|    1 |   21 |    26 |     5 |             4 |
|    1 |   22 |    26 |     4 |             4 |
|    1 |   23 |    26 |     3 |             2 |
+------+------+-------+-------+---------------+
So now group by the rounded delay (named just "delay" here)
SELECT FLOOR((stime - time) / 2) * 2 AS delay,
       COUNT(*) FROM logs
GROUP BY FLOOR((stime - time) / 2) * 2;
+-------+----------+
| delay | count(*) |
+-------+----------+
|     0 |        1 |
|     2 |        6 |
|     4 |        3 |
+-------+----------+
Add a percentage, I know I have 10 rows in my table so I can do this:
SELECT *,
       c.cnt / 10 AS percentage
FROM (
      SELECT FLOOR((stime - time) / 2) * 2 AS delay,
             COUNT(*) AS cnt
      FROM   logs
      GROUP BY FLOOR((stime - time) / 2) * 2
) c;
+-------+-----+------------+
| delay | cnt | percentage |
+-------+-----+------------+
|     0 |   1 |     0.1000 |
|     2 |   6 |     0.6000 |
|     4 |   3 |     0.3000 |
+-------+-----+------------+
Although, probably is better not to hard-code the count:
SELECT *,
       c.cnt / ( SELECT COUNT(*) FROM logs) AS percentage
FROM (
       SELECT FLOOR((stime - time) / 2) * 2 AS delay,
              COUNT(*) AS cnt
       FROM   logs
       GROUP BY FLOOR((stime - time) / 2) * 2
) c;
+-------+-----+------------+
| delay | cnt | percentage |
+-------+-----+------------+
|     0 |   1 |     0.1000 |
|     2 |   6 |     0.6000 |
|     4 |   3 |     0.3000 |
+-------+-----+------------+
There should be much better ways (better performance) of doing this... I am glad to receive some comments :)

Converting from to unix time

I write this because I always forget the right formats, options...

BSD Date - Mac OSX

The f option is for input format. The j option is to not try to set the time. The last argument is optional, is the output format and it requires an x as a prefix.
  • Convert to unix time
    $ date -jf "%Y-%m-%d %H:%M:%S %Z" "2013-07-19 00:00:01 JST" "+%s"
    1374159601
    
  • Convert from unix time
    $ date -jf "%s" 1374159601 "+%Y-%m-%d %H:%M:%S %Z"
    2013-07-19 00:00:01 JST
    
    Also:
    $ date -jf "%s" 1374159601
    Fri Jul 19 00:00:01 JST 2013
    


GNU Date - Linux, etc

The -d is for the input date. Format is recognized automatically. The last parameter, same as BSD Date, is the output format (optional). It must be prepended by a +.
  • Convert to unix time
    $ date -d "2013-07-19 00:00:00 JST" "+%s"
    1374159600
    
  • Convert from unix time
    $ date -d @1374159600 "+%Y-%m-%d %H:%M:%S %Z"
    2013-07-19 00:00:00 JST
    
    Also :
    $ date -d @1374159600
    Fri Jul 19 00:00:00 JST 2013
    

Gollum

Wikis with Gollum

Gollum is a simple wiki system built on top of Git that powers GitHub Wikis. Super easy to starting working in your wiki.

Installation

gem install gollum redcarpet rdiscount
gollum --help

Usage

git clone https://github.com/allending/Kiwi.git
cd Kiwi.wiki/
gollum
Alternatively
git clone https://github.com/allending/Kiwi.git
gollum --page-file-dir Kiwi.wiki/ TestWiki

Editing changes

As documented, changes done directly in the source should be committed to trigger gollum to reflect them

Coda

If coda is your editor then below markdown mode might become handy: Markdown.mode by bobthecow - Github

This work is licensed under BSD Zero Clause License | nacho4d ®