summaryrefslogtreecommitdiff
path: root/apps/JAWS/clients/Caching/Local_Locator.cpp
blob: 67b4ca8b60615f63e986da412641c696bf4c2fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// $Id$

#if !defined (ACE_LOCAL_LOCATOR_C)
#define ACE_LOCAL_LOCATOR_C

#include "Local_Locator.h"

#if !defined (__ACE_INLINE__)
#include "Local_Locator.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(Caching, Local_Locator, "$Id$")

int
ACE_URL_Local_Locator::url_query (const ACE_URL_Locator::ACE_Selection_Criteria how,
				  const ACE_URL_Property_Seq *pseq,
				  const size_t how_many,
				  size_t &num_query,
				  ACE_URL_Offer_Seq *offer)
{
  ACE_URL_Record *item = 0;

  ACE_NEW_RETURN (offer, ACE_URL_Offer_Seq (how_many), -1);
  
  if (how >= ACE_URL_Locator::INVALID_SELECTION)
    {
      errno = ACE_URL_Locator::INVALID_ARGUMENT;
      return -1;
    }
  
  num_query = 0;
  for (ACE_Unbounded_Set_Iterator<ACE_URL_Record> iter (this->repository_);
       iter.next (item) != 0;
       iter.advance ())
    {
      size_t i_query;
      size_t i_db;
      int found = 0;

      // Now this is a stupid implementation.  Perhaps we can implement this
      // using Hash_Map.  Better yet, I think we should put this in a database
      // and put SQL query here.
      for (i_query = 0; found == 0 && i_query < pseq->size (); i_query++)
	for (i_db = 0; i_db < item->offer_->url_properties ().size (); i_db++)
	  {
	    if ((*pseq)[i_query].name () == item->offer_->url_properties ()[i_db].name ())
	      if (how == ACE_URL_Locator::SOME)
		{
		  

	  }

      // if match and Some, copy to <offer>, inc <num_query>, advance iterator

      // else if All, advance iterator

      // else if None, check next property in <pseq>.

      if (all properties checked and found and ALL)
	copy to <offer>; inc <num_query>;
      else if (all properties checked and not found and NONE)
	copy to <offer>; inc <num_query>;
      else
	shouldn't happen, internal error

      if (num_query == how_many)
	break;
    }

  return 0;
}

int
ACE_URL_Local_Locator::export_offer (ACE_URL_Offer *offer,
				     ACE_WString &offer_id)
{
  ACE_URL_Record *item = 0;

  // First check if we have registered this URL already.
  for (ACE_Unbounded_Set_Iterator<ACE_URL_Record> iter (this->repository_);
       iter.next (item) != 0; 
       iter.advance ())
    if (*item->offer_->url () == *offer->url ())
      {
	errno = ACE_URL_Locator::OFFER_EXIST;
	return -1;
      }

  ACE_URL_Record *new_offer;

  // Offer is not in repository, we can add new one in safely.
  ACE_NEW_RETURN (new_offer, ACE_URL_Record (offer),
		  ACE_URL_Locator::NOMEM);

  this->repository_.push (*new_offer);

  offer_id = *new_offer->id_;
  return 0;
}

int
ACE_URL_Local_Locator::withdraw_offer (const ACE_WString &offer_id)
{
  ACE_URL_Record *item = 0;
  
  // Iterate thru repository and remove offer with <offer_id>.
  for (ACE_Unbounded_Set_Iterator<ACE_URL_Record> iter (this->repository_);
       iter.next (item) != 0;
       iter.advance ())
      if (offer_id == *item->id_)
	{
	  if (this->repository_.remove (*item) == 0)
	    return 0
	  else
	    {
	      errno = ACE_URL_Locator::UNKNOWN;
	      return -1;
	    }
	}

  errno = ACE_URL_Locator::NO_SUCH_OFFER;
  return 0;
}

int
ACE_URL_Local_Locator::describe_offer (const ACE_WString &offer_id,
				       ACE_URL_Offer *offer)
{
  ACE_URL_Record *item = 0;
  
  // Iterate thru the repository and produce a copy of offer's
  // description.
  for (ACE_Unbounded_Set_Iterator<ACE_URL_Record> iter (this->repository_);
       iter.next (item) != 0;
       iter.advance ())
    if (offer_id == *item->id_)
      {
	ACE_NEW_RETURN (offer, ACE_URL_Offer (*item->offer_), -1);
	return 0;
      }

  errno = ACE_URL_Locator::NO_SUCH_OFFER;
  return -1;
}

int
ACE_URL_Local_Locator::modify_offer (const ACE_WString &offer_id,
				     const ACE_WString *url,
				     const ACE_URL_Property_Seq *del,
				     const ACE_URL_Property_Seq *modify)
{
  ACE_Unbounded_Set_Iterator<ACE_URL_Record> iter (this->repository_);
  ACE_URL_Record *item = 0;
  ACE_URL_Record *target = 0;

  // Errno Checking

  for (; iter.next (item) != 0; iter.advance ())
    {
      if (url != 0 && *url == item->offer_->url ())
	{
	  errno = ACE_URL_Locator::OFFER_EXIST;
	  return -1;
	}
      if (offer_id == *item->id_)
	target = item;
    }	  

  if (target != 0)		// Aha, we found a target to work on
    {
      if (del != 0 && modify != 0)
	{
	  // We need to make a copy of the original property sequence
	  // so if any error occurs, we can revert our change easily.
	  
	  // First we need to calculate the maximum number of perperties.
	  int psize = target->offer_->url_properties ().size ();
	  if (del != 0)
	    if ((psize -= del->size ()) < 0)
	      {
		// If you try to delete more properties than we have,
		// you are doomed.  No need to proceed.
		errno = ACE_URL_Locator::INVALID_ARGUMENT;
		return -1;
	      }
	  if (modify != 0)
	    // In the worst case, all properties in <modify> will be added.
	    psize += modify->size ();

	  // Now, create a temporary work space.
	  ACE_URL_Property_Seq working (psize);
	  size_t sz = 0;
	  for (; sz < item->offer_->url_properties ().size ())
	    working[sz] = item->offer_->url_properties() [sz];

	  if (del != 0)
	    {
	      // Argh, this is really a stupid design.
	      // Go thru every property we want to delete
	      for (size_t i = 0; i < del->size () && sz > 0; i++)
		// For earch, go thru our property sequence and 
		// search for the property.
		for (size_t j = 0; j < sz; j++)
		  if ((*del)[i].name () == working[j].name ())
		    {
		      sz -= 1;
		      working[j] = working[sz]; // pack the array.
		      break;
		    }
	      // Doesn't generate error when we want to delete an
	      // imaginary property.  Is this appropriate?
	    }

	  if (modify != 0)
	    {
	      // This is also stupid.
	      // Go thru every property we want to modify/add
	      for (size_t i = 0; i < modify->size () && sz > 0; i++)
		{
		  // For each property, go thru our property list
		  // and search for the matching property
		  for (size_t j = 0; j < sz; j++)
		    if ((*modify)[i].name () == working[j].name ())
		      {
			// A match found.
			working[j].value ((*modify)[i].value ().fast_rep ());
			break;
		      }

		  // No matching property name were found,
		  // We want to add this property into the list.
		  if (j == sz)
		      working[sz++] = (*modify)[i];
		}
	    }
	}

      // Yes, all operations passed.  We can now copy the working version back.
      item->offer_->url_properties (ACE_URL_Property_Seq (sz));
      for (size_t i = 0; i < sz; i ++)
	item->offer_->url_properties ()[i] = working[i];

      if (url != 0)
	item->offer_->url (url->fast_rep ()); // replace URL location.
      return 0;
    }
  errno = ACE_URL_Locator::NO_SUCH_OFFER;
  return -1;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Unbounded_Set<ACE_URL_Record>;
template class ACE_Unbounded_Set_Iterator<ACE_URL_Record>;
template class ACE_Node<ACE_URL_Record>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Unbounded_Set<ACE_URL_Record>
#pragma instantiate ACE_Unbounded_Set_Iterator<ACE_URL_Record>
#pragma instantiate ACE_Node<ACE_URL_Record>
#endif /*ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#endif /* ACE_LOCAL_LOCATOR_C */