- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
1 T6 P3 l, \( \9 ~) s+ O1 A
9 z: ^; l* G# Z- //
& |# J8 {, ^2 U& r3 e) W6 A1 V5 t - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.9 [0 w& [- c$ o* Z0 d
- // In case this is not legally possible,
8 }; g: l+ m, K# Z: | - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
# f! j3 |. }4 r$ C* ?4 f+ R - //
! p! m* w. @+ I% a# Y - ' N# N6 {( |+ A: R4 y- ^/ `8 {
- #include <iostream>
# D3 @ K& i3 |( { B3 ] - #include <fstream>! P! r0 b- ?3 j2 i( ?0 ^* b
- #include <cctype>4 R' E% V3 V1 c: D5 T- W; O8 p' j) u
- using namespace std;1 B6 H" V! k4 c1 O4 H( `
- 4 l( u: y& V! H
- // MACROS
2 S2 e d7 |& Q+ S2 L# F - #define SYSTEM_BLOCK_STR "[system]"! P- [5 B, \8 ~4 n$ F
- #define NICKNAME_STR "nickname = "9 i- N" B7 O3 M2 e! Z! ?
- #define IDS_STR "strid_name = "
- B' X8 A5 g2 G) x - //7 U9 C1 x0 P/ G% t
- " s" t: p7 u( W& Z2 \! h6 s( W
- ifstream inf;
7 a5 W s) R% U- l1 u; f" i - char *buffer;2 `' @; ^- A g1 q, {
- * v: o8 k9 H$ n
- int main(int argc, char *argv[]). A( d* x3 v T
- {
, f# L4 D' ~2 ?. {! L - int i;
3 h8 K6 p% v2 ]6 Z -
; t! ^! P% I8 M" X6 i - if(argc!=2){
' G9 |/ f X5 w( H" E6 ~# w - cout << "This program requires one and only one argument.";
! {4 C, g& N/ c! P4 \, H8 ^% M9 D - return -1;
" s0 [3 V" c3 v$ |& N - }
# j1 o5 S9 `! K* E9 W -
; x" I! b& ~8 T. H e, t* o - inf.open(argv[1], ios_base::in | ios_base::ate);
, C- ~1 W1 f# @- [) s" Z9 d - if(!inf) {
( f) ~' s9 } w - cout << "Error opening file.";
: O/ n1 X! P/ ^' K - return -1;
8 n) o' `9 y0 _. [& R; f* C - }/ \$ E$ V/ C) _ W4 i# ~
-
" K1 p/ {5 C( G) K% C G - int streamLength = inf.tellg();. f) c. W, x( \- X0 `5 \6 ^ |5 B2 k
- if(streamLength!=-1) {- D' O; f5 _) N7 a' z; D+ p1 g7 W
- inf.seekg(0, ios::beg);
2 J2 _0 P% ?" U - 0 W' r+ D$ q% k, E( ]- l; Z
- buffer = new char [streamLength];
8 F) `/ w, C6 |/ Y& ?" h - // Read file;
" z2 N& }6 ]0 x9 `) r- X - for(i=0;i<streamLength;i++) {$ d1 |5 E0 y; `4 Y
- inf.get(buffer[i]);3 e; f: _# w- M' X% {, ? t
- buffer[i] = tolower(buffer[i]);
6 I! b1 @! n5 r* l3 E2 d9 R - }
3 j* E9 a: n4 p8 v2 _ - 7 d3 \% h1 f) \6 }& |/ |* b% W
- /* For testing
5 U2 R% _" l B2 y' W - for(i=0;i<streamLength;i++) {
$ Z+ }1 s: q$ G8 w& _ - cout << buffer[i];
5 F2 u3 ~$ R0 S - }
9 ]2 f# P; @/ W" N* Y - */9 o0 Y" O& l2 E, _
- }
+ ~/ X, p7 E! x# C0 _2 n' P' _ - else {
" n* [( z8 m: t! ^ - cout << "Failed to read file." << endl;
$ a8 s( {4 K! ^# A; T: k - return -1;
; m8 U$ X& q0 s) {5 n: L - }
. b( Y. s$ B) N; ? - ) l* J9 Q3 r, H% f" F
- string processStr (buffer);
% Z5 U. Y( r4 ~6 {! T - delete [] buffer;; d, Q! B3 o) c5 W8 \
- 2 p/ C, e! b# W0 A
- // DUMB DUMB DUMB....
5 k# Z1 e7 D$ [" o) ]% F# q - 9 T* ]$ J+ @: K
- int nextSearchPos = 0;9 p# K( g/ h9 Z8 F( n( \, D
- int tmpSearchPos[2] = {0};
2 F! G5 y5 V) [" o' w: e6 z - int entriesCount = 0;
. c6 }% u2 ]1 s4 W% n! @ -
}' K# c) t+ e -
4 S* e( U3 q. `" d, O- i& B - 6 K" [- |! t9 |# P) a& F) n
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
4 Z) u. s. _1 w, _* ]) B% \$ c" K - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;( }8 _3 f$ o! @3 H) @" S& @
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);" k) S, l# d8 L5 t x
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);2 |, q% ~- x. V: m* D( z
-
+ T/ Q# J* c; ]1 F2 K2 ] - if(tmpSearchPos[0]!=string::npos) {
9 b+ s3 c, N( R* J( J) c: ~" H - i=tmpSearchPos[0]+11;
: g9 l) L, B# U8 C8 p - ( K! f" k: k, i5 m- G' k
- while(processStr[i]!='\n' && processStr[i]!='\r') {% S) J# K- r5 B; {$ ]
- cout << processStr[i];' `3 S- ^1 A6 z' h# ^% r: d4 K
- i++;
/ L% T1 R' |! A7 G2 [& b) ^ - }
' ?$ H. n: z6 L# o - 6 I5 D- H3 k: c4 p |
- cout << endl;
% D! e% N; o- k6 _ - . N' u7 f, W1 \ W
- }" [$ E5 _/ u* {, E, d B
-
& M9 q- e0 U4 z6 s - if(tmpSearchPos[1]!=string::npos) {
8 V) {+ G7 s1 E4 X - i=tmpSearchPos[1]+13;- P/ M3 A' g4 U
- 2 o: Q1 j6 z: k: } p8 G5 v( _ a% Z
- while(processStr[i]!='\n' && processStr[i]!='\r') {
I. |: j, y$ c- j: M - cout << processStr[i];
0 @! c5 q4 f2 Q6 d# X - i++;6 _2 \$ h9 [: B' H
- }
9 @" K/ \/ v- ?# D1 _( l% U, F - $ R& | i$ b5 W1 U3 n
- cout << endl;- r9 B3 T6 @8 L; d$ u! Z6 }' a( L9 E
- 7 a) s- z" M7 z4 M0 d8 P: \# b; @
- }' k+ F# ^8 p) x2 C1 c E
- 4 j w4 [6 l8 \- l$ ^; C- J' X
- entriesCount++;
! w' a/ j5 Y7 @* z# r5 a& K- q - }
, ~( ~1 Z G0 O1 v' r. Z - 3 B- P' Q5 V% ]5 ~1 ^: X
- cout<<entriesCount;' ?; K3 w- @- \. B9 d* }9 C% C
- return 0;7 G6 C: U7 Q- h" s8 y
-
9 u& \' l% _- [0 Y* a: a" d - }
复制代码 Update: 最后一行输出条目数." _4 S3 c: }1 I' y: P
: C. |2 L3 f& w7 q: O# } e6 u[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|