- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis0 B6 B2 }9 ^( E' s1 a$ ]5 V) H4 h
- % ^* j- Q1 P: v) a! [
- //
' W& {1 C$ A; d - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
5 G: i5 Z' O. s6 u* ^0 d1 c - // In case this is not legally possible,
0 N% ^: R4 J5 R+ z - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.. G' W1 x+ ~0 \) Z
- //
4 p }! ^/ l3 U' g: ^' p0 Z/ n' V& ` - , C( t: ^6 c5 o: v
- #include <iostream>
0 K2 c- y; K& A - #include <fstream>8 q! t# _& S" o( ~3 a
- #include <cctype>
, y% ]" [- J! x6 L" @9 M+ W - using namespace std;) r0 R* y$ r) J+ K3 |- _
" z0 J/ p. b. e' G9 c- // MACROS: y& s# y# _) } }% C |0 Z! C! F: W* P
- #define SYSTEM_BLOCK_STR "[system]"
5 R$ e, V" R; x: \8 x- {$ c - #define NICKNAME_STR "nickname = "
& M1 U8 O: c+ B: U u - #define IDS_STR "strid_name = "
, ]7 t4 U' I8 |9 }: m2 T6 P - //) `/ G L- D3 [0 U1 L/ j5 c; j
- 0 U5 x D3 Z* Y% ^. D3 Y# M9 y
- ifstream inf;
R' e' W: j; x F$ f, C - char *buffer;
: s3 b6 p. d$ f! c: G- @ - + D# U+ h- s+ Q! i" f1 c8 o
- int main(int argc, char *argv[])
& l! }, R. E$ w% c E - {. Z4 j, w `) a# e0 |
- int i;! t E! x* C7 O F# r" @$ ~+ Z
-
) b- o4 v4 q8 ^% S V - if(argc!=2){ & { C7 g, u. I& U3 R) t6 k( x( N2 n
- cout << "This program requires one and only one argument.";
( C- W! p8 w7 Y6 P6 } - return -1;/ ~$ r" R: e q- x1 Q6 f
- }
5 K! L9 j: D1 L4 d - 7 A# Y" y& R# ]: c9 f1 s. v/ Y
- inf.open(argv[1], ios_base::in | ios_base::ate);$ J) L( h J/ G: P/ Y4 {
- if(!inf) {
Q) M& w# U1 w" |9 q' O8 b - cout << "Error opening file.";
9 G3 |5 Q3 T0 z5 o% Z, N! h/ m - return -1;
( p/ i5 g( a- f: W, z$ k V, Y - }
p' X! o3 X5 N" p! {. d" q -
8 H# P6 e7 k+ i1 m - int streamLength = inf.tellg();
e& c- ~& u! {) i' I - if(streamLength!=-1) {
' o% t; q/ J$ | - inf.seekg(0, ios::beg);
# c+ t9 O: @6 x( l K" M - * q( T" c* n+ a5 [
- buffer = new char [streamLength];
8 _. V) M( B2 u, } - // Read file;
+ V& `2 t+ z* [9 W4 H - for(i=0;i<streamLength;i++) {
' E9 ]3 e% ^3 e - inf.get(buffer[i]);
& V8 p. Z6 E, x5 k! |% z4 s - buffer[i] = tolower(buffer[i]);2 z1 W) S& I, f9 u/ H# ?! D; `2 v8 b
- }
: w" U# ^' _; ]* |& W$ J -
& g& F" k/ |3 G+ p1 ^( O - /* For testing6 C# F9 s2 e# g6 N9 X; z9 r0 Q
- for(i=0;i<streamLength;i++) {3 O0 h3 t. p1 ^. ~; }1 U4 k w
- cout << buffer[i];/ z/ Z3 \3 B2 I6 I( [# C4 i
- }
( h: c9 w) k( Z' c4 P/ K6 ]' @& G - */
5 Q# K3 T. h( E U - }9 A" Q- m" z- k" `7 K; V
- else {
( v( _9 G- r6 D: V0 ]: U1 o8 x3 B. ` - cout << "Failed to read file." << endl;9 n- k' Q H: r8 C
- return -1;
$ K% [3 z4 P8 v0 ^ H - }
! h8 z/ Y0 Y: _, j6 h1 _ -
& k( w4 o* L9 i9 O; r* M - string processStr (buffer);. g8 f, l, ]4 A5 g9 c4 t! T% B
- delete [] buffer;& C3 H& T3 @3 o. l) n, K
-
- I5 T+ ]$ H0 Y$ F3 h4 c - // DUMB DUMB DUMB....
% f, W; S# W2 d0 J7 P/ a2 e E" r -
% v) u) B j1 h1 Z - int nextSearchPos = 0;& ~6 {$ z" j [3 K
- int tmpSearchPos[2] = {0};
% r+ A& ]- f) `: m: ^+ ^ - int entriesCount = 0;' E; v1 Z7 L7 I0 Q4 E+ `8 |
-
; i- F5 J2 { J6 J! G( a, S: s -
* t! T" `8 [5 r: } - 8 ^+ _8 Q& \. d4 Y# X# U$ H R7 _
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
, F1 o- K$ \. F4 ]! P7 G | - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
* g8 y9 c8 g% g6 I( i O2 ]8 } - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
2 Q, w/ q( k9 [2 w. r. \ - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);0 R e+ }' b: {) I: P6 K. r
-
7 D4 X: r8 f: i - if(tmpSearchPos[0]!=string::npos) {
% _- _/ C) J' T' u9 _7 y/ l6 L - i=tmpSearchPos[0]+11;+ S: N: K v3 W* u6 z1 d3 `5 i; h
- 0 }8 R1 C$ u: M' I/ ~" v( |% I
- while(processStr[i]!='\n' && processStr[i]!='\r') {8 [7 T" _6 ?) z& w
- cout << processStr[i];5 g+ t/ ^* ]) [% g
- i++;6 d: h% I% n/ [! n* Q
- }" i# y" D3 I9 ]4 I8 N; }
-
( E6 V7 W! i* q" _! l - cout << endl;
+ H" h# {9 b! h' D- { - + \; {" [$ }% Q* }% c1 ?9 x
- }" G' T2 J9 m: l: t! N
-
! u1 e+ x( |" m' u3 f9 c - if(tmpSearchPos[1]!=string::npos) {/ R" r' w) u: u
- i=tmpSearchPos[1]+13;
$ q7 f+ t9 }/ B- C1 v- N& w( W+ s - ! r7 j! x$ ]/ {
- while(processStr[i]!='\n' && processStr[i]!='\r') {
1 m- M$ Y/ N* u6 U! P) x - cout << processStr[i];
1 q, v6 n% p; u3 [6 b+ Q* u - i++;
" p' K5 k$ F) x - }7 h7 {; n4 t, K6 G% B. r; P/ f
- ; o7 V/ ]: T/ u/ L& s: [1 ~3 o
- cout << endl;
8 n* J, B8 e [# h4 H& n - $ c% C- k1 [/ i* i: Q1 m1 q5 {
- }
0 J$ F5 e! p- z -
0 d* [# a) M+ t5 }# I - entriesCount++;. o: x g( F( o% m: S
- }
7 L+ M$ A1 R1 o9 }- a' U) { -
0 R2 ^# B' A/ b) p - cout<<entriesCount;. |/ C0 q* u+ }$ q2 T/ W# u
- return 0;
' B$ |* A& o( K# G$ p7 s - $ ]0 \0 o3 T1 Q K& Y# e) I
- }
复制代码 Update: 最后一行输出条目数.7 D8 ]/ ^5 \( |+ W
/ R3 ?! P7 }( V6 q% r/ x" ^
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|