~ubuntu-branches/ubuntu/breezy/garlic/breezy

« back to all changes in this revision

Viewing changes to extract_int.c

  • Committer: Bazaar Package Importer
  • Author(s): zhaoway
  • Date: 2001-04-24 07:09:13 UTC
  • Revision ID: james.westby@ubuntu.com-20010424070913-uzpupnwdfhmliebz
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 Damir Zucic */
 
2
 
 
3
/*=============================================================================
 
4
 
 
5
                                extract_int.c
 
6
 
 
7
Purpose:
 
8
        Extract integer from string. Start reading after the colon, if
 
9
        present.
 
10
 
 
11
Input:
 
12
        Input string pointer.
 
13
 
 
14
Output:
 
15
        Return value.
 
16
 
 
17
Return value:
 
18
        (1) Integer read from string, on success.
 
19
        (2) Zero if there are no digits.
 
20
 
 
21
========includes:============================================================*/
 
22
 
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
#include <ctype.h>
 
26
 
 
27
/*======extract integer from a string:=======================================*/
 
28
 
 
29
int ExtractInteger_ (char *sP)
 
30
{
 
31
char            *P0, *P1;
 
32
int             n;
 
33
 
 
34
/* Colon should be separator: */
 
35
if ((P0 = strstr (sP, ":")) == NULL) P0 = sP;
 
36
 
 
37
/* Replace each non-numeric character except minus sign with space: */
 
38
P1 = P0;
 
39
while ((n = *P1++) != '\0') if (!isdigit (n) && (n != '-')) *(P1 - 1) = ' ';
 
40
 
 
41
/* Try to read one integer: */
 
42
if (sscanf (P0, "%d", &n) != 1) return 0;
 
43
 
 
44
/* If everything worked fine, return the extracted integer: */
 
45
return n;
 
46
}
 
47
 
 
48
/*===========================================================================*/
 
49
 
 
50