- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis8 ]5 R a! O3 W2 A' I
- # q$ {5 F4 J( l% N5 t
- //
- D, ^4 s! d! m+ g* f' o - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
+ `# }+ l9 j9 W1 a - // In case this is not legally possible,
: T% |) n" L% k# v8 U - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.( W0 s# |9 d7 w3 d4 |! t* W; l
- //$ E9 o9 f/ w2 Z9 S2 h+ v/ a1 v
8 A! Q9 R3 }' }- #include <iostream> w' z) y" m; ^- d* d
- #include <fstream>
% `& d8 s9 o' G, ^, { - #include <cctype>- }- I, W7 d' _; ?
- using namespace std;) I$ E5 W8 a3 T
- $ ?/ D( t3 I9 }- h, U
- // MACROS/ U- K8 p4 j% @
- #define SYSTEM_BLOCK_STR "[system]"& V$ W1 \% e# N
- #define NICKNAME_STR "nickname = "
& h3 M( N( F$ y - #define IDS_STR "strid_name = "4 `( n" w- r+ M8 A9 M) R$ J
- //+ ]8 J! i4 w+ o9 G; S3 X
1 b! X) { j+ C+ Z- ifstream inf;; V, @: X; c6 Y- ], N$ g: `
- char *buffer;' s% r, ~* V" ~' S* z
" |3 {, W' W; G4 m; i% ?0 V- int main(int argc, char *argv[])' g/ x: {3 z% p0 Z5 u! K8 h
- {8 @9 z6 K& F, C0 L" j: R
- int i;
* i6 v t/ Y! } -
" Y7 Z+ ^& `, M7 x - if(argc!=2){ 4 t4 }# k% [: I
- cout << "This program requires one and only one argument.";- i/ \6 {, n! @ n: N0 ?3 h4 `
- return -1;4 {7 P+ a5 M1 d, X& _" N
- }
6 W$ w7 a$ M& p+ X0 a) `+ S - ' Z% B3 {. Y- Y: l1 j( f
- inf.open(argv[1], ios_base::in | ios_base::ate);
" P1 M% O$ g1 r$ u5 |7 p - if(!inf) {: z0 k' M: _3 i. H( @/ V
- cout << "Error opening file.";
; M i) i2 F1 b$ p8 q - return -1;
5 }, n( R+ x- R, R6 u- g - }
6 D* Q) s `6 g% N, a -
( M& |5 [0 c: w# o. P) ?# ~ - int streamLength = inf.tellg();8 D1 G8 Y, U2 [: r) k
- if(streamLength!=-1) {' V, O+ ]! O% v, U- G
- inf.seekg(0, ios::beg);. \$ S+ \! y1 X! E& P7 n+ f
- 2 o+ p: T3 q9 l: n! J
- buffer = new char [streamLength];
0 h7 x: p7 w- [# p& R( }7 h - // Read file;5 y4 G) k3 z Y |% h" i' L/ \
- for(i=0;i<streamLength;i++) {
8 f$ t7 `- y7 P! |: Y - inf.get(buffer[i]);6 O" ?( J( W" Y! w
- buffer[i] = tolower(buffer[i]);1 ?+ V9 J0 `+ {) v! y
- }- l, j: A% U+ p4 j3 t; ]
-
: ] h8 k4 q# ^) B4 Q - /* For testing6 B1 D0 e$ {/ i3 i1 L
- for(i=0;i<streamLength;i++) {
: S: C2 q9 @9 R5 j - cout << buffer[i];8 s4 K+ o$ n6 Q4 m! [4 r7 S
- }
3 i% P3 H3 a' k. K - */
/ R, D5 w( B2 h& ~1 A: R$ [" ^ - }
% g. E4 ?% I J: w5 d. p - else {* K3 s2 _1 c; q; n% ^
- cout << "Failed to read file." << endl;
8 D" c, r, @! [0 k& Z - return -1;
5 Z4 m [* X5 ^4 P+ }, V9 ] - }
! h% q" c$ ~0 x6 D+ M* L - " F2 k6 K; ^2 ^: k3 M
- string processStr (buffer);2 [3 A- C4 C; y, |0 }
- delete [] buffer;
0 u# V' O2 ]: c. i* d! P B Z -
5 }2 _! \2 U- A% B. A - // DUMB DUMB DUMB....$ q) O' F: G2 U: K& A
-
# q) i7 k" b$ u. M+ R1 m6 P6 m$ {2 N - int nextSearchPos = 0;; N% c5 x$ q( T, {
- int tmpSearchPos[2] = {0};
0 t# a# s; r; Z9 | - int entriesCount = 0;) L2 Z$ ?) i: M7 l
- 8 }, h$ a+ Q( L2 n5 E
-
0 x0 }* {# ? m" Z3 u5 v -
$ H- k# p7 v5 {1 l7 j% B - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
S* y* _( a: G; Z% f9 {) @ - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;5 ]" D6 c) a* Y, _5 U/ B
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
2 M% k- a9 F$ V O* t+ t5 J - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
+ z" i! p. S# r7 C- _ -
+ m0 }, j1 ^7 f' ] - if(tmpSearchPos[0]!=string::npos) {
5 e% h2 r9 k( q. ? - i=tmpSearchPos[0]+11;) m/ ~3 b! s7 b3 I8 t, I2 z) V2 l
- 6 ~- W. K% w+ f# ~- ?
- while(processStr[i]!='\n' && processStr[i]!='\r') {
: e6 o- _; [% W7 H$ U - cout << processStr[i];% b; X0 b( q0 S/ _" k
- i++;3 W: ^7 }1 a( ~& \4 m' d
- }
) f' b6 m: N! \" h: s1 v - ! p. W: o! r [5 h7 ]5 f* [
- cout << endl;. }: }% t' A4 U) j# Y
-
- K; ^ }, @7 d0 w6 `. Y - }
$ G( U1 s# {) b1 E: C -
* J5 A' V+ z% Y( k& U$ T& f5 g - if(tmpSearchPos[1]!=string::npos) {) h0 z' N( J/ Z3 y( l5 g% W
- i=tmpSearchPos[1]+13;
* D- r) a; {% r( p -
8 Q/ L4 v7 R9 ^8 ^% r" ] - while(processStr[i]!='\n' && processStr[i]!='\r') {
) w$ n8 a6 D! j5 u6 P - cout << processStr[i];
8 O! c* r( u6 n- R* | - i++;8 q! c# M# d- Z3 _& `; q
- }% |$ U6 K/ I+ ^( V2 P3 |
-
# M. K' e1 o, a H - cout << endl;2 `) L' I6 f, p! M' L; M: G3 m. O {) D
-
! A2 L9 C- f! W+ N - }1 Z7 R, t1 T N
- % m2 T* X' A1 r
- entriesCount++;
) j3 t4 u7 H# h - }
8 u) m8 X) Y8 z -
& T; E7 K# T; | - cout<<entriesCount;
! E% T6 {" s5 E) x% j - return 0;# Q9 |9 h* M i
-
?* u8 `3 O: s2 ` T - }
复制代码 Update: 最后一行输出条目数.
% m# i' |. z7 R4 ^, e8 f- h( R+ o. s* P
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|