URL truncation technique using pointers skillfully

Source: Internet
Author: User

When using URL truncation, we generally want to get site, port, and path from the URL. This is the most memory-saving method is to use memory skillfully.


Give an example. A Url:http://write.blog.csdn.net/postedit?ref=toolbar

By passing in a URL, we return the "/" value of port and path directly to path (pointer), get path and write "\" as "."

Then the ":" after the digit to the port because the previous \ has been written, so here can directly get 8080. Again: Write A.

Finally, direct transfer of URL+7 to the site will be able to get write.blog.csdn.net

At this point, no new memory was created to truncate the URL directly.


Specifically implemented as follows:

int X_parse_url (const char *input, Char *site, Char *port, char *path) 00766 {00767 char Tmp[gl_max_url_
LEN];
00768 char *pin = tmp;
00769 Char *p, *q;
00770 char *p_query = NULL; 00771 if (strlen (input) >= Gl_max_url_len) 00772 {00773 write_log (gl_log_debug, "X_parse_url:url is T
OO Long ");
00774 return 0;
00775} 00776 strcpy (tmp, input);
00777 X_delete_inter (TMP); 00778 if (strncasecmp (PIN, "http://", 7) = = 0) 00779 {pin + 7;} 00780//get path 00781 p = STRCHR (PIN,
'/');
00782 p_query = STRCHR (pin, '? '); 00783 if (null = p) 00784 {00785 p = p_query; 00786} 00787 if ((null!= p_query) && (p_             Query <= p) 00788 {00789 if (path!= NULL) 00790 {00791 path[0] = '; 00792 if (strlen (p_query) < gl_max_path_len-1) 00793 {00794 strncpy (PATH, "/", 2);
00795 strcat (path, p_query); 
00796} 00797 Else 00798 {return 0;} 00799} 00800 *p_query = '; 00801} 00802 Else 00803 {00804 if (P!= null) 00805 {00806 if (path!= null) 0                 0807 {00808 Path[0] = ' I '; 00809 if (strlen (p) < Gl_max_path_len) 00810 {strcpy (path, p);} 00811 else 00812 {return 0;} 00813} 008
*p = ' the ";                 00815} 00816 Else 00817 {00818 if (path!= NULL) 00819 {00820
strcpy (Path, "/");
00821} 00822} 00823} 00824 q = STRCHR (PIN, ': ');             00825//get Port 00826 if (q!= null) 00827 {00828 if (port!= null) 00829 {00830
Port[0] = ' the '; 00831 if (strlen (q) < Gl_max_port_len && ATOI (q + 1) > 0) 00832 {00833 strcpy (port, q + 1); 00834} 00835 Else
00836 {return 0;} 00837} 00838 *q = ';      00839} 00840 Else 00841 {00842 if (port!= NULL) 00843 {port[0] = ' I '; 00844} 00845         Check if the default port 00846 if (port!= NULL) && (Port, "80", 3)) = 0) 00847 {00848
Port[0] = 0; 00849} 00850//get site 00851 if (site!= NULL) 00852 {00853 site[0] = ' I '; 00854 if (p         In[0]!= ' && strlen (PIN) < Gl_max_site_len) 00855 {00856 strcpy (SITE, pin); 00857 } 00858 Else 00859 {00860 Write_log (gl_log_debug, "x_parse_url:site name too long or EMP
Ty url[%s] ", pin);
00861 return 0;
00862} 00863} 00864 return 1; 00865}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.