- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis) V' ?, c) E3 n9 W
- ' {0 u0 Q/ I* j9 f
- //* w6 O6 y) S% x' j
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
8 X5 [+ b" E1 j* n* m* e N$ |7 k - // In case this is not legally possible,3 h( q. d: n$ x3 p
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
' t o* D2 ^4 W$ C - //
! ~& G0 S4 T% Q! ^- Q# ` - 5 i7 @, T) t+ a H4 e
- #include <iostream>
& B) L. Z8 c7 O; y- x0 R - #include <fstream>5 i' C% V% F. F" U) h+ |; S B
- #include <cctype>6 l8 n8 [3 k9 o6 k" b
- using namespace std;) C6 ~+ H6 f. c* c( y. V
8 T2 t( j7 A! [# Q" i- // MACROS
* e4 ^; q% a3 T - #define SYSTEM_BLOCK_STR "[system]") _6 Z s% ?$ n. v4 R
- #define NICKNAME_STR "nickname = "
) r# q6 B6 j( o+ ~ - #define IDS_STR "strid_name = "; r! h: K* t3 B3 u
- //7 ?; E8 S! ]& _- _. y* N
- ( E- m8 R: {. r( S0 D% g
- ifstream inf;2 L1 l# [. n! g
- char *buffer;! Q+ X( }2 t( Q! f/ |
- 1 f% K6 _0 C; \+ F. {9 N* T
- int main(int argc, char *argv[])
# @8 D* B. G* R0 \' J - {
7 i( n! E* \/ Y5 a8 B! `+ T - int i;3 O& l, u) v/ c( {8 H1 B
- 6 B7 {/ n) J" c
- if(argc!=2){ Y5 x- f- f( p8 E" g6 A
- cout << "This program requires one and only one argument.";
% W. J' I; L- {: z& m8 y" c6 j- |, Q - return -1;
+ x6 U9 `" a6 b3 K' T - }1 Z% a) H3 Q3 P6 P, j
- ( q* d! |5 [$ @/ R+ c) }; h1 c* L
- inf.open(argv[1], ios_base::in | ios_base::ate);
/ R: E8 U t9 W6 H) S) N - if(!inf) {
0 S; p) q9 W5 M7 u; t - cout << "Error opening file.";
9 G7 a, H( j; O& P8 Z - return -1;8 A' L! z* L0 R# U9 S# {# a o2 |8 B
- }* D; O' f0 b {# D K* k
- * f/ ^0 v9 s% J
- int streamLength = inf.tellg();4 r# V2 o, j1 j% L
- if(streamLength!=-1) {
( @4 F( {/ s, a3 y - inf.seekg(0, ios::beg);
$ E; I3 d0 ]$ _) t* U, m - 1 N' v O4 ~; D2 S0 Y" h
- buffer = new char [streamLength];" s6 l, q1 G; { [$ b
- // Read file;8 _. X+ Q0 O) Z
- for(i=0;i<streamLength;i++) {
! N4 S; W/ r0 H/ M" K, V- C$ D& \ - inf.get(buffer[i]);* S* B+ z! \, _( b5 Y" x
- buffer[i] = tolower(buffer[i]);2 f, x, e" a( S9 z) F8 s! f
- }
* R9 E; g; W/ {# d5 [) y - 0 y, F1 f3 [, S4 M3 `$ K
- /* For testing4 E7 ?, I6 E) B' q- A+ _5 h+ ~
- for(i=0;i<streamLength;i++) {7 z; M# Z! {+ ?2 z# Y; t
- cout << buffer[i];: K# p. n+ f G
- }+ a7 x- m; V% D R0 l0 `4 ~! _! Z: s
- */0 t: K7 y( H l4 h- ?/ v) i6 }
- }
) ]6 u2 ^- u9 s9 a3 G) j - else {, h3 `% L: m1 ?1 B
- cout << "Failed to read file." << endl;
, a6 e7 M8 G; G, Q& W9 t% B/ \ - return -1;
) t& \7 y3 x y( i* p! a - }; [% ~& o3 P+ U" r+ r6 U; Z
- & e% N5 |; X; b1 c5 ? T: X% B+ b
- string processStr (buffer);
! ~4 E, P( M- C& Z2 g - delete [] buffer;% }9 M6 [; E3 j" w- A& ]
- 0 S, `( }* p, q! h7 k
- // DUMB DUMB DUMB....# ?7 n$ n8 }9 Z% H$ s$ h
- ; p+ n( j$ G$ ?5 `( l% a Z4 k* Q
- int nextSearchPos = 0;) f. r' | ?" O# z
- int tmpSearchPos[2] = {0};7 ?, q: E0 N f: d" Q/ W
- int entriesCount = 0;
4 T: i: Q8 X4 J }3 h Q -
$ \, c# z5 S3 R8 x7 F7 T0 A( ~ - : N! E- _+ w. w& {- ]+ s( f, T
-
3 P1 N# _ ?1 J8 P' H$ O: w! x - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
8 E. [' U( Y- Y! ^ - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;5 W) M! N7 I6 a W) {4 W
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
5 D3 Q/ F# f0 |! u; X1 V - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);% y8 E( q7 _4 J9 t" y& @
-
% u! @1 J; g' i" L$ g - if(tmpSearchPos[0]!=string::npos) {
+ S; h. x3 f& e, [& W9 K - i=tmpSearchPos[0]+11;
* c1 J e4 |* M' @' v2 u, d -
- @8 o9 _5 F" F4 S; b - while(processStr[i]!='\n' && processStr[i]!='\r') {
7 W- ]: W# M: N4 r6 U4 W( ? - cout << processStr[i];
5 O* E" D* E7 @( @7 o6 r' z - i++;
+ p% k! x% V; u/ f8 U8 e% r - }9 N! S q% g1 r
-
2 q* W4 @( [/ X3 v/ R - cout << endl;
4 |' ]# E! b4 ^5 `) M -
# S( I1 I% A, Y3 ? - }
' j9 D; Z% L6 d2 J" d, o& R -
0 h- w( D4 d* b# p - if(tmpSearchPos[1]!=string::npos) {
1 G9 t+ F2 Z' \: n. u" [9 E' | - i=tmpSearchPos[1]+13;' S% G: U# q) t& a) ^) q4 B; B* T# m2 P
-
2 G3 n6 ?8 r( U+ D) O* b- f6 _ - while(processStr[i]!='\n' && processStr[i]!='\r') {
4 {( ^( H0 f5 t; k5 O: ?2 E - cout << processStr[i];; L9 H% x5 F: _! d- ^3 z
- i++;, n, t" n! L& L) T. C9 B
- }
F2 w) F! q1 x! s - , Q! l8 S0 \( Q" ]3 w( C; f
- cout << endl;
# Q( f9 s- B9 x5 A% q$ Y -
7 B/ |, P7 ]9 Y0 C% X3 ~ - }
+ J) X5 W4 a* I' _0 O1 S( S" _ - 6 I5 p& I# J' f, I
- entriesCount++;8 h9 e7 G& \4 L- X
- }0 ?. ]; s' d8 G- F) I e" S% p6 v* Y
-
. R$ C# J: R, s! f - cout<<entriesCount;
6 y- Y9 k0 i8 [) R. W; h9 } - return 0;) v% X# L0 ]) M& Y' }. D
-
6 Z: a1 `0 T- i - }
复制代码 Update: 最后一行输出条目数., _0 T7 F, p' d0 _6 a
- o4 T" ?5 T. D% z+ h6 E8 L( \& `* {[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|