- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
' K$ u0 V5 G5 M, @
* B3 I8 v6 K% j: N7 z- //0 d/ j: `* d, s6 u; k! T/ P
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
: w5 @; E3 D) s7 h - // In case this is not legally possible,0 o$ ]& G5 j- b1 }: O
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law., |! G3 h5 G; C! o) z) X
- //
. ?2 T+ H3 }* x$ n8 w% O- V
$ E0 O* i& r1 i- O; Y, s, a. L- #include <iostream>
( Q( e" L" H6 _ - #include <fstream>
3 |8 }! ?$ Y8 n& r/ ^ - #include <cctype>
" [) }) d- l# S$ ~3 F6 ? B - using namespace std;
) T$ I6 j1 _0 W# w4 y
" \9 |4 P$ N- X/ _: b- // MACROS
* r. l7 n4 a2 T; H! b, o5 H3 d: H [ - #define SYSTEM_BLOCK_STR "[system]": { _' E, Y) {' K$ f, C9 p
- #define NICKNAME_STR "nickname = "! {( |5 F# F) g6 W+ D
- #define IDS_STR "strid_name = "' G( ?) }5 s+ ] v
- //. c/ I: q: N" y& H7 q( l( m; r
- $ X2 f& J/ o+ l$ m p3 ~2 o
- ifstream inf;. d8 _# i- {% U
- char *buffer;
- _& b. W# j% F1 C) C. V
* h' b6 l( V% V8 @$ j- int main(int argc, char *argv[])1 X- c$ h# H: R0 D Q, s+ M4 I
- {
/ m6 N3 z, q6 V) B" G1 T7 O- h - int i;# ]) m/ r* B) h/ B# Q$ ]% A% }
-
9 x# L" X. g8 U d& D# V7 `+ l - if(argc!=2){
8 X! D/ R* X P - cout << "This program requires one and only one argument.";
( U. S) s% `7 Q+ D0 T- \- {1 b5 Q" x - return -1;) d" u. r: Q/ z$ L- F+ @
- }
: F# L; |& Y$ X' a( A, Q5 _ -
/ J0 @% m. i. i# ~7 _5 D - inf.open(argv[1], ios_base::in | ios_base::ate);
" P& q6 u( e. k - if(!inf) {+ ?6 Z, W. x, v1 k) V$ h0 W
- cout << "Error opening file.";5 E7 G- f8 w }/ @$ ]1 l2 A4 ~
- return -1;7 d! b2 h/ k+ q- |1 ^
- }
9 |7 \ S4 D! [% R2 k3 s0 u -
0 e4 h/ ]. f% |5 Y( ?6 B - int streamLength = inf.tellg();
& u& V# [& h4 Y( H X - if(streamLength!=-1) {
0 B+ H" s& _' q* }5 S7 [ - inf.seekg(0, ios::beg);% g* K$ t2 }# [( @3 o. O
- % n" T5 T8 {; @5 d
- buffer = new char [streamLength];% n l3 V4 x2 q! f6 e. F
- // Read file;* T n7 A# }0 I7 ]
- for(i=0;i<streamLength;i++) {
" h+ g7 I8 X) [ X1 R! t$ B - inf.get(buffer[i]);
) X) p& P5 H' M9 U - buffer[i] = tolower(buffer[i]);* A6 ]0 u4 m. p4 J$ r/ a
- }
4 d9 V, o. H+ f$ H. F -
1 l6 p: m% v( R# |7 U" l* O - /* For testing
8 w8 X! K- D0 H8 J& r9 j - for(i=0;i<streamLength;i++) {( B" R8 [# E& n0 r; u2 Z9 F
- cout << buffer[i];9 B: D4 T1 d( ~ W" q2 f4 H
- }) ~4 Y6 H5 B) d1 ^
- */
4 v/ O: t8 \' _$ }7 z* V - }/ V; C p# q; Q
- else {7 t5 _! R" a- Y5 I. @& p# @( B
- cout << "Failed to read file." << endl;9 [4 d* o2 l9 W, c
- return -1;
# p; r2 b1 e8 u& }9 i/ U9 O1 l - }
: X4 o7 c; A( b2 o -
/ x, b( w, L! f6 i - string processStr (buffer);
4 g# F- f* i2 x9 x. {- J3 d2 }, Z - delete [] buffer;3 Z3 m' v) R1 }2 U: L" b& k1 q3 `
-
u! Y f/ Z0 T. M* D l1 O2 f - // DUMB DUMB DUMB....
. [+ W, W H: L ^- ]6 s -
6 a6 |- H/ U) o2 H" Q$ U - int nextSearchPos = 0;* ]0 [; o# u0 G2 E& z
- int tmpSearchPos[2] = {0};
* N1 x1 ~: h- |( U7 V& c - int entriesCount = 0;
! Q9 M, u/ O4 U+ ^) I! N. r' Z -
. ~0 k- O3 G. S' h! H -
0 D" n0 ^& t9 `$ p6 l8 U - 3 I0 E7 w0 ]0 e" A, n; M7 p
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
: Y) Q5 v$ | l - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
4 R `" l$ A- l# \" _/ L& c - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
. a" D2 T! D$ K6 ?. ?( P - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);, K; ~! m/ A: R8 w
-
+ v8 s3 o9 z2 s - if(tmpSearchPos[0]!=string::npos) {
! e# c7 e* n' \4 ~0 m7 U% r/ F - i=tmpSearchPos[0]+11;1 ^) H9 @5 Q' V
-
0 o' }! P- c; ^) W+ \, f3 A - while(processStr[i]!='\n' && processStr[i]!='\r') {/ r; }0 s7 n2 E' ?5 _
- cout << processStr[i];* @5 E% K8 E# j3 T; v
- i++;
$ \' r, K: v$ L/ ]# c4 `% `+ N+ D - }
7 P4 q4 v& m- b( |4 a -
) v. O3 a7 L7 n7 M* o; j - cout << endl;. D" ?% d( \2 B7 h
- % @2 f, f5 e( ]% F$ i# |. X
- }
, ]' o: \, L4 R2 l$ g* t -
& G* @7 o: v9 { - if(tmpSearchPos[1]!=string::npos) {4 R/ V4 }/ [% v6 o
- i=tmpSearchPos[1]+13;2 t- [& h; B! D* g& |6 A3 {
-
' A! h7 A, F. t1 b1 V% d - while(processStr[i]!='\n' && processStr[i]!='\r') {4 h; M* c$ w! ]! B6 P3 r
- cout << processStr[i];
; Z6 ]* T- a- l# G - i++;
9 @# [: ]; [# z* m- O5 z7 a - }
6 D1 B- E/ b& l5 V! e+ V4 a - ( q, u9 a& c0 v. O" A+ \
- cout << endl;( g, M3 n" N2 P; Y% y$ H" N: Q
-
8 c+ Y* x( ^; w7 M - }5 H {- @5 G! k" y* }+ Y
-
6 y! q) @- b0 `, K - entriesCount++;
2 D8 ^9 F1 D0 w, L - }9 H8 L+ B8 }+ H9 E' [6 S# E/ x$ p8 u
-
3 g# K3 }) H m" ^2 ?% ^2 X4 s" C+ q - cout<<entriesCount;! K+ E; v8 ~9 s( R
- return 0;
! }, m. B: b, u3 R ]. m U -
+ Q4 z4 `2 ]+ P' \5 @$ U/ b - }
复制代码 Update: 最后一行输出条目数.
+ f7 {9 }! {: a* n# `% N* G0 C/ M0 ^- k! ]7 l7 Y- \
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|