//importing opcode.txt
FILE* fp;
char opcode_input[81];
char* mod_opinput[3];
fp = fopen(OPCODE,"r");
while(fgets(opcode_input,80,fp)!=NULL){
//tokenize opcode
mod_opinput[0]=strtok(opcode_input," \n\t");
mod_opinput[1]=strtok(NULL," \n\t");
mod_opinput[2]=strtok(NULL," \n\t");
//generate new node & initialize
Opcode* op_mem = (Opcode*)malloc(sizeof(Opcode));
op_mem->opcodenum = strtol(mod_opinput[0],NULL,16);
strcpy(op_mem->instruction_name,mod_opinput[1]);
strcpy(op_mem->arg_num,mod_opinput[2]);
//setting the index into mod 20
int index = (int)(op_mem->instruction_name[0])%20;
add(op_mem,index);
}//end of file reading
fclose(fp);
위의 예제는 System Programming 에서 니모닉코드를 OPCODE 로 변환하기 위한 해쉬테이블을 만들기 위해,
opcode Structure 에 대한 정보를 TEXT에서 가져오는 코드이다.
-fgets 함수
= 한줄씩 가져온다.
-strtok
= 스트링 토크나이저이다. 이 때,
strtok(NULL," \n\t");
이 부분이 굉장히 의아하다
해당 함수의 헤더를 확인해보면, NULL 이 들어오면 이 전에 토크나이징 하던 포인터를 스태틱 변수 Olds 로 대체한다.
< static char *olds; >
따라서 쓰레드를 이용할 때는 다른 함수를 찾아봐야 한다.
여러개의 토크나이저를 이용할 때는 그냥 여러개를 써주면 된다.
strtok(opcode_input," \n\t");
=> '\n' '\t' ' ' 세 개를 토크나이징 한 것
-strtol
= XX진법의 수를 정수로 바꿔준다.
-strcpy
= 스트링 복사