- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
8 F ]2 Y P b! Y0 N- y - ; f( d( e9 _8 v% B+ W2 g( N1 D& P
- //4 m6 y3 y+ f" J/ w) o! p1 L/ ^* @
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
0 K! V7 H2 W) s( Z; { - // In case this is not legally possible,
0 s8 h& ?3 W) Z% b! {! j - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.5 J8 A/ {; ]4 D' r7 u
- // T; x; F7 z1 g1 S
: s8 q8 j5 x+ ^* \1 I1 d1 c, {4 r- #include <iostream>
) s) o5 v0 I" s: ]! ^" i* _- c9 U - #include <fstream>- |: m+ Q! a0 i; T0 [' r
- #include <cctype>1 P. h2 f/ _3 }5 {
- using namespace std;, l: h6 t+ `% P( p9 X/ }$ q
+ P) {6 c. Q3 E' Y9 v8 k3 h: o- // MACROS3 K2 E) Y9 H& y" ^' w6 o
- #define SYSTEM_BLOCK_STR "[system]"8 S! {$ A; J3 B/ T% F3 [
- #define NICKNAME_STR "nickname = "
- \; z& k6 I( d& Z' O3 T - #define IDS_STR "strid_name = "/ f. w/ F( ^9 c2 L
- //
7 @7 H# s7 \8 H4 }
' d; V0 n6 [. r; e6 b; T, q- ifstream inf;
; m9 T9 \4 Z6 ^8 k- r$ I - char *buffer;
* ^' M8 J/ a) s5 [- n! u) S
0 F1 F- T7 ?4 }- int main(int argc, char *argv[])
0 H5 s q5 B0 U7 |/ X/ R' S - {: n9 q! j. L3 k8 s" R' Z, B) S5 l
- int i;
% ~7 a. {6 q% C - 0 P$ K) n* z0 u l9 `0 K9 r. D
- if(argc!=2){
, s" E- i; }6 \: ]& i; R' t - cout << "This program requires one and only one argument.";
; h1 z: S$ k- L4 `6 z0 _ - return -1;5 ~% d8 C ~ x9 V# R
- }: ^7 L* @6 F3 n1 {4 O4 Y
- ; q" A, b' {3 J7 p- f; w( h( g
- inf.open(argv[1], ios_base::in | ios_base::ate);
4 P4 P. M G8 X0 @8 h - if(!inf) {
0 j( }! [- ]- `& Y - cout << "Error opening file.";& U8 m" ?5 y" F4 J; N' L H W
- return -1;- v, x T$ L+ ~ O2 L
- }) n a' m# G1 w8 }/ j
- ) }3 |4 o O7 R; Q
- int streamLength = inf.tellg();( v) L/ l5 N: K$ O1 g" b
- if(streamLength!=-1) {
2 x) y; E1 O$ I, D - inf.seekg(0, ios::beg);
+ `% @" U3 {3 e+ f# w* j -
, E3 q* e# Y6 J) j% s7 s* \ - buffer = new char [streamLength];* L; m) y t. Z: |/ P$ C. F, l
- // Read file;
' }* T: J7 n: z3 A+ u1 j7 b - for(i=0;i<streamLength;i++) {! ~1 P' L6 H3 A. m. Z
- inf.get(buffer[i]);
" |8 c, i0 w$ q, g - buffer[i] = tolower(buffer[i]);
3 {* B$ A5 P% s$ h) J5 C. c - }
! v4 P L$ x( q, { - 2 K: E! U7 v4 g/ a
- /* For testing- R3 k) s4 P$ R5 y1 i
- for(i=0;i<streamLength;i++) {& p# D/ u# k% H/ C, c5 ^% ]
- cout << buffer[i];& r2 @0 p2 C( e+ c
- }
/ u" D9 P9 J/ {! n - */' r5 h0 v- ]6 d) C# _9 U
- }0 ]- f1 o# F: L
- else {" I* m, L3 E" Y
- cout << "Failed to read file." << endl;( t; Q4 ?$ e& F4 t8 S/ Z
- return -1;
2 t( @2 R4 m" M1 t: C* o, } - }$ P" _- S$ P9 h. r' u
- - D0 X+ `: E6 d# U+ l
- string processStr (buffer);, K" p5 e+ x" b9 M# G) d4 B- b
- delete [] buffer;. g. i" G) s; [8 ~, I! o* p
-
; q! t/ M* \( i1 X* E/ `- p - // DUMB DUMB DUMB....
/ B: ?8 e, x( _4 K P -
a) J7 l( b$ }# H- X" x& s. N - int nextSearchPos = 0;( f" I! Z. s% L: `4 {& e
- int tmpSearchPos[2] = {0};3 c+ w* i5 U) y% N3 [2 y0 [
- int entriesCount = 0;( T5 T3 l6 ]/ @+ U
- ( Y9 o P8 n! e& J. T
-
* F& e8 L2 P( q( w/ m - & H2 G- E/ v( W/ a3 k0 ^# k3 p# e! Q5 p
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
( f* ~0 ~% x$ O, | - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;( {# r) }9 c# [. H( d
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);# m, H# m5 m$ I9 r! a$ A
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
1 `, M' K( F2 V, [+ ~ - e4 {) R8 w' v; M
- if(tmpSearchPos[0]!=string::npos) {' N9 u; n9 @) c- s
- i=tmpSearchPos[0]+11;
4 w' I$ ~* u4 n; d4 o -
* k' R& X" b$ j% c - while(processStr[i]!='\n' && processStr[i]!='\r') {
. E$ b: ^2 N8 z- p - cout << processStr[i];$ m. L8 c- l; ~* F7 V
- i++;" q7 \: D" w2 N4 N2 S5 B, D* h
- }
* U, v* m# o' p1 G% c; N - " |& N2 B9 F4 E: L! i) y* a
- cout << endl;
" f# v, ?. _ A- I$ A# L -
( B& T$ e) w/ j1 ?0 `$ ^# z2 ? - }
) c1 l; H, O5 V4 a/ t# Z -
4 ]. D7 C/ {$ d* m* x - if(tmpSearchPos[1]!=string::npos) {4 Q7 _) }- Z3 g6 v% J
- i=tmpSearchPos[1]+13;9 P* Y+ g. l) ^
- ; s: x4 g% r" |
- while(processStr[i]!='\n' && processStr[i]!='\r') { I* g& U% C6 y/ v- d; X! v
- cout << processStr[i];. [3 a9 n! \, F0 l# s. c# g
- i++;5 E7 |8 z* Q9 m U {' q; p2 b
- }# ?2 D; G- ]! j, m* }/ e4 s* W
- 6 I$ u2 O1 y" B$ m$ H( a# k
- cout << endl;
& q4 F6 Y2 P* v: o% ^! l - - \; L! o5 S1 i0 p; X
- }
, N4 k, S6 v/ W7 H) N d- G - ) i; p5 G( ^5 V& |! g( Z
- entriesCount++;1 C% u2 n) r! J
- }
$ f" x0 m g; f' @ - + N2 l" x ]1 s( Q' ?
- cout<<entriesCount;. @6 \/ [% \! J5 ^: b" i
- return 0;
2 s3 a4 Z0 }3 f4 q: S; o! w -
[/ x$ X4 u& M) w0 m - }
复制代码 Update: 最后一行输出条目数.
0 x: H4 T2 \/ q5 a2 @9 L% @8 Y: c6 j
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|