/[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.12 by joko, Sat May 13 12:35:32 2006 UTC
# Line 13  Line 13 
13  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
14  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)
15    
16    #define MAX_LINE_LENGTH 80
17    
18    
19  /* check for prime number */  /* check for prime number */
20  BOOL is_prime(long int number)  BOOL is_prime(long int number)
21  {  {
# Line 40  long int convert_number(const char *nptr Line 43  long int convert_number(const char *nptr
43          /* invalid characters? */          /* invalid characters? */
44          if (*endptr != '\0') {          if (*endptr != '\0') {
45                  char message[254];                  char message[254];
46                  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);
47                  PRINTERROR(message);                  PRINTERROR(message);
48                  exit(EXIT_FAILURE);                  exit(EXIT_FAILURE);
49          }          }
# Line 88  int main(int argc, char * argv[]) Line 91  int main(int argc, char * argv[])
91                                    
92                  /* (2) file mode: read numbers from file */                  /* (2) file mode: read numbers from file */
93                  } else {                  } else {
94                          char entry[81];                          char line[MAX_LINE_LENGTH + 1];
95                          long int number;                          long int number;
96                          long int lineno = 0;                          int line_length;
97                          while (fgets(entry, 81, fp)) {                          long int line_no = 0;
98                            while (fgets(line, MAX_LINE_LENGTH + 1, fp)) {
99                                                                    
100                                  /* count line number (for warnings) */                                  /* count line number (for warnings) */
101                                  lineno++;                                  line_no++;
102    
103                                    line_length = strlen(line);
104                                    
105                                  /* skip empty lines */                                  /* skip empty lines */
106                                  if (strlen(entry) < 2) continue;                                  if (strlen(line) < 2) continue;
107    
108                                  /* line handling: policy = skip exceeding lines */                                  /* line handling: policy = skip exceeding lines */
109                                                                    
                                 /* if last char is newline, strip it */  
                                 if (entry[strlen(entry)-1] == '\n') {  
                                         entry[strlen(entry)-1] = '\0';  
                                           
110                                  /* line exceeds max length */                                  /* line exceeds max length */
111                                  } else {                                  if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') {
112                                          char message[254];                                          char message[254];
113                                          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);
114                                          PRINTWARNING(message);                                          PRINTWARNING(message);
115                                                                                    
116                                          /* eat all characters until newline */                                          /* eat all characters until newline or EOF */
117                                          while (fgetc(fp) != 10);                                          while (1) {
118                                                    int charcode = fgetc(fp);
119                                                    if (charcode == 10 || charcode == EOF) break;
120                                            }
121                                                                                    
122                                          /* skip this line from prime calculation */                                          /* skip this line from prime calculation */
123                                          continue;                                          continue;
124                                  }                                  }
125                                                                    
126                                  number = convert_number(entry);                                  /* if last char is newline, strip it */
127                                    if (line[line_length-1] == '\n') {
128                                            line[line_length-1] = '\0';
129                                    }
130                                    
131                                    /* finally: prime number calculation and output */
132                                    number = convert_number(line);
133                                  PRINTPRIME(number);                                  PRINTPRIME(number);
134                                    
135                          }                          }
136                          fclose(fp);                          fclose(fp);
137                  }                  }

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

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