/[cvs]/joko/Uni/BSArch/01/prime.c
ViewVC logotype

Diff of /joko/Uni/BSArch/01/prime.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.10 by joko, Sat May 13 12:06:38 2006 UTC revision 1.13 by joko, Sat May 13 18:53:54 2006 UTC
# Line 5  Line 5 
5  #include <limits.h>  #include <limits.h>
6  #include <errno.h>  #include <errno.h>
7    
8    #ifdef _MSC_VER
9    #define snprintf _snprintf
10    #endif
11    
12  #define BOOL int  #define BOOL int
13  #define TRUE 1  #define TRUE 1
14  #define FALSE 0  #define FALSE 0
# Line 13  Line 17 
17  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
18  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)
19    
20    #define MAX_LINE_LENGTH 80
21    
22    
23  /* check for prime number */  /* check for prime number */
24  BOOL is_prime(long int number)  BOOL is_prime(long int number)
25  {  {
# Line 40  long int convert_number(const char *nptr Line 47  long int convert_number(const char *nptr
47          /* invalid characters? */          /* invalid characters? */
48          if (*endptr != '\0') {          if (*endptr != '\0') {
49                  char message[254];                  char message[254];
50                  snprintf(message, 256, "Could not convert '%s' to a valid number.", nptr);                  snprintf(message, 256, "Could not convert '%s' to a valid (integer) number.", nptr);
51                  PRINTERROR(message);                  PRINTERROR(message);
52                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
53          }          }
# Line 88  int main(int argc, char * argv[]) Line 95  int main(int argc, char * argv[])
95                                    
96                  /* (2) file mode: read numbers from file */                  /* (2) file mode: read numbers from file */
97                  } else {                  } else {
98                          char entry[81];                          char line[MAX_LINE_LENGTH + 1];
99                          long int number;                          long int number;
100                          long int lineno = 0;                          int line_length;
101                          while (fgets(entry, 81, fp)) {                          long int line_no = 0;
102                            while (fgets(line, MAX_LINE_LENGTH + 1, fp)) {
103                                                                    
104                                  /* count line number (for warnings) */                                  /* count line number (for warnings) */
105                                  lineno++;                                  line_no++;
106    
107                                    line_length = strlen(line);
108                                    
109                                  /* skip empty lines */                                  /* skip empty lines */
110                                  if (strlen(entry) < 2) continue;                                  if (strlen(line) < 2) continue;
111    
112                                  /* line handling: policy = skip exceeding lines */                                  /* line handling: policy = skip exceeding lines */
113                                                                    
                                 /* if last char is newline, strip it */  
                                 if (entry[strlen(entry)-1] == '\n') {  
                                         entry[strlen(entry)-1] = '\0';  
                                           
114                                  /* line exceeds max length */                                  /* line exceeds max length */
115                                  } else {                                  if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') {
116                                          char message[254];                                          char message[254];
117                                          snprintf(message, 256, "Line too long (max 80 chars) in line number: %i", lineno);                                          snprintf(message, 256, "Line too long (max %i chars) in line number: %i", MAX_LINE_LENGTH, line_no);
118                                          PRINTWARNING(message);                                          PRINTWARNING(message);
119                                                                                    
120                                          /* eat all characters until newline */                                          /* eat all characters until newline or EOF */
121                                          while (fgetc(fp) != 10);                                          while (1) {
122                                                    int charcode = fgetc(fp);
123                                                    if (charcode == 10 || charcode == EOF) break;
124                                            }
125                                                                                    
126                                          /* skip this line from prime calculation */                                          /* skip this line from prime calculation */
127                                          continue;                                          continue;
128                                  }                                  }
129                                                                    
130                                  number = convert_number(entry);                                  /* if last char is newline, strip it */
131                                    if (line[line_length-1] == '\n') {
132                                            line[line_length-1] = '\0';
133                                    }
134                                    
135                                    /* finally: prime number calculation and output */
136                                    number = convert_number(line);
137                                  PRINTPRIME(number);                                  PRINTPRIME(number);
138                                    
139                          }                          }
140                          fclose(fp);                          fclose(fp);
141                  }                  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.13

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed