|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "testcorpus.h" |
|
19 |
|
20 #include "cpixfstools.h" |
|
21 #include <sstream> |
|
22 |
|
23 using namespace std; |
|
24 |
|
25 LineTestCorpus::LineTestCorpus(const char* path) |
|
26 : items_() |
|
27 { |
|
28 FILE* ifs; |
|
29 ifs = fopen(path, "r"); |
|
30 if (ifs) |
|
31 { |
|
32 Cpt::FileSentry ifsSentry( ifs ); |
|
33 string |
|
34 line; |
|
35 |
|
36 while (Cpt::fgetline(ifs, line)) |
|
37 { |
|
38 wostringstream wline; |
|
39 for (int i = 0; i < line.length(); i++) { |
|
40 wline<<((wchar_t)line[i]); |
|
41 } |
|
42 items_.push_back(wline.str()); |
|
43 } |
|
44 } |
|
45 } |
|
46 |
|
47 LineTestCorpus::~LineTestCorpus() |
|
48 { |
|
49 } |
|
50 |
|
51 size_t LineTestCorpus::size() |
|
52 { |
|
53 return items_.size(); |
|
54 } |
|
55 |
|
56 |
|
57 std::wstring LineTestCorpus::item(size_t index) |
|
58 { |
|
59 return items_[index]; |
|
60 } |
|
61 |
|
62 std::map<std::string, LineTestCorpusRef::CorpusEntry>* LineTestCorpusRef::instances_ = NULL; |
|
63 |
|
64 LineTestCorpusRef::LineTestCorpusRef(const char* path) |
|
65 { |
|
66 if ( instances_ == NULL ) { |
|
67 instances_ = new std::map<std::string, LineTestCorpusRef::CorpusEntry>(); |
|
68 } |
|
69 |
|
70 this->path_ = path; |
|
71 if (instances_->count(path_) == 0) |
|
72 { |
|
73 (*instances_)[path_].first = 0; |
|
74 (*instances_)[path_].second = new LineTestCorpus(path); |
|
75 } |
|
76 (*instances_)[path_].first++; |
|
77 |
|
78 ref_ = (*instances_)[path_].second; |
|
79 } |
|
80 |
|
81 LineTestCorpusRef::~LineTestCorpusRef() |
|
82 { |
|
83 ref_ = NULL; |
|
84 (*instances_)[path_].first--; |
|
85 if ((*instances_)[path_].first == 0) |
|
86 { |
|
87 delete (*instances_)[path_].second; |
|
88 instances_->erase(path_); |
|
89 } |
|
90 |
|
91 if ( instances_->size() == 0 ) { |
|
92 delete instances_; |
|
93 instances_ = NULL; |
|
94 } |
|
95 } |
|
96 |
|
97 size_t LineTestCorpusRef::size() |
|
98 { |
|
99 return ref_->size(); |
|
100 } |
|
101 |
|
102 std::wstring LineTestCorpusRef::item(size_t index) |
|
103 { |
|
104 return ref_->item( index ); |
|
105 } |
|
106 |