- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis; Z* _5 e" x# x6 g* C; @" J) d
- ! h- I' H$ ^8 d
- //
/ Y; u. |& ]0 I9 Z, ~' J5 S) @, u - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
8 a6 f' m1 `8 d - // In case this is not legally possible,& y4 P$ @# K+ c5 _1 G6 B
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
# r( S2 I3 L3 S - //
1 }+ u7 X7 ]2 D
. U9 B: y' K: g) u3 [- #include <iostream>
" N; ?# [1 s e! B3 | - #include <fstream>0 u7 z6 `$ R; v0 I- C/ f0 Y
- #include <cctype>0 j7 O& J; K5 N# }9 h+ W/ }
- using namespace std;
) R8 q+ e9 p0 j- l - . [1 n' `& ^* b4 W$ L9 l3 [
- // MACROS X1 z- x: q2 F: A( v% \" A
- #define SYSTEM_BLOCK_STR "[system]"
# r/ Z' C/ f' x9 R5 @! @: U - #define NICKNAME_STR "nickname = "' w) V4 K8 T( ^8 I! \* P
- #define IDS_STR "strid_name = "
; w) z7 S- |3 c" S/ r" ? - //
+ H) T6 B) ?: z: w
! e* Z: w( p. l7 g; T$ A& h- ifstream inf;* W, f* C1 c3 ?
- char *buffer;
9 q- S! {" t; \& | - 7 \( r ?4 [3 C9 G6 \3 ], U4 U
- int main(int argc, char *argv[])& r B. k3 [6 }! X
- {$ p0 M9 o" `5 X4 R
- int i;
! j) T/ g, m6 q3 Q' j - ?$ Q6 o, J; z8 w4 r( Q- ?; [6 F V
- if(argc!=2){ 0 Y! e$ ^1 T/ l& A4 Y: ^
- cout << "This program requires one and only one argument.";
; a) S5 B9 G, ~4 R \ - return -1;% E% S- X9 {6 @. E
- }
/ o4 I0 N" T0 Q% S* \$ g - $ C# D+ M* W; K7 y4 h% O9 p7 u1 u
- inf.open(argv[1], ios_base::in | ios_base::ate);
# @- H Y- A) F - if(!inf) {
& ^* W: s/ s3 }. B5 q- K - cout << "Error opening file.";
6 b' l% b) c2 ]( | - return -1;
, |( J# n; B2 O5 @3 n: l$ F3 m* B - }: t2 G* t) U/ R+ m
-
2 A: H0 _: r/ e% v, q. e T" ` - int streamLength = inf.tellg();
- T0 G# F0 j" j; j. s" B3 V - if(streamLength!=-1) {: e2 i2 b( B( Z
- inf.seekg(0, ios::beg);2 |6 f0 c6 }, ^0 u0 m) k
-
8 s7 W' _' m: k - buffer = new char [streamLength];) {3 r, |! x$ J1 m
- // Read file;
6 w) U8 s- n0 G2 ? - for(i=0;i<streamLength;i++) {
& N9 G7 n) S8 C1 x# W4 R' O" y1 F - inf.get(buffer[i]);
) ?. ~4 F% q0 k2 Q9 H5 A1 l' p - buffer[i] = tolower(buffer[i]);6 E5 Q! _& P1 D5 `+ ]' c3 B0 e
- }
: J$ A1 Z' ~4 I -
0 q* T& l1 T5 x& F4 R" K - /* For testing& F, N4 W* ^: w0 \$ z3 q& E
- for(i=0;i<streamLength;i++) {# x% L0 F% r" L2 k3 h4 g
- cout << buffer[i];
# c6 W8 v: L4 I - }4 x7 r$ t% [3 V0 L1 q4 J
- */
0 t- a. p- K3 Q- ?( i1 a - }+ V8 U& t9 M1 }) L( v& \) G
- else {
1 A- T3 `2 |8 I' D9 [ - cout << "Failed to read file." << endl;0 G8 K" y. b3 H) d
- return -1;
2 \' }2 o9 ~& T! ^+ F( k7 ^! n - } @( U2 i) r* B, ~& W
-
9 T9 v s0 G1 e; f! X - string processStr (buffer); D h1 J1 Q# A( F
- delete [] buffer;+ n) z2 U7 V `* h/ ~- j
- 5 ]* O6 u4 }) J2 D* M6 C
- // DUMB DUMB DUMB....
. m2 `/ a* Y8 F8 }+ z; V - % r! J+ ~4 |! [+ T7 J) q3 M: n' n
- int nextSearchPos = 0;
/ S( l7 e/ r9 a* r - int tmpSearchPos[2] = {0};
8 h. e4 e }, ]' `0 X# s, J - int entriesCount = 0;
7 A j* i) z8 \2 Z, S8 W% x - ) }8 Z( {6 k* c) F
- ! S& _# c* I, h6 u: q) T. r
-
0 `( }- M; O) |. T9 L+ ~9 |9 w! y: s - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {2 y; u9 }( H+ N
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
( i4 ?5 R& l: \4 K) G5 a: s5 c - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
; `* D/ U; F3 U - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);4 |; l! m& x3 p6 j6 \
- 4 T6 p B/ [3 s% H s& z
- if(tmpSearchPos[0]!=string::npos) {
0 t. N2 v2 ~# ] - i=tmpSearchPos[0]+11;' t# l6 C$ {8 b. I& H8 g- x( k
- ; ^8 ?: }2 I0 D( D
- while(processStr[i]!='\n' && processStr[i]!='\r') {
( l4 L6 G, v/ V" [ - cout << processStr[i];2 V8 m% g% V# B" G# f4 ~. H$ E5 R4 y
- i++;
/ Y/ \3 R* |+ U4 u! T7 S - }
1 Q" l* ]1 c" q+ ]) E* l - 7 u* [5 ?* S5 A! s3 j! k' O
- cout << endl;- ^3 y. V* @: }) J/ G6 |
- 4 h/ [. n; `7 N! m$ X" R8 U
- }+ o/ [9 x8 E4 `! A0 i
- 3 {5 o8 a7 s/ r" P0 m0 ]
- if(tmpSearchPos[1]!=string::npos) {# G r- k; [$ S. ?6 j; @$ j
- i=tmpSearchPos[1]+13;, { E/ } N- W* S
- 1 {. i3 _! r9 f4 z1 ^
- while(processStr[i]!='\n' && processStr[i]!='\r') {
; \" |0 C% ]" B9 o; c B: } - cout << processStr[i];+ g9 n5 A$ r0 M3 }! ?
- i++;
7 _ y/ a* j' S/ M4 T2 ]! ^; k. l - }, R! _+ z. @* r# Q# p5 l* V
- 6 g! {$ A% |" Q; C% q2 \) b
- cout << endl;
2 b% x7 F0 w0 H -
3 H1 ^6 I# p" z5 K. {4 Y - }* R, Q$ z5 i. w4 D# S3 l( ~% x
- / v" p; i: V& |3 M- V, E# W
- entriesCount++;! T4 C" H, G( ^" u& e4 b0 Y
- }' k' B: C# v+ r& U( D$ _
- " j, q& h" Z- I% u1 Z2 R7 t- S
- cout<<entriesCount;0 m5 n' ]5 c" B
- return 0;8 e9 k% C5 W, n7 D0 X2 C
-
' G/ j6 a$ r" Q: \( n2 L% I& R) v - }
复制代码 Update: 最后一行输出条目数.9 v7 q; X B- t. \
: W. V/ W1 M$ K# O
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|