diff --git a/net/third_party/parseftp/ParseFTPList.cpp b/net/third_party/parseftp/ParseFTPList.cpp index b9ffbdc..be099e1 100644 --- a/net/third_party/parseftp/ParseFTPList.cpp +++ b/net/third_party/parseftp/ParseFTPList.cpp @@ -36,15 +36,18 @@ * * ***** END LICENSE BLOCK ***** */ -#include -#include +#include "net/third_party/parseftp/ParseFTPList.h" + #include -#include "plstr.h" -#include "ParseFTPList.h" +#include "base/string_util.h" + +using base::Time; /* ==================================================================== */ +namespace net { + int ParseFTPList(const char *line, struct list_state *state, struct list_result *result ) { @@ -156,11 +159,9 @@ int ParseFTPList(const char *line, struct list_state *state, pos++; if (pos < linelen && line[pos] == ',') { - PRTime t; - PRTime seconds; - PR_sscanf(p+1, "%llu", &seconds); - LL_MUL(t, seconds, PR_USEC_PER_SEC); - PR_ExplodeTime(t, PR_LocalTimeParameters, &(result->fe_time) ); + uint64 seconds = StringToInt64(p+1); + Time t = Time::FromTimeT(seconds); + t.LocalExplode(&(result->fe_time)); + result->fe_time.month--; } } } @@ -508,12 +509,9 @@ int ParseFTPList(const char *line, struct list_state *state, * So its rounded up to the next block, so what, its better * than not showing the size at all. */ - PRUint64 fsz, factor; - LL_UI2L(fsz, strtoul(tokens[1], (char **)0, 10)); - LL_UI2L(factor, 512); - LL_MUL(fsz, fsz, factor); - PR_snprintf(result->fe_size, sizeof(result->fe_size), - "%lld", fsz); + long long size = strtoul(tokens[1], NULL, 10) * 512; + base::snprintf(result->fe_size, sizeof(result->fe_size), "%lld", + size); } } /* if (result->fe_type != 'd') */ @@ -535,17 +533,17 @@ int ParseFTPList(const char *line, struct list_state *state, } if (month_num >= 12) month_num = 0; - result->fe_time.tm_month = month_num; - result->fe_time.tm_mday = atoi(tokens[2]); - result->fe_time.tm_year = atoi(p+4); // NSPR wants year as XXXX + result->fe_time.month = month_num; + result->fe_time.day_of_month = atoi(tokens[2]); + result->fe_time.year = atoi(p+4); p = tokens[3] + 2; if (*p == ':') p++; if (p[2] == ':') - result->fe_time.tm_sec = atoi(p+3); - result->fe_time.tm_hour = atoi(tokens[3]); - result->fe_time.tm_min = atoi(p); + result->fe_time.second = atoi(p+3); + result->fe_time.hour = atoi(tokens[3]); + result->fe_time.minute = atoi(p); return result->fe_type; @@ -678,25 +676,25 @@ int ParseFTPList(const char *line, struct list_state *state, p = tokens[tokmarker+4]; if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */ { - result->fe_time.tm_year = atoi(p+0) - 1900; - result->fe_time.tm_month = atoi(p+5) - 1; - result->fe_time.tm_mday = atoi(p+8); + result->fe_time.year = atoi(p+0) - 1900; + result->fe_time.month = atoi(p+5) - 1; + result->fe_time.day_of_month = atoi(p+8); } else /* oldstyle: [M]M/DD/YY format */ { pos = toklen[tokmarker+4]; - result->fe_time.tm_month = atoi(p) - 1; - result->fe_time.tm_mday = atoi((p+pos)-5); - result->fe_time.tm_year = atoi((p+pos)-2); - if (result->fe_time.tm_year < 70) - result->fe_time.tm_year += 100; + result->fe_time.month = atoi(p) - 1; + result->fe_time.day_of_month = atoi((p+pos)-5); + result->fe_time.year = atoi((p+pos)-2); + if (result->fe_time.year < 70) + result->fe_time.year += 100; } p = tokens[tokmarker+5]; pos = toklen[tokmarker+5]; - result->fe_time.tm_hour = atoi(p); - result->fe_time.tm_min = atoi((p+pos)-5); - result->fe_time.tm_sec = atoi((p+pos)-2); + result->fe_time.hour = atoi(p); + result->fe_time.minute = atoi((p+pos)-5); + result->fe_time.second = atoi((p+pos)-2); result->fe_cinfs = 1; result->fe_fname = tokens[0]; @@ -839,25 +837,25 @@ int ParseFTPList(const char *line, struct list_state *state, } } - result->fe_time.tm_month = atoi(tokens[0]+0); - if (result->fe_time.tm_month != 0) + result->fe_time.month = atoi(tokens[0]+0); + if (result->fe_time.month != 0) { - result->fe_time.tm_month--; - result->fe_time.tm_mday = atoi(tokens[0]+3); - result->fe_time.tm_year = atoi(tokens[0]+6); + result->fe_time.month--; + result->fe_time.day_of_month = atoi(tokens[0]+3); + result->fe_time.year = atoi(tokens[0]+6); /* if year has only two digits then assume that 00-79 is 2000-2079 80-99 is 1980-1999 */ - if (result->fe_time.tm_year < 80) - result->fe_time.tm_year += 2000; - else if (result->fe_time.tm_year < 100) - result->fe_time.tm_year += 1900; + if (result->fe_time.year < 80) + result->fe_time.year += 2000; + else if (result->fe_time.year < 100) + result->fe_time.year += 1900; } - result->fe_time.tm_hour = atoi(tokens[1]+0); - result->fe_time.tm_min = atoi(tokens[1]+3); - if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12) - result->fe_time.tm_hour += 12; + result->fe_time.hour = atoi(tokens[1]+0); + result->fe_time.minute = atoi(tokens[1]+3); + if ((tokens[1][5]) == 'P' && result->fe_time.hour < 12) + result->fe_time.hour += 12; /* the caller should do this (if dropping "." and ".." is desired) if (result->fe_type == 'd' && result->fe_fname[0] == '.' && @@ -955,13 +953,13 @@ int ParseFTPList(const char *line, struct list_state *state, result->fe_size[pos] = '\0'; } - result->fe_time.tm_month = atoi(&p[35-18]) - 1; - result->fe_time.tm_mday = atoi(&p[38-18]); - result->fe_time.tm_year = atoi(&p[41-18]); - if (result->fe_time.tm_year < 80) - result->fe_time.tm_year += 100; - result->fe_time.tm_hour = atoi(&p[46-18]); - result->fe_time.tm_min = atoi(&p[49-18]); + result->fe_time.month = atoi(&p[35-18]) - 1; + result->fe_time.day_of_month = atoi(&p[38-18]); + result->fe_time.year = atoi(&p[41-18]); + if (result->fe_time.year < 80) + result->fe_time.year += 100; + result->fe_time.hour = atoi(&p[46-18]); + result->fe_time.minute = atoi(&p[49-18]); /* the caller should do this (if dropping "." and ".." is desired) if (result->fe_type == 'd' && result->fe_fname[0] == '.' && @@ -1006,7 +1004,7 @@ int ParseFTPList(const char *line, struct list_state *state, * "drwxr-xr-x 2 0 0 512 May 28 22:17 etc" */ - PRBool is_old_Hellsoft = PR_FALSE; + bool is_old_Hellsoft = false; if (numtoks >= 6) { @@ -1034,7 +1032,7 @@ int ParseFTPList(const char *line, struct list_state *state, /* rest is FMA[S] or AFM[S] */ lstyle = 'U'; /* very likely one of the NetWare servers */ if (toklen[0] == 10) - is_old_Hellsoft = PR_TRUE; + is_old_Hellsoft = true; } } } @@ -1150,10 +1148,10 @@ int ParseFTPList(const char *line, struct list_state *state, result->fe_size[pos] = '\0'; } - result->fe_time.tm_month = month_num; - result->fe_time.tm_mday = atoi(tokens[tokmarker+2]); - if (result->fe_time.tm_mday == 0) - result->fe_time.tm_mday++; + result->fe_time.month = month_num; + result->fe_time.day_of_month = atoi(tokens[tokmarker+2]); + if (result->fe_time.day_of_month == 0) + result->fe_time.day_of_month++; p = tokens[tokmarker+3]; pos = (unsigned int)atoi(p); @@ -1161,25 +1159,26 @@ int ParseFTPList(const char *line, struct list_state *state, p--; if (p[2] != ':') /* year */ { - result->fe_time.tm_year = pos; + result->fe_time.year = pos; } else { - result->fe_time.tm_hour = pos; - result->fe_time.tm_min = atoi(p+3); + result->fe_time.hour = pos; + result->fe_time.minute = atoi(p+3); if (p[5] == ':') - result->fe_time.tm_sec = atoi(p+6); + result->fe_time.second = atoi(p+6); - if (!state->now_time) + if (!state->now_tm_valid) { - state->now_time = PR_Now(); - PR_ExplodeTime((state->now_time), PR_LocalTimeParameters, &(state->now_tm) ); + Time t = Time::Now(); + t.LocalExplode(&(state->now_tm)); + state->now_tm.month--; + state->now_tm_valid = true; } - result->fe_time.tm_year = state->now_tm.tm_year; - if ( (( state->now_tm.tm_month << 5) + state->now_tm.tm_mday) < - ((result->fe_time.tm_month << 5) + result->fe_time.tm_mday) ) - result->fe_time.tm_year--; + result->fe_time.year = state->now_tm.year; + if ( (( state->now_tm.month << 5) + state->now_tm.day_of_month) < + ((result->fe_time.month << 5) + result->fe_time.day_of_month) ) + result->fe_time.year--; } /* time/year */ @@ -1197,10 +1196,10 @@ int ParseFTPList(const char *line, struct list_state *state, { /* First try to use result->fe_size to find " -> " sequence. This can give proper result for cases like "aaa -> bbb -> ccc". */ - PRUint32 fe_size = atoi(result->fe_size); + unsigned int fe_size = atoi(result->fe_size); if (result->fe_fnlen > (fe_size + 4) && - PL_strncmp(result->fe_fname + result->fe_fnlen - fe_size - 4 , " -> ", 4) == 0) + strncmp(result->fe_fname + result->fe_fnlen - fe_size - 4 , " -> ", 4) == 0) { result->fe_lname = result->fe_fname + (result->fe_fnlen - fe_size); result->fe_lnlen = (&(line[linelen])) - (result->fe_lname); @@ -1216,7 +1215,7 @@ int ParseFTPList(const char *line, struct list_state *state, p = result->fe_fname + (result->fe_fnlen - 5); for (pos = (result->fe_fnlen - 5); pos > 0; pos--) { - if (PL_strncmp(p, " -> ", 4) == 0) + if (strncmp(p, " -> ", 4) == 0) { result->fe_lname = p + 4; result->fe_lnlen = (&(line[linelen])) @@ -1371,9 +1370,9 @@ int ParseFTPList(const char *line, struct list_state *state, tbuf[1] == month_names[pos+1] && tbuf[2] == month_names[pos+2]) { - result->fe_time.tm_month = pos/3; - result->fe_time.tm_mday = atoi(tokens[3]); - result->fe_time.tm_year = atoi(tokens[4]) - 1900; + result->fe_time.month = pos/3; + result->fe_time.day_of_month = atoi(tokens[3]); + result->fe_time.year = atoi(tokens[4]) - 1900; break; } } @@ -1381,17 +1380,17 @@ int ParseFTPList(const char *line, struct list_state *state, } else { - result->fe_time.tm_month = atoi(p+0)-1; - result->fe_time.tm_mday = atoi(p+3); - result->fe_time.tm_year = atoi(p+6); - if (result->fe_time.tm_year < 80) /* SuperTCP */ - result->fe_time.tm_year += 100; + result->fe_time.month = atoi(p+0)-1; + result->fe_time.day_of_month = atoi(p+3); + result->fe_time.year = atoi(p+6); + if (result->fe_time.year < 80) /* SuperTCP */ + result->fe_time.year += 100; pos = 3; /* SuperTCP toknum of date field */ } - result->fe_time.tm_hour = atoi(tokens[pos]); - result->fe_time.tm_min = atoi(&(tokens[pos][toklen[pos]-2])); + result->fe_time.hour = atoi(tokens[pos]); + result->fe_time.minute = atoi(&(tokens[pos][toklen[pos]-2])); /* the caller should do this (if dropping "." and ".." is desired) if (result->fe_type == 'd' && result->fe_fname[0] == '.' && @@ -1607,7 +1606,7 @@ int ParseFTPList(const char *line, struct list_state *state, pos = atoi(tokens[pos]); if (pos > 0 && pos <= 31) { - result->fe_time.tm_mday = pos; + result->fe_time.day_of_month = pos; month_num = 1; for (pos = 0; pos < (12*3); pos+=3) { @@ -1618,34 +1617,35 @@ int ParseFTPList(const char *line, struct list_state *state, month_num++; } if (month_num > 12) - result->fe_time.tm_mday = 0; + result->fe_time.day_of_month = 0; else - result->fe_time.tm_month = month_num - 1; + result->fe_time.month = month_num - 1; } } - if (result->fe_time.tm_mday) + if (result->fe_time.day_of_month) { tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */ p = tokens[tokmarker]; pos = atoi(p); if (pos > 24) - result->fe_time.tm_year = pos-1900; + result->fe_time.year = pos-1900; else { if (p[1] == ':') p--; - result->fe_time.tm_hour = pos; - result->fe_time.tm_min = atoi(p+3); - if (!state->now_time) + result->fe_time.hour = pos; + result->fe_time.minute = atoi(p+3); + if (!state->now_tm_valid) { - state->now_time = PR_Now(); - PR_ExplodeTime((state->now_time), PR_LocalTimeParameters, &(state->now_tm) ); + Time t = Time::Now(); + t.LocalExplode(&(state->now_tm)); + state->now_tm.month--; + state->now_tm_valid = true; } - result->fe_time.tm_year = state->now_tm.tm_year; - if ( (( state->now_tm.tm_month << 4) + state->now_tm.tm_mday) < - ((result->fe_time.tm_month << 4) + result->fe_time.tm_mday) ) - result->fe_time.tm_year--; + result->fe_time.year = state->now_tm.year; + if ( (( state->now_tm.month << 4) + state->now_tm.day_of_month) < + ((result->fe_time.month << 4) + result->fe_time.day_of_month) ) + result->fe_time.year--; } /* got year or time */ } /* got month/mday */ } /* may have year or time */ @@ -1893,3 +1893,5 @@ int main(int argc, char *argv[]) return 0; } #endif + +} // namespace net diff --git a/net/third_party/parseftp/ParseFTPList.h b/net/third_party/parseftp/ParseFTPList.h index 30ef8a3..b11abdc 100644 --- a/net/third_party/parseftp/ParseFTPList.h +++ b/net/third_party/parseftp/ParseFTPList.h @@ -35,7 +35,11 @@ * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ -#include "nspr.h" + +#ifndef NET_THIRD_PARTY_PARSEFTP_PARSEFTPLIST_H_ +#define NET_THIRD_PARTY_PARSEFTP_PARSEFTPLIST_H_ + +#include "base/time.h" /* ParseFTPList() parses lines from an FTP LIST command. ** @@ -96,28 +100,30 @@ #define SUPPORT_OS2 /* IBM TCP/IP for OS/2 - FTP Server */ #define SUPPORT_W16 /* win16 hosts: SuperTCP or NetManage Chameleon */ +namespace net { + +/* WARNING: The 'month' field of base::Time::Exploded will be in range 0-11 + * (0 = January) to match PRExplodedTime. The caller should add 1 to 'month' + * if it intends to use it. + */ + struct list_state { void *magic; /* to determine if previously initialized */ - PRTime now_time; /* needed for year determination */ - PRExplodedTime now_tm; /* needed for year determination */ - PRInt32 lstyle; /* LISTing style */ - PRInt32 parsed_one; /* returned anything yet? */ + bool now_tm_valid; /* true if now_tm is valid */ + base::Time::Exploded now_tm; /* needed for year determination */ + int lstyle; /* LISTing style */ + int parsed_one; /* returned anything yet? */ char carry_buf[84]; /* for VMS multiline */ - PRUint32 carry_buf_len; /* length of name in carry_buf */ - PRUint32 numlines; /* number of lines seen */ + unsigned int carry_buf_len; /* length of name in carry_buf */ + unsigned int numlines; /* number of lines seen */ }; struct list_result { - PRInt32 fe_type; /* 'd'(dir) or 'l'(link) or 'f'(file) */ + int fe_type; /* 'd'(dir) or 'l'(link) or 'f'(file) */ const char * fe_fname; /* pointer to filename */ - PRUint32 fe_fnlen; /* length of filename */ + unsigned int fe_fnlen; /* length of filename */ const char * fe_lname; /* pointer to symlink name */ - PRUint32 fe_lnlen; /* length of symlink name */ + unsigned int fe_lnlen; /* length of symlink name */ char fe_size[40]; /* size of file in bytes (<= (2^128 - 1)) */ - PRExplodedTime fe_time; /* last-modified time */ - PRInt32 fe_cinfs; /* file system is definitely case insensitive */ + base::Time::Exploded fe_time; /* last-modified time */ + int fe_cinfs; /* file system is definitely case insensitive */ /* (converting all-upcase names may be desirable) */ }; @@ -125,3 +131,6 @@ int ParseFTPList(const char *line, struct list_state *state, struct list_result *result ); +} // namespace net + +#endif // NET_THIRD_PARTY_PARSEFTP_PARSEFTPLIST_H_