- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis. [6 V0 Z) S. G$ F
- ) ]1 f. S# Q S
- //. r( z' p6 d8 a$ G% C
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.* [ P+ p3 _6 U5 [
- // In case this is not legally possible,7 g9 X. Z7 w6 B8 s" n4 b0 t0 d
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.' Q6 y2 T+ `: Q8 s. @0 e$ w
- //7 ~3 C& k+ d: ?( E m, j
7 N- @# m2 S0 c- N: E- #include <iostream>5 e0 O5 S$ ?7 g2 n0 b1 w+ B
- #include <fstream>' I5 U5 G, @# c0 I+ ^2 J
- #include <cctype>* r: W! B {, H- v! o) V
- using namespace std; w& [7 P" v6 g0 G
- ' d5 D% ~# B7 `* ]+ z" y
- // MACROS. X1 d" h8 P9 ]( ]+ q B8 w" p
- #define SYSTEM_BLOCK_STR "[system]"
8 O" j5 o# P/ I - #define NICKNAME_STR "nickname = "
0 j3 i' _1 ?& v' e/ \# y; ?: n - #define IDS_STR "strid_name = "; V; l1 Y6 j7 K7 e1 e8 `
- //
9 `0 ?0 e; l8 P; S* i9 n4 K
/ j* J# ?7 t, v" ?: V w/ X) _- ifstream inf;6 _- ~0 P! K1 Y" H2 s0 c+ J
- char *buffer;# [" _3 }, d" k2 Z. v, k" S
- 2 e' g2 C9 e" l
- int main(int argc, char *argv[])
3 C" |% z7 ~( W( P3 L7 K$ o - {% e' U/ }- x; o' d& Z
- int i;
( [. v* O" D- \) m, o - 4 U4 ^' s& I$ P8 H5 N
- if(argc!=2){
) P$ f- d8 T5 ?( Z$ c/ d& @ - cout << "This program requires one and only one argument.";* X9 Z! r: d% @8 e" M
- return -1;( C& v" T( c+ L9 o- o" U7 z @
- }
' b8 D$ L6 q; |* X - # x; ~- y4 f1 R- j8 z
- inf.open(argv[1], ios_base::in | ios_base::ate);
% K6 C. f3 i' J1 G - if(!inf) {3 R7 ~2 j0 m( e6 z% Z: A [5 Q/ C$ C
- cout << "Error opening file.";
; C1 T& l9 i1 g/ ` - return -1;' h) G6 M5 i; J) u) |& k9 {, E
- }
# Z% T' H" {- @) |/ B6 v -
% p2 w6 E3 j; [ - int streamLength = inf.tellg();6 ~2 z3 y+ w. o/ ^
- if(streamLength!=-1) {
; } e' W$ x; e, m - inf.seekg(0, ios::beg);
3 i0 L3 H. p8 { - 6 O# x5 }/ u+ c$ c. g* U
- buffer = new char [streamLength];8 g! u. S7 q$ s. I6 y( x/ t, h2 {3 ^9 m/ z$ l
- // Read file;* z$ d2 l( p4 ~3 N) F
- for(i=0;i<streamLength;i++) {
9 A8 W6 W9 W+ S7 a$ v - inf.get(buffer[i]);
& c# z# s. p0 _ - buffer[i] = tolower(buffer[i]);7 [0 V. @" V, G
- }
5 b( F8 Y6 s& o3 t0 ]6 s -
% _$ |# }" S' }% } - /* For testing- |4 t5 b1 j5 z! g: w6 S
- for(i=0;i<streamLength;i++) {9 T7 c4 B2 z- i* d S) D
- cout << buffer[i];. J# v7 Q! Y% Q5 I
- }
7 J& B0 h! g/ _5 {( m% J - */
# E* b. g u7 S - }6 T" W) g& q% j2 b
- else {' O; B+ c' h1 _0 F5 G# ]( h5 M
- cout << "Failed to read file." << endl;
& Y) t( `' v8 Q) O. ~+ w1 O - return -1;
1 a' `$ |! [( W, e; L0 @ - }
I2 X) T$ f, _6 Q -
7 q( s5 b' z9 Z6 ?: K - string processStr (buffer);
8 b' @4 J$ R6 n* o1 n) I3 g7 V+ d2 W - delete [] buffer;
$ }9 N w4 u; R- b3 P* C - |8 A( o8 k V5 b# X
- // DUMB DUMB DUMB....2 J |' t7 T; G" a! D# p: ?
- 4 m7 q7 L: ?, y2 i5 g; g1 r7 s
- int nextSearchPos = 0;2 }, c. G ]; \0 l
- int tmpSearchPos[2] = {0};+ e# \0 Q% t: d+ m0 E2 |/ F
- int entriesCount = 0;
9 u0 Y1 Y, q& J9 U$ I- f - ; N- v7 T$ m/ m! a" W; w
- # X+ k( t8 F: x: g
-
- N. j* [6 z: p& b! h - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {$ ^5 N3 D/ Q5 l% M e
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;( p: C4 m x$ l" X" x
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
# S, c" K2 ?, q& j - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
2 T5 o* ?+ N7 b Z6 v9 M) E - + x% V9 H& R5 ^4 `
- if(tmpSearchPos[0]!=string::npos) {; E- b6 ^# I0 p0 I9 g1 K
- i=tmpSearchPos[0]+11;# r2 ]& j4 G1 M# N/ }/ E( ^. n, [
-
$ Q1 e3 I4 w; q7 d1 Z0 @ - while(processStr[i]!='\n' && processStr[i]!='\r') {; V+ l) B* I+ T" ?9 I0 ^
- cout << processStr[i];
0 X+ M% c- l9 i - i++;
& z0 K) x/ ?) p& _4 i0 Q& b: q - }
1 Y) d3 k& _- e& w+ m8 Y3 k -
; D- @7 M/ ^) E - cout << endl;
8 x* R: j0 B9 f" p; | - 4 Q5 u8 J& P9 n7 L! S+ A
- }
. b* w" w# r( C* z- J' [/ I' B' D A -
' w5 a6 e# R0 e0 {! k) J) d L - if(tmpSearchPos[1]!=string::npos) {
; _6 e- K9 q$ r - i=tmpSearchPos[1]+13;
' R& f% k% u( Q V' u1 k6 d2 I f -
- U/ {: _+ c1 Z! U1 { - while(processStr[i]!='\n' && processStr[i]!='\r') {
, d# x2 t6 ?8 q9 q - cout << processStr[i];
$ l4 L1 n" ?; X* m4 v; |% Z* Q1 y - i++;# Z, T0 ?1 ]" g( G6 t9 I
- }! Q, w3 @& ~9 X7 c7 k, O' S1 y
-
" l5 l, d1 `9 ]4 k; Z( N: u6 s$ @# J - cout << endl;
" x3 h* [' E% Y+ a( [: f -
5 i) c% E5 Q0 w5 J: @, E - }
6 B" k M* p6 E* Y - - E2 A8 K+ V1 c% X
- entriesCount++;, m/ ]- W H0 k
- }
1 O$ R5 c/ ` B* A- j$ b4 | -
1 S6 U8 H/ m7 E5 x& c/ ~/ }2 i4 X - cout<<entriesCount;8 w- d/ X d" ?0 J
- return 0;
+ ^4 ^( @& O: D; ~- n$ o$ E -
- L% g, f7 `* M$ k - }
复制代码 Update: 最后一行输出条目数.% r% @9 u' l7 d% k
) \; }# h) _ q[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|