- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis& N9 v0 K# r$ A* x- I5 c
N6 C; J$ m# |" ~5 z9 G- //
1 O/ I+ D3 m$ V! u4 C - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
% Y. ^$ \9 P$ I - // In case this is not legally possible,1 G7 r8 u! v2 S2 a7 n
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.* J7 X* \* ^' h4 g( u$ t! ]
- //
! r& m, [/ R$ C2 \, t& m - 1 J. i3 p0 z6 A8 h, m$ y
- #include <iostream>2 ?% l4 w' r4 Q& x/ y
- #include <fstream>- }: f* J7 P1 j
- #include <cctype>
, |: h, X9 X3 u - using namespace std;
" K# S9 z, }" N4 `. `
9 ]$ J$ W7 `0 a9 {$ `& i- // MACROS
. @' x1 Q- y. S- f - #define SYSTEM_BLOCK_STR "[system]"
7 y f6 u" r9 M7 z, Z! { - #define NICKNAME_STR "nickname = "8 G7 b0 ]& C$ ^3 |& }" [4 _1 n
- #define IDS_STR "strid_name = "
8 x. t9 d) L1 Y1 W/ h" }1 q2 [2 ~ - //# G4 R2 v) r( L) t# i, Y* h7 S5 d1 L: G
- ~, x: } t4 g1 G3 }# f
- ifstream inf;7 [# A9 v# e. `3 J+ G
- char *buffer;; ^3 b( {* X0 }
! l0 F0 a& U4 P0 {+ W- int main(int argc, char *argv[])
- ~+ j* z8 {' u$ m2 C& \2 ~: } - {
& l0 {3 y [- l1 G - int i;/ ~$ I; a+ s, }) N
- $ ~. e4 K1 q% y9 b& c% u, @: p- {
- if(argc!=2){ 7 G. O+ N- f! q! q; o5 d3 B/ s1 @! }3 w
- cout << "This program requires one and only one argument.";/ V0 H+ y. m* s0 V ]4 Y
- return -1;% z+ U3 |" ^6 T" J! @& }
- }% s5 \9 Z/ N; G! o6 T6 d
-
; d7 g w7 A4 ~0 |# ^9 E( q' I - inf.open(argv[1], ios_base::in | ios_base::ate);4 ?& B1 I$ E$ k7 C# z; P, Y \
- if(!inf) {
) p+ o( o* K2 Q0 @ - cout << "Error opening file.";
6 C! L5 q; A1 \% ]$ V/ H - return -1;
6 E' B- U) n& x/ q) } - }
+ R* C- l1 N+ |% ]. K' O2 l- c -
1 \% T. ^! Q7 Y% e - int streamLength = inf.tellg();. J3 \/ m( i1 F0 E
- if(streamLength!=-1) {
& a4 N, s8 S6 M. Q/ j/ m9 R - inf.seekg(0, ios::beg);' L7 X. U! T) V
- / r* `6 Q* |2 d8 {
- buffer = new char [streamLength];/ ?: u8 j* j! f! E
- // Read file;$ k( F0 l1 u$ D
- for(i=0;i<streamLength;i++) {: m. ^+ i5 K$ _6 ?9 n; G
- inf.get(buffer[i]);
( E! V% v+ X9 |9 x - buffer[i] = tolower(buffer[i]);" |: K: `6 z$ x$ e9 z
- }) d$ }& |7 G% ^; Y m# Q# V& K' f5 O
-
; E/ U& F0 o+ X% M: w# H# h" N, K - /* For testing
6 ~" K2 W9 y% s( i5 \4 {2 t: X8 p - for(i=0;i<streamLength;i++) {! N* k2 f8 h+ s- d- Z
- cout << buffer[i];
/ M( c' T# f6 F: Y' e - }. K% h% ^' B- d+ A
- */9 C H2 Y5 x9 K5 F7 N. X
- }2 o* c7 x2 v# T% o$ R/ w
- else {
( A) \+ k" C; t - cout << "Failed to read file." << endl;4 U' ^* o0 _+ c# ~7 N5 b) {
- return -1;! s' {- O+ I! ?9 ]+ [9 n
- }
8 c* F+ u3 ~9 T+ ]0 g6 s; m$ w -
; T: L" h, t, G! W) F/ q& i9 d - string processStr (buffer);" ~/ p, l! }- E- Z' V5 r, j/ Y' Q
- delete [] buffer;
- z; H) O) @, ^ - 1 D9 r1 T* y. M; ]7 |
- // DUMB DUMB DUMB....
% e# i1 J$ O( r -
0 V% e$ g) Q$ s% @; `! a - int nextSearchPos = 0;
, b5 E' y; n- v- C+ I - int tmpSearchPos[2] = {0};
1 C, [; }! y7 [ - int entriesCount = 0;6 a/ Z; i- b- _; }) ~
-
5 _: G. D4 B) v# A( a. p -
/ n2 Z; N+ F9 k3 v- t$ X -
7 t0 V. e6 G. J% H4 c6 u& @ - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {, | ?2 m5 t5 m
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
w; g% v! A4 A - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);) T' E% V6 L0 c2 U) j
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);1 s4 Z5 u6 |0 B! k5 l# L+ r3 d
-
Y) @; N8 k7 r' e& | - if(tmpSearchPos[0]!=string::npos) {" c; s( F/ y1 v: A
- i=tmpSearchPos[0]+11;
: D, q- ?) P- d ] s2 p3 w( \5 k -
" F0 j2 k- E3 p+ a9 A5 H" W - while(processStr[i]!='\n' && processStr[i]!='\r') {
) Q: V5 K1 p" c6 z1 H - cout << processStr[i];
% w' f& {. b$ T& [' n7 ? - i++;
9 ?* v! r( k6 q, \ _7 _ - }
' J# D, Y- X& { -
+ G9 D0 C: f1 u+ `5 y - cout << endl;
C9 R( M2 I/ d. I5 |( ~ - & w/ Q: Y1 o7 L
- }
. ]' p1 t2 j, r" p - " c8 |. b( W7 B' v$ t
- if(tmpSearchPos[1]!=string::npos) {
: G$ b, ]) I. ?2 D$ l - i=tmpSearchPos[1]+13;) V0 C. V3 h( o
- 7 v* k7 w" n7 R- r
- while(processStr[i]!='\n' && processStr[i]!='\r') {
; Q5 Z) \6 ~) o. v6 ~2 s; X! u - cout << processStr[i];/ |( C+ [1 [$ v$ S4 c0 F6 ~
- i++;
5 m! l. F' ?: r, Y" s4 { - }/ n: w0 C) e E2 v' z
- 2 x, h8 O8 g" g: n. c# Z
- cout << endl;' L$ {- V& x& E$ t1 u) ?
-
4 G* b( T" g, U' L, x5 { - }
+ |5 [. o6 S& x8 u9 g; f -
' N7 Y% o* h1 y. J& i0 ?1 ^ - entriesCount++;5 W) k) a$ g$ o( ]- h7 k3 ]6 \. J# H5 E
- }" B/ I+ Q V3 D. T
- 6 x! |1 A4 D, t
- cout<<entriesCount;: A: [( r$ C5 Z# ^* V b0 e# N) h
- return 0;
8 I8 h- [5 }. H2 [( i t( A; e - ; N2 K4 Z) V! R1 S, L6 E" ~
- }
复制代码 Update: 最后一行输出条目数.
! O6 v {. m+ D
4 n. B2 H& R0 O s[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|