- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
: X& B+ P! T. }* p" ]0 p - : K$ P& F5 ]: N
- //4 O X) x. a/ b+ u/ L
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
2 K9 @0 I" _- L, S r% y - // In case this is not legally possible,' m' _3 `' ]& v
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
, {9 d( s6 y3 | x. w, s; b - //
1 A" ^; R7 j; _& r* P - 7 n# O! {. n0 x+ p
- #include <iostream>
' \8 H' V' y) y- o- g; R - #include <fstream>
% M* x9 m0 A! ~6 y8 n - #include <cctype>5 c9 N3 g* l" l4 b N' Z2 T/ q& h
- using namespace std;
: t/ U; x, C# j! `; h' T) Z
) K6 d+ X) H% o2 x- // MACROS
" P* j- {$ M: d8 G& ]1 T0 u% a5 D - #define SYSTEM_BLOCK_STR "[system]"; O, H- p4 y4 _4 j$ }4 X! k
- #define NICKNAME_STR "nickname = "
$ T3 a6 s1 m. C* z7 J7 I$ x - #define IDS_STR "strid_name = ": P) ] ?" \3 h8 ^6 P9 B
- //* s/ X R# ?; f+ U7 A1 Y" X3 P2 b
- % S3 s8 x3 l9 w, U+ K- \
- ifstream inf;3 A1 S6 j, [; W( `3 M6 n
- char *buffer;
8 @, ~7 D& b5 `2 e D$ x! n ]% S
e0 v# G1 y; D- int main(int argc, char *argv[])
& X0 P5 S0 L Y* _. J - {3 [8 @ k5 R2 C" a/ G
- int i;7 o8 _# R. z1 p6 E' u+ b
- % m) @8 @- l0 Y. X( h3 x/ J* L* [( l
- if(argc!=2){
% K1 }4 d7 P+ t* j - cout << "This program requires one and only one argument.";
4 X! v& j& W* o! H - return -1;
9 R/ i9 \4 {. k9 ?- M4 o - }: X5 c& h; ]( u5 V
- 9 r6 Q* s, v0 Q* \! `$ w% K5 l
- inf.open(argv[1], ios_base::in | ios_base::ate);
; e; K/ N$ l# u2 j- L* j - if(!inf) {1 B' T# P! g! E# S) ^0 ?
- cout << "Error opening file.";+ k5 N% D/ Z2 e" W4 V
- return -1;7 p# {8 _9 O3 h- H# b3 A4 g
- }9 X; j1 `! a6 V5 y. c& g
-
, Z! E) A0 ]2 k# V - int streamLength = inf.tellg(); _7 v+ |* w- l5 b! W/ n6 q
- if(streamLength!=-1) {/ I, F8 \6 f+ A1 s9 @7 g& P
- inf.seekg(0, ios::beg);
! o" J. i' l. V3 ~: h" q3 \ -
; K. b9 {) D6 Q1 J& A5 s - buffer = new char [streamLength];: J) t* }( B: j4 U* P2 h6 O( M$ H
- // Read file;$ }" J/ C# ~ h' ]
- for(i=0;i<streamLength;i++) {- \7 B8 n4 I& b+ u7 y
- inf.get(buffer[i]);
$ U# _( Y. n3 ?# P, ] - buffer[i] = tolower(buffer[i]);
3 g6 W1 b$ s) D, ]) Z2 o - }
$ F- @2 j, I; b4 }* T* C+ V -
3 X1 k3 E+ A+ ]% l6 K! n! w - /* For testing% [5 @- o9 x2 X1 R5 |' O
- for(i=0;i<streamLength;i++) {# j' R! l' A9 R# v0 Q! a. E K
- cout << buffer[i];) D5 N6 H& Z; V
- }/ j" k7 e. W" C$ i0 u& ?' T
- */5 j4 D5 |# K& a8 e2 \( O1 K- o9 `
- }$ M. q/ M+ `0 G" z
- else {" P+ \ P! n: Y5 o
- cout << "Failed to read file." << endl;
3 e0 Y% L. X7 r: _, x8 k - return -1;
$ b2 m9 M$ ~* A% C# x - }
- |8 |- _, ~( t4 _: q -
) ~2 T( t# d9 {" V- c - string processStr (buffer);" Q1 u6 P1 j. `$ I& S
- delete [] buffer;1 C% u7 Z4 X6 R7 }
- 2 M1 }/ p) q/ a* X3 m
- // DUMB DUMB DUMB....8 U2 y" j& Q& {9 H
- 7 R0 [& x# p4 D1 F0 R- V0 Z! I" F
- int nextSearchPos = 0;4 i4 [* u1 q+ H6 k- t
- int tmpSearchPos[2] = {0};
+ H% H+ a1 |& a a+ G2 I2 S1 ^ - int entriesCount = 0;
2 R8 m6 h3 f/ @9 p3 c0 q -
@8 `: m, ~& t -
, v9 U9 W' u @. S - + g1 ]; b& `, g' d
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
$ V! e9 {: f2 |9 l( L: n - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
4 h0 [7 A3 e% Z1 q* F1 Y - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);4 O2 r# `) p: E, ]- I2 i; Z
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);2 l7 s% U* \/ S" n, S# ~- ?' _
-
X0 T5 R7 a/ s- q, W - if(tmpSearchPos[0]!=string::npos) {% d: C/ \5 I2 J* C' q
- i=tmpSearchPos[0]+11;8 U9 Y* T# v% o5 f5 R9 M9 h( e
-
0 t8 d& Q3 `* | - while(processStr[i]!='\n' && processStr[i]!='\r') {
- e) a4 N4 `* ?% N+ y; B3 \- { - cout << processStr[i];( A1 X# h3 G! r1 L' b5 }' h3 f& w
- i++;# }' Y( U( m3 C# d, H: T
- }
/ s6 p2 X) V- Z0 s -
1 c! i$ \* t! r1 H - cout << endl;
5 }# i! y# `) j: z -
% a* \- D/ a; E; w: G# k - }2 I& F: {$ q. w* M7 ]( G; K1 y
- 6 Z# Q! r9 g8 e( `3 S% a, I* y _
- if(tmpSearchPos[1]!=string::npos) {6 `+ }6 I1 q4 \
- i=tmpSearchPos[1]+13;1 A9 Q' R1 N7 V* r% p
-
1 o) c) l( t& E' T/ Y - while(processStr[i]!='\n' && processStr[i]!='\r') {2 ] @' _ B8 K
- cout << processStr[i];; g2 q0 q& ~; b" i
- i++;* I$ U2 [7 u y r: C
- }0 g1 p' z& b" P8 U1 R9 ^, ?/ S( O
- % d$ A4 }) ^! b- Y0 ~' v6 G, h
- cout << endl;
* i f. w, _/ ] _' W - 5 V3 O+ |. w" z
- }
& N5 S% J J2 N - * G* p; G* L* Q6 L- M4 {9 y, j
- entriesCount++; N: _& f# E- J; |
- }
& c$ w/ F N8 k( g" b - 0 r3 P1 Z8 t0 m# e: i8 c% ~+ w
- cout<<entriesCount;
3 E& Z' o: M& q- } [. _ - return 0;
: d X3 [- p, C) w) `" d - : m8 q6 u% P3 [9 @7 s" L% a6 Z
- }
复制代码 Update: 最后一行输出条目数.
j+ l. |2 y# m+ V( Q2 Z) V- J5 A; C/ k
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|