- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
, G& r& L8 v# c! U. x1 S
0 R7 q5 ]7 J$ r) w" t* V- //
) l/ Y9 _; B$ T: Q1 ] - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
7 {; }) R, {+ J' Z9 h5 P - // In case this is not legally possible,
0 C( v7 t" x7 {$ L. g) Z - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.9 |7 u* r7 B0 r# a2 f9 Q3 _
- //
# _9 l. k* N' U/ n0 ^/ o
% t( U0 P5 w; {1 Z- #include <iostream>
( w7 I& P# [# h - #include <fstream>
2 b; }5 G( t4 O. i* U0 T7 F - #include <cctype>
6 ] @1 X( |4 U5 _* q& U - using namespace std;
' f3 H; m: x4 e8 F: E
5 y6 s9 n+ b" |' _' s- // MACROS s' v' |9 Z+ u) j0 w
- #define SYSTEM_BLOCK_STR "[system]"! m! S& P. l) N+ p" V4 c
- #define NICKNAME_STR "nickname = "" B& ~) I- k; p& U
- #define IDS_STR "strid_name = "
9 e4 L( v! l0 Y( @3 I7 c. K+ o - //6 Z/ I7 r( [" B4 z t
- 3 ^7 G- z, o4 D* F
- ifstream inf;
: z( ]; K) I9 [" w. e - char *buffer;
; [8 p7 \9 a; V( s+ c, ^7 A; Z J2 d - * l. c7 I. ?5 J0 o I# v
- int main(int argc, char *argv[]), u/ B. u% c6 U o3 @) @6 C |% M! n! k7 w+ F
- {
' x, Y3 l5 Q: i0 M3 G' }* w2 c - int i;: }+ m A- l; @! k% r
-
. W0 a) U* s& S8 E9 R& o. j$ `/ u - if(argc!=2){ ' F/ b: L: U# Q. d4 O1 |# d
- cout << "This program requires one and only one argument.";$ Q3 c" u0 p7 Y2 ?! A
- return -1;3 h6 j4 [, t( D" t
- }
r' o% i7 H- y% v - / f7 r, F8 N3 p* S) d. i9 a% n: h
- inf.open(argv[1], ios_base::in | ios_base::ate);
+ h r5 \: i1 z - if(!inf) {* k! ]4 ]3 w* p4 a/ H2 Z4 x2 P
- cout << "Error opening file.";/ q$ A) q) Y; c" V+ X* ] Y5 B g
- return -1;' K" R. E( B& |" T2 j _
- }
4 g l4 U/ D- x6 k- |/ l/ c" h - `, I# V$ p" P/ |0 t2 \, e
- int streamLength = inf.tellg();5 A; q1 r3 f/ @/ Z3 R- u; J
- if(streamLength!=-1) {
8 d W4 _4 A7 p+ e4 ?' j" w9 f/ k - inf.seekg(0, ios::beg); G! ]" j2 ` R! U$ m1 d
-
7 Q/ x2 ~. c' P" \ - buffer = new char [streamLength];6 W" P; @1 N: i7 ^9 M
- // Read file;" u5 h0 m4 u/ v5 b: Y
- for(i=0;i<streamLength;i++) {$ g7 H+ n: w& S& t3 l) T
- inf.get(buffer[i]);$ \- }4 [/ a7 ^8 W: K$ v) _
- buffer[i] = tolower(buffer[i]);
% A" ]* \' ^6 A+ ?" X - }& y" v+ L( y8 V5 c
-
- Z+ w4 B% ]5 ?/ n# b7 Q+ I$ C/ O - /* For testing. Z% I/ K# T- b0 S! }7 n% o
- for(i=0;i<streamLength;i++) {
6 V/ I* v! C- R, c- w - cout << buffer[i];
0 P+ I( W7 a- s8 k# Q- ~& p6 ` - }; O; g) S/ G# F; F
- */6 }7 O% q8 x* O" o2 L( o
- }
% d* w- T8 ~5 [8 D* c( k# B* } - else {2 k3 @7 _' w, ?/ H
- cout << "Failed to read file." << endl;6 b; O! {- ~# g( \# h; E* h( q
- return -1;
# ^' I' Q% L$ b# F7 ]4 N% R - }
1 j. {/ F; b A$ H2 i2 j -
2 t& F: w/ G9 t5 X+ B - string processStr (buffer);$ h2 B8 h+ t0 k. E9 D' u# @$ O6 `' |
- delete [] buffer;- g3 ^) j: N9 ~ G2 R+ v! ~
- 7 X. W) N P9 B) w) \. g
- // DUMB DUMB DUMB....
1 Y& @# X i( C6 w+ I - 8 T) l) Z! C6 l4 N8 P% X5 J
- int nextSearchPos = 0;$ a! F/ k4 v$ u+ p: A: ?8 v
- int tmpSearchPos[2] = {0};
9 V0 h0 Q: G! }; B8 e; }* x - int entriesCount = 0;
1 f) Y! M7 d) x$ Z; ^0 S0 K -
& ]8 d$ |. n9 c. Q2 E6 } -
4 F( s; i/ Z! y) U- @$ x -
~2 d, i& r: f" c - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {, z/ s% M6 y2 R" c5 Y" d- W# E2 A
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;* P' z! {9 @: V$ X4 n9 f" G
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);: x& J; n* Q" G4 l! P E, i7 D t5 ]
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);) X8 M; O* U* v* H: f! x. u
- 4 P ?- |/ |5 e* i7 u
- if(tmpSearchPos[0]!=string::npos) {
" R ?. B9 v3 j! L - i=tmpSearchPos[0]+11;
2 |7 U( w$ }, z2 ~ -
* M0 M( a3 W. G% U9 s& ]0 b - while(processStr[i]!='\n' && processStr[i]!='\r') {
& _3 @, T) V. ~6 Q9 J) ?* Z - cout << processStr[i];
0 m9 j/ B) r* o8 Q( O! U& Y+ N - i++;+ `& E; k5 a% F( B: ]+ z' O
- }+ w; y1 M' i+ i1 c
-
$ J- u0 P7 j2 x* U! X$ N - cout << endl;$ F& J/ k# T- w6 U
- - s% e+ g- V* b3 j/ P. Q/ C0 x8 ~( ~" g
- } G$ Q; |1 b$ b; u$ J) O' s
- " C: D# Y3 d+ A4 X; W7 o6 e8 \
- if(tmpSearchPos[1]!=string::npos) {
. O' ?2 i- b- r& W! J5 O - i=tmpSearchPos[1]+13;
# ^" X/ H) @! q+ s5 X) _ a -
3 M, ?: V* u* u1 h - while(processStr[i]!='\n' && processStr[i]!='\r') {( q& Z( A( o$ }+ p' @, D
- cout << processStr[i];# x% x3 w- N) x
- i++;& ^. d4 j8 D7 g: H1 I7 ]
- }2 @' t' y* ]) q) |
-
% M I) z9 m) F) w8 r, B - cout << endl;- {: ]5 Y) s2 a! Y0 Q0 a9 O
-
8 h( A% {, n) y0 }) f/ v. U - }
# G6 u9 h) k; p7 Z; F; F5 Q: X1 T -
2 D# L2 i) z y! Q. I) Y# Z( V - entriesCount++;9 \$ c( q, T3 V) @* ]' \2 I# W
- }
7 m) V5 @ Q( S6 Q9 F* M# ?* ] - 8 w( \1 |3 ]! j" {1 A
- cout<<entriesCount;/ g1 C" n$ h" j* V) F k
- return 0;
/ J: g- c9 z2 W& ~6 u' O- l - % P% Q1 T* W- j
- }
复制代码 Update: 最后一行输出条目数.
2 z1 i1 k, e: c: C% N* ~8 l
W& S9 L; a' H* Y* @7 A[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|