- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
: a- E& I1 p8 ?- g9 D
, f: @5 ^/ \3 k* E! o' b2 y' k/ {- //. f6 s6 T2 E1 s4 f; R
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.9 M9 \+ q1 ]; E {' u; f3 g. u
- // In case this is not legally possible,
+ F3 Z4 R6 [1 U6 ]+ e% S. [ - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.: d4 g& G* X1 V
- //
! q/ o- B9 i8 o; \' Z - ! y/ c |! v. w
- #include <iostream>( K* B7 N' B) x. R
- #include <fstream>
! q q2 B H) x+ V& W - #include <cctype> O' M( n J0 y6 V2 o o7 }
- using namespace std;
$ n2 p4 j( D# c( p9 D: N+ s& x
! r. ]6 n w8 z0 z3 D- // MACROS
+ D/ t2 t; q; _# S, X - #define SYSTEM_BLOCK_STR "[system]"
- V" d1 z+ ^+ B5 {9 e4 f% w - #define NICKNAME_STR "nickname = "4 u" H, s) @5 F/ h
- #define IDS_STR "strid_name = "
" j2 E0 z, }1 d - //
3 I( a. J ~' n5 ? d
) S, f! p% |& A/ A- ifstream inf;
9 E4 E5 x% W( S( D2 ~7 { - char *buffer;
! W* R) |& {8 K; k - ) b4 C) f1 Q0 Y* s, q
- int main(int argc, char *argv[])6 R5 J: q$ g V; i' i
- {0 O' f4 \7 U+ @0 {+ G% E0 X
- int i;& B/ t7 q f& C2 ]
- / k1 K, {; @: c$ n& r6 a& b
- if(argc!=2){
F+ Q( Y! w0 C" R* X K - cout << "This program requires one and only one argument.";. V% ?6 P2 J p& u% P! g
- return -1;
; W9 }9 F& `6 w% B8 a4 n - }+ W- `& `, m- C$ b0 `* k w
- 6 r; z3 D' Z* b& s- ] T
- inf.open(argv[1], ios_base::in | ios_base::ate);
X3 L8 F/ F S$ W' ^# e5 d! a% [4 o - if(!inf) {
' h& l5 v1 N3 k- _" O0 o" q0 H2 Q - cout << "Error opening file.";
4 o0 P) |- o# N) z& y0 B0 z l7 V - return -1;: ^2 l1 `/ c2 u: h; o! O
- }
/ Q3 X& a9 d( @7 { - 2 Y; }2 T5 h: C
- int streamLength = inf.tellg();
+ _1 z/ C# J, X# [, T& e e - if(streamLength!=-1) {
7 m' t( t2 B3 W5 M+ q! C& ~0 Y - inf.seekg(0, ios::beg);, T' t2 |5 s& P6 ]. T! K/ F
-
! U. n0 C8 j1 k0 C5 @/ u - buffer = new char [streamLength];
% \* Z- O1 Z7 Y; @ - // Read file;
4 f' v$ g8 ^6 W' [2 q - for(i=0;i<streamLength;i++) {
3 C( Y; }( q+ E: Q6 U% L - inf.get(buffer[i]);/ t4 x1 C5 [0 ]' D/ G
- buffer[i] = tolower(buffer[i]);! J C. `0 j. }7 C/ F
- }
! x4 J- y: j m5 ]6 v - ' h+ o! }* ^2 @! _% g1 U3 A
- /* For testing( n- L! P$ \$ W& r8 x! m+ F0 o
- for(i=0;i<streamLength;i++) {
; H; C* S, y9 E, d/ q8 w - cout << buffer[i];
2 V' S- v+ W& Z! t: h4 v9 o - }5 F$ V& W7 a' y5 N; H
- */
' N& q6 D: H2 { - }
: p" K; j* H0 w+ g6 a- L - else { w6 w0 X5 ^' L
- cout << "Failed to read file." << endl;
/ O( v; G7 ^) A% U9 ?. D2 c; G - return -1;) t3 R& m' E, Z% ]% ]
- }, v1 z* y7 r& v) F2 \
-
4 N) b$ M: [1 l4 l4 S& } - string processStr (buffer);
' C5 r4 A: w$ o& c. j* i8 ~- U - delete [] buffer;
% g( m* L( m- }: w; m E: Y - : O$ b- [$ a6 O
- // DUMB DUMB DUMB....
! B5 Z2 g0 ]! m5 X, a+ X. X3 q - / S' B/ ?3 M9 ^; t1 ]
- int nextSearchPos = 0;4 i! x; [/ a0 }4 o
- int tmpSearchPos[2] = {0};; D8 g. {8 }9 |; S3 Y# r
- int entriesCount = 0;, e4 `; J& j2 N, n0 K; G$ F
-
0 C& O7 T; V% u' W V7 s -
+ {0 @% ?5 t, z6 J* _; t -
; N }/ B' I! d - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
5 m* T+ W- e6 A+ E% l6 C A - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;. A: O3 U- B! R6 N% t6 y
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);8 R3 H# {2 \# p4 u! Z$ e4 h1 `
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);# p, K' K$ l; p% c4 j; r
- : H0 W$ m" K" f
- if(tmpSearchPos[0]!=string::npos) {
Y. P! ] q* M8 ?1 u, c - i=tmpSearchPos[0]+11;8 p% _8 c* W7 b/ r( h2 B" p2 m1 q# s
- 7 n) C* d" s, q: _% ?
- while(processStr[i]!='\n' && processStr[i]!='\r') {
# ]/ N4 P: l6 v" |: M& x - cout << processStr[i];+ @' x4 m; @8 C5 j
- i++;
0 j( m- y, s7 [' O* X t - }( d6 r) b3 k# }+ m; ^
- . j2 I0 X v( |$ m2 T
- cout << endl;
5 Z$ @1 P2 P2 H* Q$ T -
$ A ]) b w2 f6 R- T+ ` - } s2 P% L8 |( n/ M8 H
-
' ]3 {# S" T* ? - if(tmpSearchPos[1]!=string::npos) {6 j* Z+ B& S w
- i=tmpSearchPos[1]+13;
: q' }' `% j$ L: a - 1 H: D. r+ A7 ~5 k% k5 @
- while(processStr[i]!='\n' && processStr[i]!='\r') {
2 H. ^2 t6 T7 ]& N8 v - cout << processStr[i];1 F# ?) }! R; W4 q, o$ f4 f
- i++;
+ q: o: M- D. K, e0 W. ~" O - }
b/ e9 [0 L9 I" N4 U - 5 c1 F- v7 w: P' ^
- cout << endl;
1 a" G1 [: k& u! {2 N* ?" L& A -
# r% ~/ c9 D2 V - }
( h3 B+ [4 F# T7 q, _1 J5 t9 x -
# ~2 f2 p( Y- p: `% Q: Z% e% L - entriesCount++;. x: o, T' v. n) r
- }
3 K9 u, n# P! w" q2 Z" J -
c( a$ l& M8 n$ t - cout<<entriesCount;" q- w* i. j0 n* D2 Q2 S
- return 0;
9 o" V, [9 S# q n4 y6 F -
3 q2 O/ K9 U1 n0 B( g - }
复制代码 Update: 最后一行输出条目数.- S+ o6 w, P/ z8 D: D/ v1 E
J, _8 Z9 Y2 n! c- v
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|