- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis% r- Q5 v# e( Y! P. ]$ y
a' `1 {: A: c+ p- //
4 r% t: }. f. K4 Y - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.3 S5 ?4 }6 j5 H+ p/ d S
- // In case this is not legally possible,
# P5 D1 W, g: `# {% ` - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
( P8 y) [% Y) I! \4 r - //" X1 {3 D {9 A. s* C
" E$ Q3 x% u2 B4 p, e0 f5 Q- #include <iostream>/ `& C. F1 A) J: q( u; E5 c0 Q
- #include <fstream>& r+ R( x4 C/ y
- #include <cctype>2 m2 ]. o6 e1 Q! ~
- using namespace std;
- Y b/ [; {* Y) ~- a( v
0 v+ y5 o& F* Y7 n2 S/ {- // MACROS
7 D$ Z# u7 g( A8 z9 B - #define SYSTEM_BLOCK_STR "[system]"0 L7 i: _ _* S
- #define NICKNAME_STR "nickname = "9 J* J+ v, f* r0 S
- #define IDS_STR "strid_name = "0 P4 M$ s& T& N5 s( s. D1 N4 _
- //$ x; o+ Z6 z0 g+ n) z4 J
# I0 z4 @5 z+ w" G( F- ifstream inf;. b. t: b& I+ d/ o2 H
- char *buffer;
& i0 p: t% i) u0 _3 o) V H
2 y/ V, A* f* f! y- int main(int argc, char *argv[])9 p8 y( k: K% g
- {. `# G$ u# T; ~' K; B; A
- int i;- F+ @2 d/ l! L! s! o
- ! U8 [( {; P4 ?- V
- if(argc!=2){ ' N9 h+ O9 n" T* O
- cout << "This program requires one and only one argument.";
. n* V! b' L, C0 z, s - return -1;
* S& e; B# k$ ?' k - }
2 m7 b3 G* O$ D) j; }: o( n* a -
7 J1 c7 U( L9 _$ A- @% M - inf.open(argv[1], ios_base::in | ios_base::ate);) T" @9 y3 Q9 ]- S8 a: r
- if(!inf) {
4 L& }5 o N, x' l9 Y1 O - cout << "Error opening file.";
+ O/ b( |2 x& I5 c% r" }: R: f - return -1;
7 ~" e- U5 `+ i* c - }0 {" J- q3 C8 z; g
-
% [, y5 O( j, R4 ~4 l; t - int streamLength = inf.tellg();
' M0 G7 l& h1 S2 | _ - if(streamLength!=-1) {
) Q) d4 c& f9 p8 G" A* \ - inf.seekg(0, ios::beg);
$ t7 k, y0 h3 k, j( ] -
/ B, _; M- E0 p) P! Y5 N - buffer = new char [streamLength];
! B. n' q3 K. X {4 n! ^. h" O - // Read file;
1 l; ~; y& p. [6 @- X' |! r3 ]+ X - for(i=0;i<streamLength;i++) {2 ~; _" E4 ?. d( G1 @
- inf.get(buffer[i]);
$ t% e' t9 t( W) U/ f - buffer[i] = tolower(buffer[i]); M' @; M: J+ a! t$ {2 ]
- }, S' ~! X2 e% T3 Y
-
M# m; I& T' m" q5 @5 x; K - /* For testing
$ N& p! Z5 F0 M - for(i=0;i<streamLength;i++) {& S( _& h4 f+ [* `7 O1 Q1 f% l
- cout << buffer[i];
9 W+ i+ w1 H) C' p - } _! {/ u9 D# L- I" g5 q$ M
- */
$ Z) R( J8 J( l$ c, s* d) T+ l2 }8 g - }/ P9 M, d; v1 I* Q7 J6 o3 a
- else {
: l# d/ ^% d$ f6 X - cout << "Failed to read file." << endl;5 \! q! J' G- l x
- return -1;
. E( q" C% Q |& W; ~0 t - }0 ]3 b7 s+ U ~! h/ o
-
: I( n2 ]# w6 Z9 ]2 _ - string processStr (buffer);
& _& t4 e$ M/ E" g - delete [] buffer;
5 h2 {+ V1 x1 N: g5 i -
( v8 K& E$ H; M9 p. S - // DUMB DUMB DUMB..../ N2 n6 f0 K' U% Q& b
-
! J! ]2 c' A ?2 E$ }6 K& F) ~: E - int nextSearchPos = 0;; q; J8 H9 w. K6 |. j9 c m
- int tmpSearchPos[2] = {0};, v% z& A2 a' n0 T. g
- int entriesCount = 0;4 k& C1 \$ v7 O
-
* d8 l. X! q' ` m/ f. q -
( ], l1 v% j9 H$ @ L" ~0 w -
( a r: R& ]" x; c9 g. F' D! F - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {, n# g L" t2 o0 A. g$ g
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
- T ^5 Q2 }. @% [, y: i - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
* w) c! i4 T) J3 W" r8 f - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);3 H' d! X" ~" t' E! v
-
* j% `8 u& Q6 {% |5 K - if(tmpSearchPos[0]!=string::npos) {
* i: v* R+ ^" A# M8 b3 } - i=tmpSearchPos[0]+11;4 W' n* `2 {; {3 l/ @2 H- i. a
- " [- }9 `0 I0 ~9 a' F
- while(processStr[i]!='\n' && processStr[i]!='\r') {
) E6 H* ^3 ]* Z - cout << processStr[i];0 L5 P- T* k9 w2 N# ]: r
- i++;" W3 C1 \) L( }. ?9 F
- }
. C4 q& K6 D. F; u7 ? -
( b, d7 {# R" W- d. j% F9 n( d - cout << endl;
h: o* y2 i; G: V -
2 u* F% A( n( R- @ - }
; Y! W0 o9 T- F3 j: s' v( C -
i0 G4 d$ Q r: X8 E# T9 E" n - if(tmpSearchPos[1]!=string::npos) {/ V( w7 d) K8 g0 q, h
- i=tmpSearchPos[1]+13;
2 P2 d" N. l# q6 Z -
, |: P j' {- d+ ] - while(processStr[i]!='\n' && processStr[i]!='\r') {
; d6 O5 s1 g2 Q7 X! _/ f - cout << processStr[i];$ T2 r" ` F1 Y* p' R# u' w
- i++;9 l: D( J% a! {) U3 N
- }
7 e/ S0 D$ Q3 k& U& ~" z$ x - , J; S- }; e \
- cout << endl;
* p5 a* e3 G* S3 N - 4 Y* ?, \1 k% g5 v, }9 I, q
- }
. y4 v; ~! l1 u7 D% [ - + ~- f( `( Z% x$ {' E1 K. T' E3 T
- entriesCount++;! c! k2 K0 w Z$ N3 o) T0 {2 C
- }# p8 j& X5 I- U. [
-
' Z% {; y9 h* E& R. L - cout<<entriesCount;
4 k* }0 q. i) O0 W }" r) C - return 0;
% N" g# {- n' T! _( f: B1 d+ Q -
: ^" T& U4 e; M- F - }
复制代码 Update: 最后一行输出条目数.
+ K* S! o1 \ `' b# X. \7 P( O* M6 z3 Y
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|