- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis7 V" C- Q Y! v9 G- Z
- 2 W& R8 |+ i) X$ B( K2 B3 [
- //
0 l1 }' Q ~9 Y - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
' A9 S9 w* O3 i# E( j - // In case this is not legally possible,9 g5 D2 |- }5 E9 @1 j/ y4 |; ^( g
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.4 [# i. r3 j6 v. B, e. P9 \7 B
- //
) I7 r- r7 o4 U* Y) \
$ e3 @' b0 c" Z- #include <iostream>% u& X: U. z) C* O% `% i; P4 I
- #include <fstream>
: V$ r- M0 n3 s* Y! u+ ? - #include <cctype>
# D4 e$ K1 a, S* K" W - using namespace std;
+ v: X1 n" i/ [; s% x3 N# t! I
# I& [* |1 W8 [0 e- // MACROS
) o; U# ^* O8 d4 F u - #define SYSTEM_BLOCK_STR "[system]"8 J) o: b9 O7 E& j5 h
- #define NICKNAME_STR "nickname = "
" y J$ _7 S) ^2 S j - #define IDS_STR "strid_name = "" J( u) V4 z2 G' ]+ M$ J
- // ~% x5 V, {4 t0 s( E6 L
4 ~$ j5 z# Q( }8 O4 e3 A2 I/ V- ifstream inf;9 `$ {2 j* [. U$ F5 d3 K
- char *buffer;# l7 |* b0 [. Q- ^3 m
( O. [& ]8 W/ B6 V- int main(int argc, char *argv[])
$ @; X5 U1 A' [* S I# ^, |0 s, }1 h - {
, q5 x* ~0 J" P* j - int i;" X3 h. A1 q( ?( K/ ?8 X! B" Z0 }
- 3 o4 H0 @* S6 M; {; D) S1 m
- if(argc!=2){ % z" C9 F2 R5 S% p* H2 L1 D
- cout << "This program requires one and only one argument.";
; A- a* O) g& K3 @3 S$ j% s - return -1;# M) C5 I* x( s& \
- }! V, ?$ v! M0 k, D# c3 V
- 4 v v) h$ ]1 ~8 J E7 P8 c
- inf.open(argv[1], ios_base::in | ios_base::ate);* X6 q) u! |$ T3 ^ V! i2 V4 @& U
- if(!inf) {1 b) i% s* P3 ~- Y3 v' h$ ]
- cout << "Error opening file.";* `0 D7 T: E' w l* b
- return -1;
( j, e9 x. `9 @5 D+ m1 C$ \ - }
: w% [& p4 _# r- V! Z$ R( ] - 9 A. |* Z* \! R
- int streamLength = inf.tellg();
$ K5 o+ Q" E8 B3 z, f - if(streamLength!=-1) {8 k) w; h' U% `3 `
- inf.seekg(0, ios::beg);' ^" E3 U% J/ N9 G3 L- q, P2 ^
- $ ]- C5 v ~1 @5 y3 I
- buffer = new char [streamLength];2 Y+ u1 A% [# }5 N, Q
- // Read file;7 {* x( v+ X' ~) |' R1 k; Y" \
- for(i=0;i<streamLength;i++) {
' R* {6 A9 |" `6 b7 _ - inf.get(buffer[i]);
0 Y& E0 n2 J# `4 S! l - buffer[i] = tolower(buffer[i]);
5 r% v8 X- F: W! u% S: A8 \ - }8 k1 p( g3 Z& n1 C2 E/ Y4 q
-
9 s B$ X( S! X& A. U - /* For testing" O" z# M6 }+ T% c% i
- for(i=0;i<streamLength;i++) {6 `5 D+ ?) q* o3 I" E# p+ D$ h
- cout << buffer[i];
3 q2 N* u" ]3 X6 E1 I - }
$ ^$ O, [: e" g Q2 j4 i9 b7 z1 B0 c - */
. {9 R: ?) `3 l. A - }' R6 I" X3 @7 F- }9 c: \
- else {& Z( s& H) s f* P6 K
- cout << "Failed to read file." << endl;1 h, D" q7 z6 Y' l: V7 H
- return -1;
, b$ U/ y+ g- x: c6 v - }- X# E7 u ^( c9 L/ h I" n; t) T' F
-
9 Y/ E' U8 @# N1 g: i+ A+ |) g - string processStr (buffer);2 s' ?" e- g" ^3 V4 @
- delete [] buffer;' E# a. j' F2 G! ^, |; H* F
-
8 H/ v7 ]0 s9 `5 i4 M& [ - // DUMB DUMB DUMB....( T5 g: ^ r# t5 l' B- B! p
-
4 B6 G: S5 N5 b u - int nextSearchPos = 0;. O7 b6 ^! P& V% [ ^2 n3 l
- int tmpSearchPos[2] = {0};
8 S* l+ P$ j5 _) o [' `% Y @0 l J - int entriesCount = 0;
$ X& r& _% {5 [+ k - 6 i# Y+ Y' B- T. C5 ^
- $ c2 p' i9 m* [1 w1 _0 z) b/ A
-
% _2 E- c }( h; p - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {; r+ p7 @% s. s+ _1 y
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;8 v1 q5 b. g9 Y) F2 W# k
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);/ W( `! P. U; ~; s
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);% O5 G& {6 d" x, Q# k" C/ P
- : Y" G+ ?5 E; B2 ?8 s
- if(tmpSearchPos[0]!=string::npos) {4 ], H% F# l. |7 l% l" \+ A
- i=tmpSearchPos[0]+11;
0 l7 l) F8 N! m4 w8 v2 ^* n -
. f8 R* B* V% q* P - while(processStr[i]!='\n' && processStr[i]!='\r') {; _, v7 ]9 h- @
- cout << processStr[i];
* w$ y7 C% ?* K& i# T - i++;8 \: W$ ?; x: O3 [- P" L' H
- }
) ^/ @3 m; K& b0 s -
& p& b/ I/ T& ?3 | - cout << endl;
. O6 |! B% `5 a0 G$ H" c - ' ^* D5 A" V1 e- K( M2 U1 m
- }
, X, K5 u( l. K+ R5 u - ! J; q; Z9 Y& n6 S0 |
- if(tmpSearchPos[1]!=string::npos) {. S2 _+ A0 d/ u4 P8 w
- i=tmpSearchPos[1]+13;2 n: R0 t! V/ [6 v4 s1 B5 U4 P
- ! v O% l$ P, _; l$ `
- while(processStr[i]!='\n' && processStr[i]!='\r') {5 E( m( z9 ?8 l/ Q, P2 h/ o1 g; F
- cout << processStr[i];: E1 I3 @& D \- l. I
- i++;
* ~2 c9 U% N# G9 ?5 o* Q3 ] - }4 ~, _7 Y- b) E( b3 E- V/ h- A
-
: W L3 e% T2 r* }8 M7 { - cout << endl;+ a$ r$ v7 |. ?0 u" q g
- 2 D, z9 ?9 J; j9 C. \3 e
- }1 Y& B- {+ w% V/ h
- 7 d* ?8 Z3 C! R$ d
- entriesCount++;) l" k" a; Z$ j# o/ \
- }; S9 F, D) f4 p9 g5 X0 y3 \9 `
- 8 u1 q4 W9 n0 t9 x. F* D: i+ G* F, F# t
- cout<<entriesCount;
& ~# S$ v# r/ j9 v - return 0;/ r! N5 \# F; ?. S7 |
- + j/ j, S% ~0 @- f: P
- }
复制代码 Update: 最后一行输出条目数. Q4 t. M* V5 x- j. N& h1 _8 T
% M: U* x4 H2 X L[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|