- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
) {8 W3 i& K8 P) e" `: Z9 R
9 ]! b/ M0 U- W# @3 h- //* l6 k4 M9 ^5 Q, O
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
3 Q) t& S2 j! G7 d% w5 v; M- o# \ - // In case this is not legally possible,5 b: h4 [: T$ G- R$ k0 A
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.7 h1 B6 L. d! Q
- //# \, y) o. H9 z, h+ o& n. L1 [8 ^$ p
! G( _8 U& [! _9 C1 Q( X \! H2 Y- #include <iostream>; m' s; E! b+ L$ g/ Y+ b- I
- #include <fstream>6 X0 }& d s0 e Y! d
- #include <cctype>6 y. P; k; t' G. w3 X/ f$ G
- using namespace std;
: Z1 u8 ]; d/ m% n; J9 y- w2 ~& ~ - 4 W# X; A+ l1 _: q
- // MACROS9 Q4 G6 M1 z( z* d3 R9 B
- #define SYSTEM_BLOCK_STR "[system]"
2 D R2 W6 E9 l5 p; c0 k - #define NICKNAME_STR "nickname = "6 D( M! k( R! [8 K7 ~" C
- #define IDS_STR "strid_name = "% a- Z& R# x) c
- //
: m4 Q O8 |. u0 w4 S
& a- o( p" n% P. R, M- ifstream inf;
; @6 u2 I" D5 n$ j+ _ - char *buffer;. _0 J. D; d3 A2 Q8 s3 \4 P1 @. @
# q5 b- Z( k" b- int main(int argc, char *argv[])
6 i6 X+ n* B! ~, o3 ], h - {
9 n% q9 Z- V) m1 n7 l& O$ Y1 r - int i;" c) X$ i5 f/ ?! l5 X* E, G0 [' k
- 7 [9 [% y5 e# v& V! N; C
- if(argc!=2){
" H l4 a& _/ b7 g - cout << "This program requires one and only one argument.";
( w7 O$ ]4 C0 `7 J - return -1;$ R! o5 ?) z! z
- }8 J9 C7 X0 c$ ~
- _/ t$ r9 i, O6 I8 |) A
- inf.open(argv[1], ios_base::in | ios_base::ate);/ I( w9 C f2 T1 m0 d& |/ ?
- if(!inf) {0 R1 J* F2 p5 | b0 I: ~
- cout << "Error opening file.";! W( Q9 X5 z8 q* z/ ?. i, r
- return -1;8 s# k6 _- | O I
- }
# ?, e# ^9 U+ M$ `1 E -
4 k8 C9 V ^0 j3 m - int streamLength = inf.tellg();. J" w3 W$ w4 A( u' q5 i# `8 z
- if(streamLength!=-1) {, l* C! O: v/ Y" \4 `& V3 ` t
- inf.seekg(0, ios::beg);
2 `1 ]6 q/ z/ U L7 O6 s - $ C& a2 y2 @( n) t. ^0 Y
- buffer = new char [streamLength];
% H3 l8 q, V' w$ s. d, I - // Read file;! r% a5 X/ a1 S( C5 T) W9 X
- for(i=0;i<streamLength;i++) {
. W' D. p: N3 w v - inf.get(buffer[i]);- e' s. n; Q# W& F9 I9 y* Z* `
- buffer[i] = tolower(buffer[i]);
. {- ?7 J; R \- b; s5 a$ Y. y - }" M' y" D9 u3 P0 b7 ^5 L
-
( j) W3 {" o: J5 `! a - /* For testing( }% g9 H( ~% L; X7 b+ O z, w% g
- for(i=0;i<streamLength;i++) {
7 ?5 I! s) ^: h$ \ - cout << buffer[i];+ e; O# [# D9 J, ^4 |/ K
- }
3 ]* a# { ]7 m. t; r# N q: H - */
[% ~, T# t: @, ` - }
# d. x- Z( P l% m - else {; J2 x/ A, w3 D6 ~
- cout << "Failed to read file." << endl;8 n( `. ]: m, I8 M) `! m& _
- return -1;! r7 p; ?3 l4 {+ H) r4 a) F. H
- }! Z) o. A5 a6 W+ ]( D f9 g$ H
-
s$ [) [2 X' h2 F1 r8 M. O( s% e - string processStr (buffer);: v8 E) R9 n7 U( b5 n
- delete [] buffer;/ g6 X* @+ M6 {& W' [
-
% H, J, K. j* y2 Y1 C Z: w( } - // DUMB DUMB DUMB....
0 ]/ ~: s5 Y! P9 y -
F+ `3 ~/ N- m# |5 T - int nextSearchPos = 0;9 P, N/ C3 f0 h# ]; G# q
- int tmpSearchPos[2] = {0};6 M+ N' G2 o+ {- `$ p' p$ C0 l
- int entriesCount = 0;1 |) i% u' g. \. y( y( l
- & D7 g0 r$ w: }, g3 p" h
-
& i+ R0 \. Y/ `# H8 B - # q# t5 x$ k, C
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {" Y- a3 X! |) ~; V
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;- P7 n c8 N+ k4 H
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
+ Q* s& L, b* s7 Z. A - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);0 v1 g% J6 }5 P3 P) N5 ^" f
-
5 g" ?4 w5 T) u! h, x5 j$ t6 v - if(tmpSearchPos[0]!=string::npos) {
* F- @% r1 @* i G - i=tmpSearchPos[0]+11;
1 f8 U I( P( E- e* ~ -
1 e1 k9 O5 _! X( J. y# @ ~% } - while(processStr[i]!='\n' && processStr[i]!='\r') {: u( X& j4 t" X
- cout << processStr[i];
1 {* U1 s, b7 p4 |6 z+ {/ B - i++;2 w, f9 W! ~0 E! r( w5 \: S
- }
- q2 }$ j$ ^8 N$ v3 c$ t3 z% w -
' P# V/ v F2 \: m! V - cout << endl;
# G9 Q( j: k) t: s, ?. K6 P. g% ] - * x9 {/ b( U5 {' {: ~ k8 B e
- }: |7 \" a, `/ B* Y K' N
-
7 @( c8 t/ Y# c8 J; n - if(tmpSearchPos[1]!=string::npos) {
: Z( k4 @* k& t/ W3 t0 V - i=tmpSearchPos[1]+13;
+ K; U- ?8 ^6 g0 |! g5 W - : b: _& S) ?6 b* Z- w, q1 @6 z
- while(processStr[i]!='\n' && processStr[i]!='\r') {
% J, o% H2 e# _/ q8 | - cout << processStr[i];, }4 @' E9 b! }- N- ]/ x. P2 X
- i++;
4 o2 o5 S/ `3 v - }: r& @3 f1 }) J9 i- i
- * J1 m1 |" V. H0 v8 A# E: y
- cout << endl;
: ^* H! U( M8 Q6 { -
/ o8 Z; o: d2 Y0 r - }
, | B3 B# [/ G, c$ n: l -
' w- N& E+ H$ T; h Q - entriesCount++;
* m/ [8 U; Q& q$ n$ h - }& n7 f$ h( I; Q$ m: Y
- % L2 }! l6 V9 K1 C' r! W
- cout<<entriesCount;$ r- b7 E$ T" ~4 F R& m7 r
- return 0;6 v' O( l" Y6 E, N3 s/ W) P
- 7 H; G* W) M h( ]$ s4 s
- }
复制代码 Update: 最后一行输出条目数.
* t4 {% h% A- M k- e
# R9 T6 G! b8 v- W/ U9 |9 m2 y[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|