Yi's Blog

不图新奇,但问优劣

年终的胡言乱语

IMG_5033

窗外阳光明媚,内心几多焦灼。

距离上篇博客的更新已经过去了半年之多,之所以没有持续的更新,除了懒惰之外,还因为生活发生了诸多变化:上半年离开了生活了三年的北京,告别了自己的学生时代,下半年来到了上海,开始了全新的生活。

与工作相比,在计算所求学的三年过得并不轻松。读研后期的科研任务和工作之后的任务不同,没有一个明确完成的标准,整个过程都需要不断的探索、反思和修改。做好科研工作需要全身心的投入和长期的努力,不会因为你熬了几个晚上,或者勤奋上几周就会有明显的结果。而我在开始研究生生活之前就做好了要为找个好工作而努力奋斗的准备,并没有沉下心全身心的完成科研工作,因此离开计算所时,我并没有带着很多留恋的情绪,与之相反,更多是松了一口气。

除了科研上的压力,在读研期间我感受到最多的还有人与人之间的隔阂。在整个读研的过程中,每个人都沉浸在时间的催促中,努力地思考自己毕业之后留在哪个城市,找个什么样的工作。这种使命感,因为年龄的原因,与本科时相比来得更加强烈,强烈到让人无暇顾及旁人的感受。在读研期间,你能看到许多同学的身影,但是却很难坐下来闲聊和相处。这种若即若离的相识让我感到人与人之间连接的脆弱,这个时代好像已经不再有什么肝胆相照,两肋插刀之类的故事,到处都是岌岌可危,自顾不暇。

当然,研究生生活也给我留下了许多美好的回忆。譬如说在找工作时实验室的同学共同讨论题目时的自由,在一次次提心吊胆的考验结束时偶有的欢愉,在酣畅淋漓的运动之后瘫坐在宿舍里享受无所事事的时光。

7 月底来到上海时,天气闷热难耐。在曝晒的阳光下,穿梭于陆家嘴周围不几天就把我晒得黝黑。最后租到的房子虽然有点贵,但是到公司只要步行上班,还挺方便的。

我不善于融入新的环境,每次进入一个新的环境,总要花上几个月才会适应。

希望一切安好。

- EOF -

《文明 5》科技树

去年大概玩了十多个小时的《文明 5》,很喜欢其中丰富的游戏内容,特别是对于科技树的设定,觉得光知道这些科技的名字和发展顺序就是很有趣的事情。

诗五首

二月四号那天,因为准备毕设的原因,睡得不踏实,凌晨四点醒来。

醒来时突然感觉心里有话要说,便写下了如下的句子,妄自称之为诗吧。

Reverse a char

I met this problem in one interview: How to reverse a char? It took me a long time to figure out which I thought was very easy.

#include <iostream>
using namespace std;

unsigned char reverse_char(unsigned char in) {
    unsigned char high = 0x80;
    unsigned char res = 0x0, tmp = 0x0;
    for (int i=7; i>=0; i--) {
        tmp = (in&0x1)<<i;
        in = in>>1;
        res = (res&~high)|tmp;
        high = high>>1;
    }
    return res;
}

int main(int argc, char *argv[]) {    
    printf("%x\n", reverse_char(0xc4));
}

You have to pay attention to the difference of char and unsigned char.

More info about signed char: What does it mean for a char to be signed? - Stack Overflow

- EOF -

Fix Soft Links in an Archived File

OS X 10.8 sdk was not included in Xcode 6.1, so if you want to use that sdk, you have to add it to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ yourself. For me, the reason I needed 10.8 sdk was I wanted to compile objc runtime source code with it so I can debug the code.

How to get the previous sdks

You can extract previous sdks from old versions fo Xcode. Or you can get them more efficently from this repository(phracker/MacOSX-SDKs) by using git or just downloading the zip file.

But if you choose downloading the zip file, the zip format will break all the soft link by changing them into plain text files.

How to fix it

In order to fix it, I wrote several lines of python. Here is the code:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Jeswang<wangyi724@gmail.com>
#         http://blog.jeswang.org
# Created on 2014-10-22 13:03:12

import os

def file_exists(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False

def folder_exists(foldername):
    try:
        os.walk(foldername).next()
        return True
    except StopIteration:
        return False

def generateSoftLink(realfile, linkfile):
    print("%s -> %s" % (linkfile, realfile))
    os.symlink(realfile, linkfile)

def generateSoftLinkForPath(path):
    list_of_files = {}
    for (dirpath, dirnames, filenames) in os.walk(path):
        for filename in filenames:
            print filename
            linkfile = dirpath+"/"+filename

            if os.path.islink(linkfile) or os.path.getsize(linkfile) > 200:
                continue

            f = open(linkfile)
            relpath = f.readline()
            f.close()

            if len(relpath) == 0:
                continue

            relpath = relpath.replace("\n", "")

            if len(relpath.replace("/", "")) == 0:
                continue

            realfile = dirpath+"/"+relpath

            if file_exists(realfile) or folder_exists(realfile):
                os.remove(linkfile)
                generateSoftLink(relpath, linkfile)

if __name__ == "__main__":
    generateSoftLinkForPath("/Users/jeswang/Downloads/MacOSX-SDKs-master")

The tricky part is, under mac os x python’s function os.path.exists can’t deal well with soft links, so you have to test whether there is a file or folder in given path by trying to read the file or list the folder.

Because of the recursion of references, you may need to run this script several time to make sure all the soft link have be changed back.

- EOF -