- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis7 A* V7 E9 K g/ c9 c9 Q
- . [( N& y, X3 M& G4 f# L; b
- //
# O. @9 G; F+ m% s# Z* L - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.1 X: }0 x% i' p9 v3 }0 g, Q/ q' ~3 t
- // In case this is not legally possible,
8 @/ X7 u7 z* k3 C7 k0 \ - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.1 {" H" c# U/ {9 K- m
- //
- x0 V- G' V8 B4 q( @2 Z - 7 t# \+ ^7 K) y; O1 ]2 k, q( S
- #include <iostream>
7 u4 w& |! I! K" N! d* @% G - #include <fstream>; ^3 s! J. Q) I( t. f+ q ], Q
- #include <cctype>0 b8 w; U) L1 B/ ], y
- using namespace std;
5 p7 B8 j( l% Z4 u6 B: W
# h. q* x/ q- j7 O. {- // MACROS
. h" o" d+ i3 Y+ S - #define SYSTEM_BLOCK_STR "[system]"- L1 }# D$ T6 }# q/ r8 c4 M
- #define NICKNAME_STR "nickname = "
; X3 @# n3 q" G1 H* o* |6 c7 H; ~ - #define IDS_STR "strid_name = "7 K6 O2 Z& w( Z, Y; V r/ K
- //" U9 C8 Y$ L! \& g
: [: E# C, }/ F8 f% s" y- ifstream inf;
: \" Q; g' y- j3 |: r( T* K - char *buffer;
) Z6 C5 U) d1 K, O" M+ g$ d0 |& C+ V
* F! G5 C( D7 ~3 [- int main(int argc, char *argv[])
) {9 Y% x7 {+ a - {, G8 N. \8 p+ L. t# x" @8 |
- int i;) u, Z {. e2 w5 _0 c% w: x2 y" @
-
' u' b" R) G2 f9 Q7 N- ?; s F% | - if(argc!=2){
. c3 o0 K0 }) Z6 Q/ N' S6 {0 D' Z - cout << "This program requires one and only one argument.";' O6 d3 O+ M: K2 [! e
- return -1;
6 R3 O+ a7 z$ p" G7 Z# q& q L - }
) c* l+ o' V7 _0 H -
+ D% o& J) T( O, ?1 W - inf.open(argv[1], ios_base::in | ios_base::ate);
$ ~1 q& I( Q |" Q - if(!inf) {
/ x) Y0 B- T1 Q; j2 q6 o7 {/ W - cout << "Error opening file.";2 o) [0 Y% B' b* Y" p G( Q
- return -1;
$ }4 h/ k6 Z6 h( k7 _1 B. l - }2 {/ l `, U; w6 C* ?! F3 |
-
# O6 n! l3 K7 B/ w* ], X) M - int streamLength = inf.tellg();
8 p- G& f3 @; _- D ]6 c - if(streamLength!=-1) {
) U0 a1 W; n8 P1 z9 H) A - inf.seekg(0, ios::beg);7 ~: p; X( u# e8 p! }& Y
- 5 r: j; h( H7 E( C" Y7 }/ H
- buffer = new char [streamLength];
9 c( T! j# [- L) R3 M9 r - // Read file;
: j/ G7 b$ d3 ]" t - for(i=0;i<streamLength;i++) {
+ E7 K1 t3 P! d* q: I6 S1 a+ J - inf.get(buffer[i]);
0 S# I; @' o6 \. w( E! u( Z9 Z" I6 y - buffer[i] = tolower(buffer[i]);& k: m9 ]6 g: A5 w c( ?! D
- }
& v& S% ]( H% j' r2 D/ X - 7 x; A: d4 d9 C7 |
- /* For testing
7 u8 n0 n7 x3 ]0 R6 Y$ v - for(i=0;i<streamLength;i++) {
$ v& I4 o H- F% S8 s6 \ - cout << buffer[i];, Q' f( n, W2 x2 ?& ~. d: E# y
- }# S- L7 [& W% a! i6 q
- */
% u" g4 h2 R0 \ - }' e! O( s; T7 Z* {
- else {4 o4 \ z7 E$ R: |9 \4 }
- cout << "Failed to read file." << endl;9 `" r( B9 K1 D! _, Z# c
- return -1;
& h1 G; u9 C# {. ~+ _ - }
7 Q4 [/ n' y9 e3 R4 d - - |- b- r6 s Q2 r ~
- string processStr (buffer);- S# k/ M3 |1 H
- delete [] buffer;+ r- K, x- W# A8 k( v. l
- 2 R, K C6 m; y6 c2 s
- // DUMB DUMB DUMB....
8 @; F9 v4 J! N1 |$ W% s -
8 i5 t: C" a2 H1 X( v5 n5 } - int nextSearchPos = 0;2 j0 w z8 s5 y) O8 c
- int tmpSearchPos[2] = {0};
- r) C: U# I e; @ - int entriesCount = 0;2 D, A7 h" O7 K$ C9 C
- 0 z1 R* ?; _+ c: D9 [( Z
-
. F, C" i9 N& z0 B9 R! C5 o- M7 g -
3 U2 e( \% d/ C y - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {! r+ r- [ D$ V4 ]
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;1 r8 f2 v; @! R, {
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);0 D$ x/ I ]$ O7 T; g* F* {
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
3 F) e% G& ^1 W3 y$ p -
: T4 K% K( N" z/ a. \& a - if(tmpSearchPos[0]!=string::npos) { @! F( O' k5 p" u. a
- i=tmpSearchPos[0]+11;6 j% n9 Q5 s" Y5 y% f6 z6 w8 I
-
* b! s0 _! q' F - while(processStr[i]!='\n' && processStr[i]!='\r') {; o& }& _3 j2 c. U) N
- cout << processStr[i];
: T+ K# H. L7 s- d/ U0 V - i++;
. I. t& Z. C4 S2 s - }
& R N$ b# T* X) V& V9 j -
7 P6 p7 V0 r9 W; B - cout << endl;
+ w3 K9 J- a( X2 w9 a( c: {- c -
( s7 B$ J, q6 ^1 n! W - }( |: G% |: u5 \! `
- 5 e, P; h" u+ R- A1 X
- if(tmpSearchPos[1]!=string::npos) {6 y$ z& |) t: I1 }) @
- i=tmpSearchPos[1]+13;
! Z. k( p. V/ G2 }# v( L - 3 E8 u3 \3 J" t5 ~- C* U
- while(processStr[i]!='\n' && processStr[i]!='\r') {
9 |' J5 Y" |' T' F7 | - cout << processStr[i];5 s# @, o) H! g' s+ J( O
- i++;
! y4 b2 x6 F# U - }
& d; |8 @3 F4 u. i, ^ O0 S9 O - 8 p* d' _8 R1 \+ _
- cout << endl;
9 q8 t" n; S* Q! `, o; Z -
1 B- L; W3 O! h& I: M9 b - }8 }2 ?- u; M# E! U' G9 a5 L
-
2 ~8 M1 f) e O) A' V$ Z' _8 [$ ] - entriesCount++;% {6 a s2 _( k* C# w6 \1 F
- }) o. b4 ~% ?# C
- 1 s E: `, j4 `2 n. ^$ x
- cout<<entriesCount;
/ Q; i/ W( r4 M, }& b - return 0;6 K) d; a! F6 F2 K2 ?4 \
- 3 E: f0 P$ E5 w+ i. t h/ N
- }
复制代码 Update: 最后一行输出条目数.
[4 ?0 ?0 ^* T; z w( u
/ e, @9 u# X$ ?5 V7 I[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|