- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
# B' `/ f. F: ~6 c+ o f/ D - 6 ^4 R: x* |0 r7 h3 Y9 p; n! f
- //8 d, @0 W- U/ O
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.2 w% c, \8 `+ S! g2 t+ z9 E
- // In case this is not legally possible,
2 o8 [3 U/ B- ] I, ] - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
. S7 e+ x; @9 i: H/ L - //4 r" [5 v/ | T$ |
- ! Q( F0 U$ m) |
- #include <iostream>
! u& V9 C9 j% ~) h( z4 t5 K6 o$ h - #include <fstream>
( c o2 ~ u3 H$ H - #include <cctype>( Q5 D# M; }, N( o3 n# B
- using namespace std;
C% M# ]! b. T4 R `. W- M* q9 Y - ' g) Y# J1 G* N& h( y' G6 X
- // MACROS
; H1 F4 N) s* R" A - #define SYSTEM_BLOCK_STR "[system]"$ W3 }3 B w; F
- #define NICKNAME_STR "nickname = " V& \6 Z4 Z) w% w" E2 V
- #define IDS_STR "strid_name = "
: G1 r: y7 z3 y3 y - //- c7 Z6 l3 H6 S; @3 A
- - N+ U' O7 P, ~6 P
- ifstream inf;
( Z! w2 ~/ y) p7 f1 X - char *buffer;/ W) R8 c' d+ l: E) {! u
+ z. C5 F+ S4 x% @. `) q- int main(int argc, char *argv[])# l& P0 O& ~; ^9 k
- {
$ J% D1 C9 ^- q9 i. D0 X/ @; p - int i;
- q$ i* G$ I4 }" O! B& K - D, J3 q5 `" p3 Z: V
- if(argc!=2){
& _/ |5 m0 k' T6 E; Z- ]% ~ c$ w9 m - cout << "This program requires one and only one argument.";, q" y; X* w% }3 B4 E: E# X
- return -1;
3 a* i( ]& s5 N0 M$ V3 O" I5 m0 i - }
2 C! r' L. x! Z -
+ Y4 j2 w* e5 y" ? - inf.open(argv[1], ios_base::in | ios_base::ate);% i3 a/ j$ |4 @8 u
- if(!inf) {& J3 x. ^- ~* Z. X& b- ~. y8 o
- cout << "Error opening file.";
# Y! ^: J" V3 S! r1 q9 ]- E' U7 f5 S - return -1;; E3 v K# k0 r' t
- }
; g- Q+ K6 V# |. K; _ -
' a; B; N4 x$ N1 x/ d) @5 i( ? - int streamLength = inf.tellg();
2 C. a7 D6 o; Z: s' n3 Q - if(streamLength!=-1) {( y; \; J" u! _. V# {
- inf.seekg(0, ios::beg);1 U+ h' J' X- \. b* F* V" l
- , a7 {8 m$ c. {' ~( V
- buffer = new char [streamLength];
4 n2 v- q( M: J- o - // Read file;. S! m. J5 @2 l
- for(i=0;i<streamLength;i++) {
7 D8 {0 X+ |$ q - inf.get(buffer[i]);5 @0 N d: E* F9 o* z
- buffer[i] = tolower(buffer[i]);& S! l2 `+ @5 N5 O! C& q
- }
" _/ I9 d1 l6 V+ o - 2 ^( S6 Y1 E. ]
- /* For testing
7 g* p: p% q+ y# p - for(i=0;i<streamLength;i++) {3 N$ q1 ^5 n( G5 v4 A* O
- cout << buffer[i];
3 S+ t5 V3 B" s5 T# ~2 [4 k - }
8 ^; j- C: _. x( t1 D+ h" `9 ~ - */' M9 w }; R `) W
- }0 i% t: N4 T) `8 L j, {9 A7 ]
- else {6 M. @5 B* |0 |, j( E% M
- cout << "Failed to read file." << endl;
- d" K; F; o: L$ s- o4 n; L2 d - return -1;
' w ?) _0 h4 k# }- E5 a2 d+ E - }
) v/ {/ {0 Y/ w2 @5 E1 X6 P @ - ! } L1 f+ l+ F/ @
- string processStr (buffer);
! c* n4 n+ { _+ s - delete [] buffer;
% d4 [5 y5 v6 c" p3 |) E0 E; ~ -
+ c) K% ^8 O' k; j/ U - // DUMB DUMB DUMB....
[: I* [$ A+ {" v- m( r$ ^ -
j, u; h0 I C; d9 i$ o% ? - int nextSearchPos = 0;2 Z5 Y4 s& L1 S+ H
- int tmpSearchPos[2] = {0};
/ c% [3 W; j% `! g( d - int entriesCount = 0;
1 Y# M4 D. I7 ~1 I2 s; _5 { -
/ H0 X/ y; z( d - ( A+ d: r. R+ i3 z- O7 ?: W
- 1 Q) d$ y* C3 a& E( f, N# S+ W
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
1 M2 T. _; J% V - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
- V' U6 M0 |7 j; l9 K - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);, b8 u# A( L5 [5 _4 }% t2 _
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
# Q2 d. F$ @2 X, T/ K& r - 9 ?% m% ~5 Y0 \+ l: k
- if(tmpSearchPos[0]!=string::npos) {
* _" A% B/ J, L( v! A - i=tmpSearchPos[0]+11;+ |2 J% I8 d5 W9 ^3 G
- ' R$ E2 ^% h, F; x# Q# ^
- while(processStr[i]!='\n' && processStr[i]!='\r') {
2 E0 X5 c; `1 T! R7 i3 h - cout << processStr[i];
* L6 y* j0 k5 G( N" b - i++;
( A# Q* c$ b% u, g9 v8 [6 h: S - }
+ F6 L' i2 }: k* w+ h" d% K' S -
' U, H7 G% b9 M1 ] - cout << endl;
; B$ G( V4 W' e2 u/ ?1 i' t# X -
& ^ s& E w" C' S - }" Z# Y# ^- B% W# _: E7 J3 V8 E
-
( v& k% P8 I3 o" Y9 ] - if(tmpSearchPos[1]!=string::npos) {! G; h/ j" x+ z! `8 `4 _0 U
- i=tmpSearchPos[1]+13;
& s+ S" X- x- X& P7 f2 M1 } - ( s2 ?7 U0 X2 O% O. R! r
- while(processStr[i]!='\n' && processStr[i]!='\r') {9 D$ f) B' ?) E& l
- cout << processStr[i];
) |7 z" S! y6 d" j7 q - i++;
! Z. S8 h4 Z$ ~, k& b. a3 y( Z3 m - }. V, P$ }# \! a
- ; w7 z& Y% ?2 f, T2 w c. e4 M
- cout << endl;
. ~) X4 i1 m! Z8 J0 C - 3 Y- H+ `$ |/ R) D
- }+ E( |0 j3 N6 d3 b0 d
- # Y5 w7 D( F$ t6 O# W
- entriesCount++;5 S$ ~$ b# A+ E$ S; G% d
- }
/ _+ [* s7 @: X) A - 2 k* [& _- [ J b
- cout<<entriesCount;
+ K$ F$ m: y8 P7 C; l - return 0;
$ N# Q* g' ~, M/ Y' t - , t4 z0 r0 } L2 u9 p. e
- }
复制代码 Update: 最后一行输出条目数.% k' F4 F, t- B5 z
" D1 R, c# _' B5 \- A
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|