- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
# Q) _' Q: F2 z
' U, u; \/ D& h: Q7 w* W! T# M/ x7 m- //
% N' o1 y- o8 U( v+ M( ]3 U+ Z! Y( } - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
" h0 r! U) @" a L- u - // In case this is not legally possible,
! ]% E2 n5 s9 ^$ S- J2 M - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.0 [+ U; w% d0 h
- //
4 a/ D K: O' W" m! _: f - 8 i N$ p2 R8 G; P0 L
- #include <iostream>
0 W2 J5 f4 t$ L: r9 V5 K8 m- o J - #include <fstream>
% f6 W* B+ R& w( K! F- m - #include <cctype># W4 n# P ?" y0 g0 N2 o& Y
- using namespace std;
0 q* c1 u4 h5 g2 o0 o" i3 s! z
, R' |1 M/ b" I) `4 H6 m! d! d- // MACROS; |" N4 D E/ i$ Q" M+ h
- #define SYSTEM_BLOCK_STR "[system]"( v/ t, a$ A0 _# S) |
- #define NICKNAME_STR "nickname = "
1 ?' { N1 o$ y1 b! i - #define IDS_STR "strid_name = "# d* ]: E) W: c# K/ V
- //
% K% x: y! j, C& I% W' n' c - & E4 W6 z9 O' c, q. P1 I+ Z2 x
- ifstream inf;
7 g5 D' i1 b6 f0 Q( [0 y9 v4 O - char *buffer;
3 I* ~8 x- N, O2 l1 ?7 e7 F6 R% x6 A - * h1 i% N' S' j/ q5 C& e
- int main(int argc, char *argv[])- k p a z! R3 L" A( s8 a! [
- {! C' ]: r- l& L% T2 B3 F Z
- int i;
2 x* { E/ J! Y$ g. N: d7 ^3 d -
0 [7 s% T+ m: Q ` - if(argc!=2){ . e0 h N$ \1 b2 V
- cout << "This program requires one and only one argument.";
2 p" h" z6 S5 j: V. R - return -1;9 T+ V4 z4 t1 [" ?# T' \* I. m/ @
- }
' t6 b& l; _+ P -
9 J5 R- i8 T6 x R3 p9 X - inf.open(argv[1], ios_base::in | ios_base::ate);+ B4 v5 }) G# q" N* S* v; l9 o% m
- if(!inf) {
% w# `0 ?4 l) L; @/ T3 h# X - cout << "Error opening file.";3 C8 U) ^6 K/ u4 D' I& J
- return -1;, m* q, G' @5 Z* {; z
- }
7 ~- a. F* z* x2 V% n8 Y2 T2 ` - 7 i5 L& `- V; @+ o( U+ Z `
- int streamLength = inf.tellg();
, A$ G4 A. j: H2 I6 Y+ O* m - if(streamLength!=-1) {) p6 o0 W' l, H" y" [
- inf.seekg(0, ios::beg);3 j6 }- a# P" l. Q* ?$ L/ ]
-
+ q4 y, i; A! i% v- } - buffer = new char [streamLength];! j* M4 [8 Z- U; S' |" O$ h
- // Read file;
( D$ d, E6 P8 u9 ]0 z9 w/ v - for(i=0;i<streamLength;i++) {/ F T/ m, A y$ b8 d
- inf.get(buffer[i]);
6 d% x7 b' `1 O: V- {' G' u" d - buffer[i] = tolower(buffer[i]);
5 g0 z, A- q9 @ b) {5 l+ v - }5 S( y9 _+ M F$ ~! Q! l# F
-
d$ J* u/ b2 q) N' O( s8 M0 X - /* For testing' t" I4 r5 v6 I) n# G; v
- for(i=0;i<streamLength;i++) {
# A8 Z0 z: U. p' n# a: a) X - cout << buffer[i];
$ `+ i8 y& h7 x, I; V$ M; I - }
0 A1 P$ A3 I, p5 P - */4 v j; e% ?+ E e7 D
- }
3 c% Z0 f. r. W! h/ |% j - else {9 K4 z7 I" B% u3 F/ F+ C
- cout << "Failed to read file." << endl;
}) t8 }6 j' X' N/ e5 { - return -1;
. J6 _) ^( R" S6 H/ ] - }
* h& N, K4 [2 D! G -
5 p l+ c. L$ y6 o3 | - string processStr (buffer);/ q) Q& U% s# w! C6 r
- delete [] buffer;2 h/ [: `& u, _( T! b
- ( C M! i6 N) f; m/ u
- // DUMB DUMB DUMB....
P. n- ~0 U0 @. l - 2 S( b' p! g+ e
- int nextSearchPos = 0;
3 t# m+ C! v+ p4 ]+ h/ \+ V4 r* g - int tmpSearchPos[2] = {0};
6 a/ a# H' p c! _. f - int entriesCount = 0;
$ e% ~$ k$ \9 w- d/ l8 n! b - ( ?3 z! _1 `1 w; E8 |6 i: ?* D
- / e! M7 @ ^% s. |' @ f+ u
-
3 B/ N# E; G1 }' m9 E - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
7 R# U7 C# C3 V - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;' Y7 j+ _# O6 d
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);) M* {' s0 i: }" U$ n
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);: M3 m* l. g* U" ?5 G
-
" U# \+ ~! l9 y# g - if(tmpSearchPos[0]!=string::npos) {
7 Y( t# j" C/ T( k# t; s8 o3 ` - i=tmpSearchPos[0]+11;
& t0 t/ \; H U3 Y% S -
0 N; j/ U1 C. O - while(processStr[i]!='\n' && processStr[i]!='\r') {
@6 f4 S+ d9 Z- e - cout << processStr[i];
0 _+ S# K# \0 m1 k - i++;% j' Q6 D5 y5 Q: z) {3 o
- }
3 A5 I7 k/ h8 F: c. M - 1 W" r1 R' s; Q0 |! a5 J
- cout << endl;
6 L5 @2 J! {+ g& o" l9 R - J" H) a5 J, i- J/ A7 r
- }
3 s7 f0 {' D! N8 R Q" v. L% ? - 6 W4 x. e. z. ]
- if(tmpSearchPos[1]!=string::npos) {
. ]: l; v6 f6 O0 s8 q - i=tmpSearchPos[1]+13;$ [/ ^+ l$ E. g/ r- W, u0 ]4 h
-
5 a4 P/ ~# p; n: f: T% U1 { - while(processStr[i]!='\n' && processStr[i]!='\r') {
7 ?5 x: u4 q8 k' C$ H$ x5 Y2 h. e3 w - cout << processStr[i];
M0 N8 f7 ~, U* o6 p0 N - i++;
& E1 A0 ?$ |* z. a; ]' J; u - }
' r8 z8 z) I2 _ -
2 o) V- k* B' Z" K5 W - cout << endl;" w5 @: {9 ^. a& F
-
# B% O5 K7 Z6 U! i+ u - }8 h9 S9 t: U: {+ z+ a$ Y
-
, q" m6 h' a U! c - entriesCount++;
" V& E; H) z7 O% U8 T - }
2 l- p) b$ K9 M* W% O$ E. f) K5 b% S - ; l% w) P8 D8 t+ {2 C t
- cout<<entriesCount;3 r& d3 k2 Z( ~, o" ], f/ L
- return 0;1 f. D+ m3 `1 A
- ) }# d, T( E; O: p. w* }. q& ~" a
- }
复制代码 Update: 最后一行输出条目数.* J `1 h; [- [5 p- `4 L: X
, N t6 X! E ~; {( E( a- M[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|