- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis& p/ ^0 q' ~! n$ s3 n) d. ?
4 I/ Y& a4 B* d- C- //
8 q* h' T0 b: ?* G$ x/ e - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
- F i4 G; }2 A) a% K5 G l9 ] - // In case this is not legally possible,
# ^! g- r# \: R, ]' G6 x8 a - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law./ o3 M Y' X5 J( \
- //
0 q `8 ]# q5 X, Q
& d2 u- @& P, i% Z8 w3 ?- #include <iostream>6 h+ H6 I- r/ L3 I. J% n
- #include <fstream>
2 V7 x1 A N# f1 _& U - #include <cctype>
. o/ Q& @0 r, _3 `9 ?/ p - using namespace std;
$ N5 W T% q/ ^, X' y( R - 9 I# a- d( G4 | @8 j+ V
- // MACROS
, o9 F! z" c, f - #define SYSTEM_BLOCK_STR "[system]"
; w7 J; L/ h6 R - #define NICKNAME_STR "nickname = "
4 }2 T- p3 W+ M7 o - #define IDS_STR "strid_name = "* t, r. u1 Z. ^1 [
- //
4 }1 c. b" U7 w2 m# U5 Y" y - 9 q, ]1 e; {. G4 \0 s _
- ifstream inf;
* R1 K" w/ f& d3 | - char *buffer;6 ?' x0 w4 X6 U0 B( C
- Y, {% }( Q) J' H
- int main(int argc, char *argv[])' l& X' Q6 x( p. C8 [/ |
- {
! B) J" E0 U6 ?( R& U- B - int i;
1 x: B' m/ R6 j& h1 }/ p* F( U -
8 H: m( e0 C; o: \ - if(argc!=2){ , L! }! e1 m, h Q6 g0 e' w
- cout << "This program requires one and only one argument.";
/ S( ?/ A, S- p5 b0 t3 F: n - return -1;3 a5 j( \5 s! T0 \) I
- }, g- Z7 G# p! r7 u0 a
-
; w" C, a* i s: |* S8 b9 C$ O. w - inf.open(argv[1], ios_base::in | ios_base::ate);5 j' C M J- z
- if(!inf) {+ l$ I1 ?. I! G, f
- cout << "Error opening file.";
1 B: C6 J$ ^6 H5 `, m! I - return -1;. v- O" Y# E3 k7 f: ^
- }
! F$ |7 @5 v. j+ o) _ - 3 x2 ^) g6 k% i l- Q
- int streamLength = inf.tellg();+ c, P- L, I9 U7 a; ?% d
- if(streamLength!=-1) {
3 P* w* w7 b+ s4 h/ Z2 ~2 j - inf.seekg(0, ios::beg);+ A; @$ u, l# @, k: n( _1 ]
-
" @5 O$ |. |2 u& R2 G' S% I0 Q - buffer = new char [streamLength];
) `* }7 ?$ _( _6 f, \6 {8 ?# O - // Read file;
$ E; ]9 w' L; t1 q2 N - for(i=0;i<streamLength;i++) {0 V- T5 X. ~( F! w9 e! }7 o/ {
- inf.get(buffer[i]);7 i+ f/ R5 Z7 f5 U2 q2 o9 C1 ~- b
- buffer[i] = tolower(buffer[i]);
s( h4 v s# j - }& d0 s1 U" N. h: s( E, ^
-
* o4 `/ p" u. F: h8 W, } - /* For testing2 x7 |% K' D& t& u+ _( T
- for(i=0;i<streamLength;i++) {# b4 L2 c! N# _
- cout << buffer[i];
$ Z. W3 j! q* Z! A - }/ f+ [# x& }6 `4 {" a r/ ]; J
- */
9 W5 ]5 f$ Q6 y9 o' N# d - }: [' B! i) g, C" M ~$ X' c
- else {8 i8 @" n! Q+ l7 W( ~
- cout << "Failed to read file." << endl;$ F) O, |7 H) u# _! u0 B- |
- return -1;* W6 A; W6 D/ C8 X
- }% q6 K8 D/ ?$ x6 Y0 a% Y
- 4 D8 l) F( i/ r2 ~
- string processStr (buffer);
, Y. o/ @8 p- [: L - delete [] buffer;
) V+ V5 O) d9 H$ |+ H - . G: P2 m2 X8 s- P" ~
- // DUMB DUMB DUMB....
" K" ?# c1 y" o, I& q# |$ G9 i -
8 ~% U! o V4 ]' U4 P L4 D - int nextSearchPos = 0;9 a q; i# k {1 P2 P( _) D
- int tmpSearchPos[2] = {0};* {% p" E* j! H* h
- int entriesCount = 0;0 X1 F, v5 _' S D' @
- , ^8 |3 @* g5 {# c: U
- 1 |/ C( [+ m8 O, g9 K7 o
- + D6 a8 q# A- @) o
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {" H- [, a3 G# P( R, i
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
+ _$ \( Q7 z! F [, e - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
$ ^/ x# s, ~1 w% F - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);! m% L' ~% o, }
- # [+ `& p" p% b7 s, y( f
- if(tmpSearchPos[0]!=string::npos) {
$ x( E+ B& @3 ^3 @. S2 q: W - i=tmpSearchPos[0]+11;
+ F2 ], a0 y9 S9 t. c5 m2 f6 Q' X -
7 `7 e9 v+ G/ H3 t2 f4 K4 F - while(processStr[i]!='\n' && processStr[i]!='\r') {$ F- f* @+ O+ ?% @8 U; m
- cout << processStr[i];
- G! H$ ]6 N+ x6 D - i++;
5 T$ H' z% t6 J1 A - }
" l- Q$ M- U a' ?8 R$ l6 } -
. h! q1 K# g9 H - cout << endl;
' j: E; z4 j- q/ x3 u& @ -
4 B' m) Y6 r) M- K! v, g - }
( C+ I2 M, t/ f. P' p" C9 V6 V - . X/ B' G2 q( F# [
- if(tmpSearchPos[1]!=string::npos) {
3 h) _# m0 g; d9 a - i=tmpSearchPos[1]+13; ?, j! |+ C- q
-
4 U7 \0 j% E+ A - while(processStr[i]!='\n' && processStr[i]!='\r') {; m8 G# M& J3 J2 B" C# o
- cout << processStr[i];5 E9 K7 Z. i* @9 g3 n- P7 Y# o
- i++;
! z4 V+ D5 j* e& Y; d0 ] ^ - }" L5 s {( q; X# m* P( N
- 2 y! R: f3 e" l1 j, U
- cout << endl;4 Q; q5 K/ F! X- t
- - ?0 v7 F2 ~/ A5 r# H
- }: y) y% ` x% [ e" ~, g* ~& h3 `
-
9 }# ?4 C- m5 W/ }7 [ - entriesCount++;: d" y2 w0 U# W5 k1 y* w+ v
- }
4 W0 a' O' F9 M1 Y -
6 H9 E$ ^8 @$ H0 w* C - cout<<entriesCount;
- L# r( z' C( ^) ^9 \* S9 I7 V# I - return 0;3 H/ a2 U; f t ^6 ]
- ( F$ Z9 ?1 m. B3 t# H
- }
复制代码 Update: 最后一行输出条目数.- w" H# C# y: y4 C
' A7 i! @1 p$ U G: G) F
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|