This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] Setup: Display mirrors sorted by location


Hi,

The following patch will display the location
info beside the download site.  e.g.
North America, Virginia  ftp://mirrors.rcn.net
North America, Virginia  http://mirrors.rcn.net
North America, Wisconsin  ftp://sourceware.mirrors.tds.net

Please let me know your comments.

Notes on the changes
* The earlier setup sorts sites based on url.
  This patch sorts on area, location too.
* The non-official mirrors will not show the location information.
  They will be shown together at the bottom of the list.
  This could act as a cue, to the user, that it is a non-official mirror.
* area, location are new members of the class.
* The 'key' for site sorting is area + location + url
* Two sites are same if the URLs are same (area doesn't matter).
  last-mirror doesn't store the area info.
* The existing code tries to handle .com/.edu/.org (sort together)
  but I don't think that it is actually effective.
* I've added String.find(char, pos) method in String++.
  string.find(c, pos) handles negative pos better.
  substr() is better too.
  I'm confused about string vs String.  Can't we just use std::string?
  Maybe I should lookup the archives for the history of String++.
* I haven't integrated Bas van Gompel's changes to site.cc

Regards,
rb

--- site.cc.old 2005-10-14 09:53:14.000000000 +0530
+++ site.cc     2005-11-09 16:03:36.854172000 +0530
@@ -99,47 +99,24 @@
 }

 void
-site_list_type::init (String const &newurl)
+site_list_type::init (String const &newsite)
 {
-  url = newurl;
-
-  char *dots = new_cstr_char_array (newurl);
-  char *dot = strchr (dots, '.');
-  if (dot)
-      {
-         dot = strchr (dot, '/');
-        if (dot)
-               *dot = 0;
-   }
-  displayed_url = String (dots);
-
-
-  dot = dots + strlen (dots);
-  char *dpsave, *dp = new char[2 * newurl.size() + 3];
-  dpsave = dp;
-  while (dot != dots)
-    {
-      if (*dot == '.' || *dot == '/')
-       {
-         char *sp;
-         if (dot[3] == 0)
-           *dp++ = '~';        /* sort .com/.edu/.org together */
-         for (sp = dot + 1; *sp && *sp != '.' && *sp != '/';)
-           *dp++ = *sp++;
-         *dp++ = ' ';
-       }
-      --dot;
-    }
-  *dp++ = ' ';
-  strcpy (dp, dots);
-  delete[] dots;
-  key = String (dp);
-  delete[] dpsave;
+  parseSite(newsite);
+  int slash = url.find('/', url.find(':') + 2); // "http://site/a/b/c";
+  String site = String(url.substr(0, slash - 1));
+  if (area.size() > 0) {
+    String loc = area + ", " + location + "   ";
+    displayed_url = loc + site;     // Display location info also
+    key = area + location + url;    // Area; Location; URL
+  } else {
+    displayed_url = site;           // No location info
+    key = String("~~") + url;       // "~" prefix to keep the URL at the end
+  }
 }

-site_list_type::site_list_type (String const &newurl)
+site_list_type::site_list_type (String const &newsite)
 {
-  init (newurl);
+  init (newsite);
 }

 site_list_type::site_list_type (site_list_type const &rhs)
@@ -147,6 +124,8 @@
   key = rhs.key;
   url = rhs.url;
   displayed_url = rhs.displayed_url;
+  area = rhs.area;
+  location = rhs.location;
 }   
   
 site_list_type &
@@ -155,19 +134,45 @@
   key = rhs.key;
   url = rhs.url;
   displayed_url = rhs.displayed_url;
+  area = rhs.area;
+  location = rhs.location;
   return *this;
 } 
   
 bool
 site_list_type::operator == (site_list_type const &rhs) const
 {
-  return key.casecompare (rhs.key) == 0;
+  // Two sites are same if the URLs are same, location field doesn't matter
+  return url.casecompare (rhs.url) == 0;
 }

 bool
 site_list_type::operator < (site_list_type const &rhs) const
 {
-  return key.casecompare (rhs.key) < 0;
+  // Sorting order is based on area, location, URL
+  return (key.casecompare (rhs.key) < 0);
+}
+
+void
+site_list_type::parseSite (String const &newsite)
+{
+  /* extract the components of the site */
+  /* ftp://sunsite.dk/mirrors/cygwin;sunsite.dk;Europe;Denmark */
+  enum site_parts { URL, DOMAIN, AREA, LOCATION };
+  string part[LOCATION + 1];
+  string site(newsite.c_str());
+  int pos = 0, npos = 0; 
+  for (int i = 0; i <= LOCATION; i++) { 
+    npos = site.find(';', pos);
+    part[i] = site.substr(pos, npos - pos);
+    if (npos < 0)   // no more ';'
+      break;
+    pos = npos + 1;
+  }
+  url = String(part[URL]);
+  // domain = String(part[DOMAIN]);
+  area = String(part[AREA]);
+  location = String(part[LOCATION]);
 }
 
 static void
@@ -250,9 +255,6 @@
       if (strncmp(bol, "http://";, 7) == 0 ||
          strncmp(bol, "ftp://";, 6) == 0)
        {
-         char *semi = strchr (bol, ';');
-         if (semi)
-           *semi = 0; 
          site_list_type newsite (bol);
          SiteList::iterator i = find (all_site_list.begin(),
                                       all_site_list.end(), newsite);
@@ -265,8 +267,14 @@
              all_site_list = result;
            }
          else
-           //TODO: remove and remerge
-           *i = newsite;
+           { // remove and remerge
+             SiteList result;
+             all_site_list.erase(i);
+             merge (all_site_list.begin(), all_site_list.end(),
+                    &newsite, &newsite + 1,
+                    inserter (result, result.begin()));
+             all_site_list = result;
+           }
        }
     }
   delete[] theString;
@@ -305,7 +313,7 @@ 
       site_list.push_back (tempSite);
     }
   else
-    site_list.push_back (tempSite);
+    site_list.push_back (*i);       // all_site_list is the master!
 }

 void
@@ -475,8 +483,15 @@
   for (SiteList::const_iterator n = site_list.begin ();
        n != site_list.end (); ++n)
     {
+      site_list_type sel_site = *n;
+      // site_list may not have location info, look up all_site_list
+      SiteList::const_iterator sel_iter = find (all_site_list.begin(),
+                                               all_site_list.end(), sel_site);
+      if (sel_iter != all_site_list.end())
+        sel_site = *sel_iter;   // display url with location information
+
       int index = SendMessage (listbox, LB_FINDSTRING, (WPARAM) - 1,
-                              (LPARAM) n->displayed_url.c_str());
+                              (LPARAM) sel_site.displayed_url.c_str());
       if (index != LB_ERR)
        {
          // Highlight the selected item
--- site.h.old  2003-11-01 11:28:46.000000000 +0530
+++ site.h      2005-10-31 21:25:42.640214000 +0530
@@ -57,9 +57,13 @@
   site_list_type (String const &);
   /* workaround for missing placement new in gcc 2.95 */
   void init (String const &);
+  void parseSite (String const &);
   ~site_list_type () {};
   site_list_type &operator= (site_list_type const &);
   String url;
+  // String domain;        // Not used
+  String area;
+  String location;
   String displayed_url;
   String key;
   bool operator == (site_list_type const &) const;
--- String++.h.old      2005-09-01 21:12:15.000000000 +0530
+++ String++.h  2005-10-25 08:24:16.411192000 +0530
@@ -38,7 +38,8 @@
   char const * c_str() const; // only valid until the next mutator call
                              // pretends to be const !!
   inline size_t size() const; // number of characters (!= storage size).
-  size_t find (char) const;
+  //  size_t find (char) const;
+  size_t find (char, int npos=0) const;
   String substr (size_t start = 0, int len = -1) const;
   // operator == and != can be done if/when we have a 'casesensitive' flag to
   // the constructors
--- String++.cc.old     2005-09-01 21:12:15.000000000 +0530
+++ String++.cc 2005-11-09 16:41:19.337464000 +0530
@@ -86,9 +86,9 @@
 // XXX FIXME: Introduce npos, and change all
 // if (size) calls to be if (size()==npos)
 size_t
-String::find(char aChar) const
+String::find(char aChar, int npos) const
 { 
-  for (size_t i=0; i < theData->length; ++i)
+  for (size_t i=npos; i < theData->length; ++i)
     if (theData->theString[i] == aChar)
       return i+1;
   return 0;




	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]