- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis9 f! o% q1 F) U4 _! @- c" ~, Y
) h. \* f1 T, v. A- //
- E7 L. ` l! K( r# j. A8 y7 @' V - // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.. H: J" s: f3 p. z: x7 ^/ m
- // In case this is not legally possible,
0 H3 u( x* s1 C1 E; \ - // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
* X5 l! s( ^" F! `- r - //
7 a) ?! K& W& e' T3 _2 c - 2 b- ?* p9 H6 Z( S. i/ t' o! ^( B
- #include <iostream>
5 B# g% g2 r) j2 I5 ^' ?: B - #include <fstream>
" z0 r7 [5 D; N2 ~6 n - #include <cctype>
0 z- x6 |. }1 J8 A - using namespace std;
$ O" a/ @' N/ N) ^" t
* J( |& y8 p( T- // MACROS$ s6 z0 i& w2 l
- #define SYSTEM_BLOCK_STR "[system]"
; W, }# V+ _9 ]) r - #define NICKNAME_STR "nickname = "
8 ^# k- z' W; N6 I. ]" L( W - #define IDS_STR "strid_name = "
; V: T; z1 ~. {! r( ?3 g6 \' R - //
* v% j* s( A$ U3 o9 \, J
4 L8 x! o s% O4 U- ifstream inf;, t# T0 C- p: W- o/ j. Q1 |
- char *buffer;% t$ g l, L' h e m! n
- - h V. t3 r! x8 J/ \" z
- int main(int argc, char *argv[])
! }4 n" {8 I w) g; A - {9 J( i0 ` o3 p1 G/ h
- int i;3 L1 Y8 a% J/ `+ q( b
- ! e; G- Q1 C& }; J. F+ k1 G3 o
- if(argc!=2){
5 Z/ J$ p9 p+ x [! c6 J - cout << "This program requires one and only one argument.";
. c9 ?9 a6 D1 _. m4 C" t8 z z - return -1;. G7 i/ `9 M, K8 ~0 @) d' q
- }
- p/ l0 ^. Y& N. @ -
3 ^+ P& u$ U6 c1 m. `2 W - inf.open(argv[1], ios_base::in | ios_base::ate);' t( y9 H0 B9 W+ Z. b
- if(!inf) {, j( C# \0 C# t! ^
- cout << "Error opening file.";
) n" U( w" a& \( T( {2 `2 F - return -1;
' b/ m& p- I* F9 j+ I) f - }! J0 N3 X" P# @- U2 Z: B
- 7 q$ U% l$ X' s* Z! c
- int streamLength = inf.tellg();
2 i U6 X5 |' t4 y! g - if(streamLength!=-1) {# b, u) y- D7 h6 H Z" p( G& ?
- inf.seekg(0, ios::beg);9 {7 U, @! K Y2 N
- 3 ?+ _3 U% g! g- v% H
- buffer = new char [streamLength];: E6 d6 M7 m: Y
- // Read file;6 d: Z2 Z* q: m! Q. e. c& H
- for(i=0;i<streamLength;i++) { h) Z$ d& N- x- @- P- ~
- inf.get(buffer[i]);) [* t' C7 {" E4 L/ y2 ^
- buffer[i] = tolower(buffer[i]);
: P% q8 Z% Z9 q - }
7 v, S- V. n# k) R - ; H$ X/ V6 h$ P9 h \' q! \3 \
- /* For testing- L* \4 j) A/ }, S0 ^
- for(i=0;i<streamLength;i++) {
4 ]" ^* Q- ^! u ~& J: { - cout << buffer[i];' M' g7 ]. i& F2 e- ]6 K5 `
- }
- T# S1 z6 [8 J3 _- J2 U4 E, y - */& |$ R0 h) {3 m c5 J- }' `1 N# P
- }
6 w3 _# M H1 X' H! W. d% R/ ` - else {* ?! G7 V5 ^2 i) j: p% }
- cout << "Failed to read file." << endl;
# b* D& h. z3 g# {7 ~' f8 ^0 b0 P - return -1;
! [1 c5 k& h& x" R7 v - }/ f7 R* P/ ]3 g% J' n' J& [
- . V3 K; w3 \% o: |: r0 a; e
- string processStr (buffer);+ Y$ x2 [& H, ?& Z4 L
- delete [] buffer;
9 }% o% X" W$ _0 y8 z% i - 0 m$ @: S# O1 C* g/ o" ]) _# K
- // DUMB DUMB DUMB....& J4 b# j& F; K; A% Z
-
' h# ?, t, U6 a6 }9 A - int nextSearchPos = 0; U2 T1 B: Y* s7 {
- int tmpSearchPos[2] = {0};: Z7 u1 @# ]! a' x' L% K! q
- int entriesCount = 0;* y; J" R8 ?! V6 ^# o
-
2 c8 U8 ?5 D2 V" p" v# ~2 d -
5 u: o9 x2 @/ U0 b: Q) t# c. x -
/ Y' @1 O% x3 A3 {3 ]! t - while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {
3 {! p6 B0 E6 C, {0 @$ v* k - nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8;
+ X. V0 w0 L+ J4 P, X6 k- J- b - tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);0 \$ u/ ~7 n8 A6 h, q
- tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);
, t- ^( P) Z8 ] -
5 }1 |# l" S0 O V - if(tmpSearchPos[0]!=string::npos) {
7 i- N9 Q; x. T/ V; X7 _ - i=tmpSearchPos[0]+11;
! n4 o$ Z5 p7 D: i - - [* j/ D E, q( {( g4 k# k0 x: s. V
- while(processStr[i]!='\n' && processStr[i]!='\r') {
9 c# o( x- V, c. P2 j. X ] - cout << processStr[i];
# f, Q: c" y' S. Y8 Z0 n - i++;
$ r+ `, S1 ?) \7 w; A! S - }$ y6 x% x+ ^; d) ^! _* L
-
, z4 s$ l6 w; B G - cout << endl;
- l. |% b2 t, I -
6 X% A; ?; @' D" B - }
( o1 W: G: G3 x& B -
# d) x# _( C- b3 L0 j - if(tmpSearchPos[1]!=string::npos) {, r: L& e$ I$ U
- i=tmpSearchPos[1]+13;
+ k, G# w: O% s- Q -
4 v8 V3 f! m5 y) V1 t; ] - while(processStr[i]!='\n' && processStr[i]!='\r') {7 L# @/ ]$ Q; @- L/ x
- cout << processStr[i];8 G$ ?- Y( m. f) A. }
- i++;; p! o6 T, {5 G
- }
$ Q$ r" q8 c8 |( b - / W9 o/ o `7 d- H' r5 p/ J3 G0 r
- cout << endl;
; L# a& S5 [+ e+ r - ; J% _6 u8 d, x$ f4 J- v
- }
2 H, w9 S5 B( h! J6 e -
, q3 }1 ?- M8 k3 s* Z - entriesCount++;
w; G; X6 r# h/ |& V2 e) b - }
9 n; T% `! K" r7 }; D2 K- g - 0 k/ q2 B( x5 ^6 V* c) G- d
- cout<<entriesCount;
" N0 ]* j( u4 Q( w1 R- [& [ - return 0;
9 J* t1 W0 U/ z9 E$ c D' ~; |0 [ -
+ N. G5 d9 m5 P - }
复制代码 Update: 最后一行输出条目数.' h0 H+ R6 h/ K4 ?" n$ p% m5 O! @
, y& k& c% C0 Y: n8 z" @
[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|