]> git.saurik.com Git - apt.git/blob - test/scratch.cc
Bugs
[apt.git] / test / scratch.cc
1 #include <apt-pkg/acquire-item.h>
2 #include <apt-pkg/acquire-worker.h>
3 #include <apt-pkg/init.h>
4 #include <apt-pkg/error.h>
5 #include <strutl.h>
6
7 #include <signal.h>
8 #include <stdio.h>
9
10 class AcqTextStatus : public pkgAcquireStatus
11 {
12 unsigned int ScreenWidth;
13 char BlankLine[300];
14 unsigned long ID;
15
16 public:
17
18 virtual void IMSHit(pkgAcquire::ItemDesc &Itm);
19 virtual void Fetch(pkgAcquire::ItemDesc &Itm);
20 virtual void Done(pkgAcquire::ItemDesc &Itm);
21 virtual void Fail(pkgAcquire::ItemDesc &Itm);
22 virtual void Start() {pkgAcquireStatus::Start(); BlankLine[0] = 0; ID = 1;};
23 virtual void Stop();
24
25 void Pulse(pkgAcquire *Owner);
26 };
27
28 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
29 // ---------------------------------------------------------------------
30 /* */
31 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
32 {
33 cout << '\r' << BlankLine << '\r';
34 cout << "Hit " << Itm.Description;
35 if (Itm.Owner->FileSize != 0)
36 cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
37 cout << endl;
38 Update = true;
39 };
40 /*}}}*/
41 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
42 // ---------------------------------------------------------------------
43 /* This prints out the short description and the expected size */
44 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
45 {
46 Update = true;
47 if (Itm.Owner->Complete == true)
48 return;
49
50 Itm.Owner->ID = ID++;
51
52 cout << '\r' << BlankLine << '\r';
53 cout << hex << Itm.Owner->ID << dec << " Get " << Itm.Description;
54 if (Itm.Owner->FileSize != 0)
55 cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
56 cout << endl;
57 };
58 /*}}}*/
59 // AcqTextStatus::Done - Completed a download /*{{{*/
60 // ---------------------------------------------------------------------
61 /* We don't display anything... */
62 void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
63 {
64 Update = true;
65 };
66 /*}}}*/
67 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
68 // ---------------------------------------------------------------------
69 /* We print out the error text */
70 void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
71 {
72 cout << '\r' << BlankLine << '\r';
73 cout << "Err " << Itm.Description << endl;
74 cout << " " << Itm.Owner->ErrorText << endl;
75 Update = true;
76 };
77 /*}}}*/
78 // AcqTextStatus::Stop - Finished downloading /*{{{*/
79 // ---------------------------------------------------------------------
80 /* This prints out the bytes downloaded and the overall average line
81 speed */
82 void AcqTextStatus::Stop()
83 {
84 pkgAcquireStatus::Stop();
85 cout << '\r' << BlankLine << '\r';
86
87 if (FetchedBytes == 0)
88 cout << flush;
89 else
90 cout << "Fetched " << SizeToStr(FetchedBytes) << " in " <<
91 TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
92 "/s)" << endl;
93 }
94 /*}}}*/
95 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
96 // ---------------------------------------------------------------------
97 /* This draws the current progress. Each line has an overall percent
98 meter and a per active item status meter along with an overall
99 bandwidth and ETA indicator. */
100 void AcqTextStatus::Pulse(pkgAcquire *Owner)
101 {
102 pkgAcquireStatus::Pulse(Owner);
103
104 enum {Long = 0,Medium,Short} Mode = Long;
105
106 ScreenWidth = 78;
107 char Buffer[300];
108 char *End = Buffer + sizeof(Buffer);
109 char *S = Buffer;
110
111 // Put in the percent done
112 sprintf(S,"%ld%%",long(double(CurrentBytes*100.0)/double(TotalBytes)));
113
114 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
115 I = Owner->WorkerStep(I))
116 {
117 S += strlen(S);
118
119 // There is no item running
120 if (I->CurrentItem == 0)
121 {
122 if (I->Status.empty() == false)
123 snprintf(S,End-S," [%s]",I->Status.c_str());
124 continue;
125 }
126
127 // Add in the short description
128 if (I->CurrentItem->Owner->ID != 0)
129 snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID,
130 I->CurrentItem->ShortDesc.c_str());
131 else
132 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
133 S += strlen(S);
134
135 // Show the short mode string
136 if (I->CurrentItem->Owner->Mode != 0)
137 {
138 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
139 S += strlen(S);
140 }
141
142 // Add the current progress
143 if (Mode == Long)
144 snprintf(S,End-S," %u",I->CurrentSize);
145 else
146 {
147 if (Mode == Medium || I->TotalSize == 0)
148 snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
149 }
150 S += strlen(S);
151
152 // Add the total size and percent
153 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
154 {
155 if (Mode == Long)
156 snprintf(S,End-S,"/%u %u%%",I->TotalSize,
157 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
158 else
159 {
160 if (Mode == Medium)
161 snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
162 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
163 else
164 snprintf(S,End-S," %u%%",
165 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
166 }
167 }
168 S += strlen(S);
169 snprintf(S,End-S,"]");
170 }
171
172 // Put in the ETA and cps meter
173 if (CurrentCPS != 0)
174 {
175 char Tmp[300];
176 unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
177 sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
178 unsigned int Len = strlen(Buffer);
179 unsigned int LenT = strlen(Tmp);
180 if (Len + LenT < ScreenWidth)
181 {
182 memset(Buffer + Len,' ',ScreenWidth - Len);
183 strcpy(Buffer + ScreenWidth - LenT,Tmp);
184 }
185 }
186 Buffer[ScreenWidth] = 0;
187
188 // Draw the current status
189 if (strlen(Buffer) == strlen(BlankLine))
190 cout << '\r' << Buffer << flush;
191 else
192 cout << '\r' << BlankLine << '\r' << Buffer << flush;
193 memset(BlankLine,' ',strlen(Buffer));
194 BlankLine[strlen(Buffer)] = 0;
195
196 Update = false;
197 }
198 /*}}}*/
199
200 int main(int argc,char *argv[])
201 {
202 signal(SIGPIPE,SIG_IGN);
203
204 /* URI Foo(argv[1]);
205 cout << Foo.Access << '\'' << endl;
206 cout << Foo.Host << '\'' << endl;
207 cout << Foo.Path << '\'' << endl;
208 cout << Foo.User << '\'' << endl;
209 cout << Foo.Password << '\'' << endl;
210 cout << Foo.Port << endl;
211
212 return 0;*/
213
214 pkgInitialize(*_config);
215
216 pkgSourceList List;
217 AcqTextStatus Stat;
218 pkgAcquire Fetcher(&Stat);
219 List.ReadMainList();
220
221 pkgSourceList::const_iterator I;
222 for (I = List.begin(); I != List.end(); I++)
223 {
224 new pkgAcqIndex(&Fetcher,I);
225 if (_error->PendingError() == true)
226 break;
227 }
228
229 Fetcher.Run();
230
231 _error->DumpErrors();
232 }