持续时间转换 [英] Duration Conversion
问题描述
[在TRU64 Unix上运行的C应用程序]
Hello All
我有一个简单的任务让我发疯。以下列格式表示
持续时间的字符串将传递给我的应用程序,
函数专用于将此持续时间转换为秒;
[H] H:MM:SS
例如0:00:00或00:12:45
作为转换过程的一部分使用atoi的原始函数和
它产生了一些可怕的转换0: 00:10转换为956秒
等
该功能被改写为使用strtol因为它更好
错误处理功能,但它现在核心转储!假设有任何东西可能会被传递到此函数中,有人会有一个
防弹的想法来做这个转换;
例如A:00:00或:0:45等
以下转载的是违规函数,它当前转储的行是用@标记的。 br />
我们非常感谢任何帮助。
谢谢,
B
void secondDurations()
{
long hr,min,sec,sec_duration = 0;
char tm [10];
int ch ='':'';
char * end_ptr = NULL;
char duration [41];
char * pfirst;
char * plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(持续时间,00:00:34);
//字符串应该是7字符长的
if(strlen(duration)> = 7)
{
pfirst = strchr(duration,ch);
plast = strrchr(duration,ch);
//测试持续时间格式00:00:00
if((pfirst!= NULL || plast!= NULL)&&(pfirst!= plast))
{
errno = 0;
//获取小时部分
strcpy(tm,strtok(持续时间,":" ;));
//转换
hr = strtol(tm,& end_ptr,10);
//测试
if(hr INT_MAX || hr< INT_MIN)
{
//拒绝;
返回;
}
否则if(end_ptr == tm || * end_ptr!=''\''')
{
//拒绝
返回;
}
其他
{
errno = 0;
@ strcpy(tm,strtok(NULL,":"));
min = strtol(tm,& end_ptr,10);
if(min INT_MAX || min< INT_MIN || min 59)
{
//拒绝;
返回;
}
else if(end_ptr == tm || * end_ptr!=''\''')
{
//拒绝
返回;
}
其他
{
errno = 0;
@ strcpy(tm,strtok(NULL,":"));
sec = strtol(tm,& end_ptr,10);
if(sec INT_MAX || sec< INT_MIN || sec 59)
{
//拒绝;
返回;
}
else if(end_ptr == tm || * end_ptr !=''\''')
{
//拒绝
返回;
}
其他
{
sec_duration = hr * 3600 + min * 60 + sec;
printf(" duration in seconds =%d \ n",(int)sec_duration);
}
}
}
}
}
[C application running on TRU64 Unix]
Hello All
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;
[H]H:MM:SS
e.g. 0:00:00 or 00:12:45
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
e.g. A:00:00 or :0:45 etc
Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
Any help would be much appreciated.
Thank you,
B
void secondDurations()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = '':'';
char *end_ptr = NULL;
char duration[41];
char *pfirst;
char *plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(duration, "00:00:34");
//String should at leat be 7 characters long
if (strlen(duration) >= 7)
{
pfirst = strchr(duration, ch);
plast = strrchr(duration, ch);
//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;
//Get hour portion
strcpy(tm, strtok(duration,":"));
//Convert
hr = strtol(tm, &end_ptr, 10);
//Test
if (hr INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
min = strtol(tm, &end_ptr, 10);
if (min INT_MAX || min < INT_MIN || min 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
sec = strtol(tm, &end_ptr, 10);
if (sec INT_MAX || sec < INT_MIN || sec 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duration in seconds=%d\n",(int)sec_duration);
}
}
}
}
}
推荐答案
bcpkh写道:
bcpkh wrote:
[在TRU64 Unix上运行的C应用程序]
Hello All
我有一个简单的任务让我发疯。以下列格式表示
持续时间的字符串将传递给我的应用程序,
函数专用于将此持续时间转换为秒;
[H] H:MM:SS
例如0:00:00或00:12:45
作为转换过程的一部分使用atoi的原始函数和
它产生了一些可怕的转换0: 00:10转换为956秒
等
该功能被改写为使用strtol因为它更好
错误处理功能,但它现在核心转储!假设有任何东西可能会被传递到此函数中,有人会有一个
防弹的想法来做这个转换;
例如A:00:00或:0:45等
以下复制的是违规函数,它当前转储的行是用@标记的。
[C application running on TRU64 Unix]
Hello All
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;
[H]H:MM:SS
e.g. 0:00:00 or 00:12:45
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
e.g. A:00:00 or :0:45 etc
Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
从阅读你的代码时,这些无效的时间字符串应该被拒绝。
由于格式相当严格,你可以接受,我将使用
sscanf()执行此任务。
void secondDurations()
{
int hr,min,sec,num_matches,num_chars;
long sec_duration = 0;
char duration [41];
// 00:00:00
// 0:00:00
// 01234567
strcpy(持续时间,& ; 00:00:34");
//字符串应该是7字符长的
if(strlen(duration)< 7)
{
//拒绝
返回;
}
/ *尾随%n导致消耗的字符数为
。这应该是字符串的整个长度* /
num_matches = sscanf(持续时间,%d:%d:%d%n,& hr,& min,& sec ,
& num_chars);
if((num_matches!= 3)||(num_chars!= strlen(duration)))
{
//格式错误的日期。拒绝
返回;
}
其他
{
sec_duration = hr * 3600 + min * 60 + sec;
printf(持续时间以秒为单位=%ld \ n,sec_duration);
}
}
From reading your code, these invalid time strings should be rejected.
As the format is rather strict in what you can accept, I would use
sscanf() for this task.
void secondDurations()
{
int hr, min, sec, num_matches, num_chars;
long sec_duration = 0;
char duration[41];
// 00:00:00
// 0:00:00
// 01234567
strcpy(duration, "00:00:34");
//String should at leat be 7 characters long
if (strlen(duration) < 7)
{
// Reject
return;
}
/* The trailing %n causes the number of characters consumed to be
recorded. This should be the entire length of the string */
num_matches = sscanf(duration, "%d:%d:%d%n", &hr, &min, &sec,
&num_chars);
if ((num_matches != 3) || (num_chars != strlen(duration)))
{
// Malformed date. Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duration in seconds=%ld\n",sec_duration);
}
}
>
我们非常感谢任何帮助。
谢谢,
B
>
Any help would be much appreciated.
Thank you,
B
Bart v Ingen Schenau
-
acllc-c ++常见问题解答: http://www.comeaucomputing.com/learn/faq
clc常见问题: http://www.eskimo.com/~scs/C-faq/top.html
clc ++ FAQ: http://www.parashift.com/c++-faq-lite/
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
" bcpkh" < va ************** @ gmail.comwrote in message
news:11 **************** ******@19g2000hsx.googlegro ups.com ...
"bcpkh" <va**************@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
[在TRU64 Unix上运行的C应用程序]
Hello All
我有一个让我疯狂的简单任务。以下列格式表示
持续时间的字符串将传递给我的应用程序,
函数专用于将此持续时间转换为秒;
[H] H:MM:SS
例如0:00:00或00:12:45
作为转换过程的一部分使用atoi的原始函数和
它产生了一些可怕的转换0: 00:10转换为956秒
等
该功能被改写为使用strtol因为它更好
错误处理功能,但它现在核心转储!假设有任何东西可能会被传递到此函数中,有人会有一个
防弹的想法来做这个转换;
例如A:00:00或:0:45等
以下转载的是违规函数,它当前转储的行是用@标记的。 br />
我们非常感谢任何帮助。
谢谢,
B
void secondDurations()
{
long hr,min,sec,sec_duration = 0;
char tm [10];
int ch ='':'';
char * end_ptr = NULL;
char duration [41];
char * pfirst;
char * plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(持续时间,00:00:34);
//字符串应该是7字符长的
if(strlen(duration)> = 7)
{
pfirst = strchr(duration,ch);
plast = strrchr(duration,ch);
//测试持续时间格式00 :00:00
if((pfirst!= NULL || plast!= NULL)&&(pfirst!= plast))
{
errno = 0;
//获取小时部分
strcpy(tm,strtok(duration,":")) ;
//转换
hr = strtol(tm,& end_ptr,10);
/ /测试
if(hr INT_MAX || hr< INT_MIN)
{
//拒绝;
返回;
}
否则if(end_ptr == tm || * end_ptr!=''\''')
{
//拒绝
返回;
}
其他
{
errno = 0;
@ strcpy(tm,strtok(NULL,":"));
min = strtol(tm,& end_ptr,10);
if(min INT_MAX || min< INT_MIN || min 59)
{
//拒绝;
返回;
}
else if(end_ptr == tm || * end_ptr!=''\''')
{
//拒绝
返回;
}
其他
{
errno = 0;
@ strcpy(tm,strtok(NULL,":"));
sec = strtol(tm,& end_ptr,10);
>
if(sec INT_MAX ||秒< INT_MIN || sec 59)
{
//拒绝;
返回;
}
否则(end_ptr == tm || * end_ptr!=''\''')
{
//拒绝
返回;
}
其他
{
sec_duration = hr * 3600 + min * 60 + sec;
printf(持续时间以秒为单位=%d \ n,(int)sec_duration);
}
}
}
}
}
[C application running on TRU64 Unix]
Hello All
I have a simple task that is driving me crazy. A string representing a
duration in the following format is passed to my application, a
function is dedicated to convert this duration to seconds;
[H]H:MM:SS
e.g. 0:00:00 or 00:12:45
The original function used atoi as part of the conversion process and
it produced some scary conversions 0:00:10 converted to 956 seconds
etc.
The function was rewritten to make use of strtol because of its better
error handling functionality but it now core dumps! Do someone have a
bullet proof idea to do this conversion assuming that anything might
be passed into this function;
e.g. A:00:00 or :0:45 etc
Reproducted below is the offending function the lines where it
currently dumps is marked with a @.
Any help would be much appreciated.
Thank you,
B
void secondDurations()
{
long hr, min, sec, sec_duration = 0;
char tm[10];
int ch = '':'';
char *end_ptr = NULL;
char duration[41];
char *pfirst;
char *plast;
// 00:00:00
// 0:00:00
// 01234567
strcpy(duration, "00:00:34");
//String should at leat be 7 characters long
if (strlen(duration) >= 7)
{
pfirst = strchr(duration, ch);
plast = strrchr(duration, ch);
//Test duration format 00:00:00
if((pfirst != NULL || plast != NULL) && (pfirst != plast))
{
errno = 0;
//Get hour portion
strcpy(tm, strtok(duration,":"));
//Convert
hr = strtol(tm, &end_ptr, 10);
//Test
if (hr INT_MAX || hr < INT_MIN)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
min = strtol(tm, &end_ptr, 10);
if (min INT_MAX || min < INT_MIN || min 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
errno = 0;
@ strcpy(tm, strtok(NULL,":"));
sec = strtol(tm, &end_ptr, 10);
if (sec INT_MAX || sec < INT_MIN || sec 59)
{
//Reject;
return;
}
else if (end_ptr == tm || *end_ptr != ''\0'')
{
//Reject
return;
}
else
{
sec_duration = hr * 3600 + min * 60 + sec;
printf("duration in seconds=%d\n",(int)sec_duration);
}
}
}
}
}
尝试这样的事情:
long hrs,mins,sec;
char * ptr,* next;
hrs = strtol(duration,* ptr,10);
if((* ptr =='':'')&&(ptr!= duration)){
/ *小时阅读,试试分钟* /
next = ptr + 1;
mins = strtol(next,& ptr,10);
if((* ptr =='':'') &&(ptr!= next)} {
next = ptr + 1;
sec = strtol(next,& ptr,10);
if(* ptr ==''\'''){
/ * good - 计算总秒数* /
}
}
}
应该做出其他安全问题,例如那个
ptr-duration是1或2,而ptr-next是2等等,
并且hrs / mins / sec值在正确的范围。
-
Fred
Try something like this:
long hrs, mins, sec;
char *ptr, *next;
hrs = strtol( duration, *ptr, 10 );
if ( (*ptr == '':'' ) && (ptr != duration) ) {
/* hours read, try minutes */
next = ptr+1;
mins = strtol( next, &ptr, 10 );
if ( (*ptr == '':'') && (ptr != next) } {
next = ptr+1;
sec = strtol( next, &ptr, 10 );
if ( *ptr == ''\0'' ) {
/* good - compute total seconds */
}
}
}
Other safety chacks should be made, such as that
ptr-duration is 1 or 2, and ptr-next is 2, etc.,
and that the hrs/mins/sec values are in the correct range.
--
Fred
Hello Bart和Fred
感谢您的建议和意见,您一直非常有帮助!
乐于助人!我一定会利用你的意见。
B
Hello Bart and Fred
Thank you for you suggestions and comments, you have been very
helpful! I will definitely make use of your inputs.
B
这篇关于持续时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!