1 /*
2  * ������ˡ:
3  *     text <book-path> <subbook-index> <number>
4  * ��:
5  *     text /cdrom 0 10
6  * ����:
7  *     <book-path> �ǻ��ꤷ�� CD-ROM ���Ҥ�����������ܤ����ӡ���ʸ
8  *     ����Ƭ���� <number> ��ʬ��ñ�����������Ϥ��ޤ���
9  *
10  *     <subbook-index> �ˤϡ������оݤ����ܤΥ���ǥå�������ꤷ��
11  *     ��������ǥå����ϡ����Ҥκǽ�����ܤ����� 0��1��2 ... ��
12  *     �ʤ�ޤ���
13  */
14 #include "config.h"
15 
16 #include <glib.h>
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <eb/eb.h>
22 #include <eb/error.h>
23 #include <eb/text.h>
24 
isjis(gchar * buff)25 inline gboolean isjis(gchar *buff){
26 	g_assert(buff != NULL);
27 
28 	if((buff[0] >= 0x21) && (buff[0] <= 0x74) &&
29 	   (buff[1] >= 0x21) && (buff[1] <= 0x7E))
30 		return(TRUE);
31 	return(FALSE);
32 }
33 
34 
35 int
main(argc,argv)36 main(argc, argv)
37 int argc;
38 char *argv[];
39 {
40 	EB_Error_Code error_code;
41 	EB_Book book;
42 	EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
43 	int subbook_count;
44 	int subbook_index;
45 	char text[EB_SIZE_PAGE];
46 	ssize_t read_length;
47 	int text_count;
48 	int block_no;
49 
50 	gchar *p_hex;
51 	gchar *p_char;
52 	gchar hex_buff[512];
53 	gchar char_buff[512];
54 	gchar buff[512];
55 	char *result;
56 	int i;
57 
58 	/* ���ޥ�ɹ԰���������å���*/
59 	if (argc != 4) {
60 		fprintf(stderr, "Usage: %s book-path subbook-index block-number\n",
61 			argv[0]);
62 		exit(1);
63 	}
64 
65 	block_no = strtol(argv[3], NULL, 16);
66 
67 	/* EB �饤�֥��� `book' ��������*/
68 	eb_initialize_library();
69 	eb_initialize_book(&book);
70 
71 	/* ���Ҥ� `book' �˷���դ��롣*/
72 	error_code = eb_bind(&book, argv[1]);
73 	if (error_code != EB_SUCCESS) {
74 		fprintf(stderr, "%s: failed to bind the book, %s: %s\n",
75 			argv[0], eb_error_message(error_code), argv[1]);
76 		goto die;
77 	}
78 
79 	/* ���ܤΰ����������*/
80 	error_code = eb_subbook_list(&book, subbook_list, &subbook_count);
81 	if (error_code != EB_SUCCESS) {
82 		fprintf(stderr, "%s: failed to get the subbbook list, %s\n",
83 			argv[0], eb_error_message(error_code));
84 		goto die;
85 	}
86 
87 	/* ���ܤΥ���ǥå����������*/
88 	subbook_index = atoi(argv[2]);
89 
90 	/*�ָ��ߤ����� (current subbook)�פ����ꡣ*/
91 	if (eb_set_subbook(&book, subbook_list[subbook_index]) < 0) {
92 		fprintf(stderr, "%s: failed to set the current subbook, %s\n",
93 			argv[0], eb_error_message(error_code));
94 		goto die;
95 	}
96 
97 
98 	if (zio_lseek(&book.subbook_current->text_zio,
99 		      (block_no - 1) * EB_SIZE_PAGE, SEEK_SET) == -1) {
100 		fprintf(stderr, "Failed to seek zio\n");
101 		goto die;
102 	}
103 
104 	read_length = zio_read(&book.subbook_current->text_zio, text,
105 			       EB_SIZE_PAGE);
106 	if (read_length < 0) {
107 		fprintf(stderr, "Failed to read zio\n");
108 		goto die;
109 	}
110 
111 
112 	printf("block number : 0x%x\n\n", block_no);
113 
114 	for( i = 0 ;  i < EB_SIZE_PAGE ; i=i+2){
115 
116                 // ���ɥ쥹��ɽ��
117 		if((i % 16) == 0){
118 			p_hex = hex_buff;
119 			p_char = char_buff;
120 			sprintf(p_hex, "0x%02x(0x%08x) ", (i / 16), (block_no - 1) * EB_SIZE_PAGE + i);
121 			p_hex += 17;
122 
123 			sprintf(p_char, " ");
124 			p_char += 1;
125 		}
126 
127 		sprintf(p_hex, "%02x ", (unsigned char)text[i]);
128 		p_hex += 3;
129 		sprintf(p_hex, "%02x ", (unsigned char)text[i+1]);
130 		p_hex += 3;
131 
132 
133 		if(isjis(&text[i])) {
134 			*p_char = text[i] | 0x80;
135 			p_char ++;
136 			*p_char = text[i+1] | 0x80;
137 			p_char ++;
138 			*p_char = '\0';
139 		} else {
140 			sprintf(p_char, "..");
141 			p_char +=2;
142 		}
143 
144 		if((i % 16) == 14){
145 			printf("%s", hex_buff);
146 			printf("%s\n", char_buff);
147 		}
148 	}
149 
150 
151 
152 	/* ���Ҥ� EB �饤�֥������Ѥ�λ��*/
153 	eb_finalize_book(&book);
154 	eb_finalize_library();
155 	exit(0);
156 
157 	/* ���顼ȯ���ǽ�λ����Ȥ��ν�����*/
158  die:
159 	eb_finalize_book(&book);
160 	eb_finalize_library();
161 	exit(1);
162 }
163