spacemacs – install spacemacs

install spacemacs is very simple.
make sure using emacs version >= 24.5

but the keypoint is to remove “.emacs” file.
it will force emacs to load config from “.emacs.d” folder.

in windows, %HOME% must be set to a path for .emacs.d

Linux:
$ git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

Windows:
> cd %HOME%
> git clone https://github.com/syl20bnr/spacemacs .emacs.d

spacemacs

on mac os, sometime lock file is very annoying.

every time you edit it, emacs will ask you to choose s,p,q,?,

check more details.
https://www.gnu.org/software/emacs/manual/html_node/emacs/Interlocking.html

Here is code to make it work.
;; for mac os x
(setq auto-save-default nil)
(setq create-lockfiles nil)

TCC – dynarray_add

This is very common function in TCC state.

here is main logical.

1
2
3
4
5
6
7
dynarray_add(void ***ptab, int *nb_ptr, void *data) 
{
...
pp= *ptab; nb=*nb_ptr;
pp[nb++] = data;
...
}

what dynarray_add do is simple. input array has just 3 parts that is double pointer, index and value.

then assign data to double pointer just like assign string to char array.

Here is sample call stack, it call to add pathname into library_paths.

1
2
3
dynarray_add(p_ary, p_nb_ary, str.data);
tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);

the define of library_paths is following

1
2
3
4
5
6
struct TCCState {
...
    char **library_paths;
    int nb_library_paths;
...
}

ionic side menu left button and right button disappear

When doing Ionic project, one problem that sometime nav buttons for side menu will disappear, this is very annoying.

Here is sample code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<ion-side-menu-content>
  <ion-nav-bar class="bar-positive">
 
    <ion-nav-buttons side="left">
      <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
      </button>
    </ion-nav-buttons>
 
    <ion-nav-buttons side="right">
      <button class="button button-icon button-clear ion-navicon" menu-toggle="right">
      </button>
 
    </ion-nav-buttons>
  </ion-nav-bar>
 
  <ion-nav-view name="centre-panel"></ion-nav-view>
 
</ion-side-menu-content>

The solution is following: adding enable-menu-with-back-views=”true” to ion-side-menus

1
<ion-side-menus enable-menu-with-back-views="true">

Lucene – Build Lucene and import to eclipse.

This is first article on Lucene source code analysis.
There are two ways to get source code, one of them are download directly form lucene’s WebSite http://www.apache.org/dyn/closer.lua/lucene/java/6.2.1

you could choose to download lucene-6.2.1-src.tgz and release binary build lucene-6.2.1.tgz .

However, lucene-6.2.1-src.tgz are not easy to import to eclipse.run “ant -p” to check all commands and “ant eclipse” command is not ready yet. you have to check out https://github.com/apache/lucene-solr. From readme you could find the following message
“To compile the sources run ‘ant compile’
To run all the tests run ‘ant test’
To setup your ide run ‘ant idea’, ‘ant netbeans’, or ‘ant eclipse'”

install ivy for ant.
Download ivy file such as “apache-ivy-2.4.0-bin.zip” and put to ant library file.

after git clone from https://github.com/apache/lucene-solr, run “ant eclipse” and then run eclipse , “Import” -> “Existing Projects into workspace”

TCC – string index from char str[1] to str[len]

Check TokenSym declaration firstly, TokenSym is to store a single token info.

1
2
3
4
5
6
7
8
9
10
typedef struct TokenSym {
    struct TokenSym *hash_next;
    struct Sym *sym_define; /* direct pointer to define */
    struct Sym *sym_label; /* direct pointer to label */
    struct Sym *sym_struct; /* direct pointer to structure */
    struct Sym *sym_identifier; /* direct pointer to identifier */
    int tok; /* token number */
    int len;
    char str[1];
} TokenSym;

when malloc memory, it will alloc TokenSym+len.

1
ts = tcc_malloc(sizeof(TokenSym) + len);

the later on, memcpy copy str with len to str memory.
Of course, str[1] will be out of range, but extra len memory is appended.
therefore, str[1] is expanded to str[len].

This is very popular within TCC’s struct declaration.

1
2
memcpy(ts->str, str, len);
ts->str[len] = '\0';

Google CodeJam – Problem B. Rank and File

https://code.google.com/codejam/contest/4304486/dashboard#s=p1&a=2

the idea is very simple, every items on the matrix will appear even time, therefore, for the row which is missing, all items should have odd times.

the code is following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
int main(int argc,char **argv) 
{ 
  int N;
  int array[3000];
  scanf("%d\n",&N);
 
  for(int i=0;i<N;i++)
  {
	  int T;
	  scanf("%d\n",&T);
	  memset(array,0,sizeof(int)*3000);
 
	  int temp;
	  for(int m=0;m<2*T-1;m++)
	  {
		  for(int n=0;n<T;n++)
		  {
			scanf("%d",&temp);
			array[temp]++;
		  }
	  }
 
	  printf("Case #%d: ",(i+1));
 
	  for(int m=0;m<3000;m++)
	  {
		  if(array[m]>0 && array[m]%2==1)
		  {
			  printf("%d ",m);
		  }
	  }
	  printf("\n");
  }
 
  return 0; 
}

Google CodeJam – Problem A. The Last Word

https://code.google.com/codejam/contest/4304486/dashboard#s=p0&a=2

This problem is very simple, use basic linked operation will be enough,

it only compare the head item and tail item.

if new item > head item, then new item will be inserted into head.

if new item <= head item, then it will be inserted int tail. check following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
struct element
{
	char c;
	struct element *next;
};
 
int main(int argc,char **argv) 
{ 
	int N;
	char c;
	scanf("%d\n",&N);
 
	char array[2000];
 
	for(int i=0;i<N;i++)
	{
		memset(array,0,sizeof(char)*2000);
 
		scanf("%s\n",array);
 
 
		struct element *tail;
		struct element *head = (struct element *)malloc(sizeof(struct element));
		memset(head,0,sizeof(struct element));
		head->c = array[0];
		head->next = NULL;
		tail = head;
 
		for(int j=1;array[j]!='\0';j++)
		{
			if(array[j]>=head->c)
			{
				struct element *e = (struct element *)malloc(sizeof(struct element));
				e->c = array[j];
				e->next = head;
				head = e;
			}
			else
			{
				struct element *e = (struct element *)malloc(sizeof(struct element));
				e->c = array[j];
				e->next = NULL;
				tail->next = e;
				tail = e;
			}
		}
		printf("Case #%d: ",i+1);
		struct element *p = head;
		for(;;)
		{
			if(p!=NULL){
				printf("%c",p->c);
				p=p->next;
			}
			else
				break;
		}
		printf("\n");
 
	}
 
	return 0; 
}

Sqlite DateTime compare in WebSQL

Sqlite has DateTime. However, in my test ‘DateTime’ is similar with ‘Text’.

for example, javascript moment.js,

1
2
3
4
5
6
 
var date = new moment().format('YYYY-MM-DD hh:mm');
saveToDatebase(date);
 
var date = new moment().format('YYYY-MM-DD h:mm a');
saveToDatebase(date);

in sqlite, two different format will saved as different string.

it is better not to compare two different format, such as ‘2016-10-10’ and ‘2016-10-10 10:10:10’. it is comparable, but it is meanless.

therefore, in my test, and date time should be save as YYYY-MM-DD hh:mm, such as ‘2016-10-10 10:10’ format.

then, sql statement like ‘select * from test where startDate < '2016-10-10 15:15' will work perfectly well.

change Storyboard to xib project

as we know, project with xib now is no allowed to create. It only allow Storyboard project, xcode 6.4 for example,

However, following the steps you could still be able to create a project with xib.

1. create a Storyboard, you have no choices but make storyboard

2. right click on project file, choose iOS->”User Interface”->View

Screen Shot 2016-03-19 at 上午1.53.28

3. after you add xib to project, make sure move it to right folder.

4. right click on project property, find “App Icons and Launch Images”
-> Lanuch Screen File
Screen Shot 2016-03-19 at 上午2.02.00