ISLEC  Version 4.2
clear_comments.c
Go to the documentation of this file.
1 /* src/clear_comments.c
2  *
3  * Copyright (C) 2011-2018 Dongdong Li
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundataion; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABLITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
34 #define STRING_LENGTH 1024
35 
50 extern bool clear_comments (char *input, char *temp);
51 
52 /* implementation of the clear_comments() function */
53 extern bool clear_comments (char *input, char *temp)
54 {
55  if (input == NULL || temp == NULL)
56  {
57  printf ("clear_comments.c: clear_comments () the input ");
58  printf ("database filename or temporary file name is null.\n");
59 
60  return false;
61  }
62  /* define file points for the input database and temporary file */
63  FILE *input_fp, *temp_fp;
64 
65  /* alloc memory for temp_line and initialize memory */
66  char *temp_line = (char*)malloc(STRING_LENGTH*sizeof(char));
67  memset(temp_line, 0, STRING_LENGTH);
68 
69  /* open input file and check errors */
70  if ((input_fp = fopen (input, "r")) == NULL)
71  {
72  printf ("clear_comments.c: clear_comments () open database file error.\n");
73  return false;
74  }
75  else
76  {
77  /* open temp file and check errors */
78  if ((temp_fp = fopen (temp, "w")) == NULL)
79  {
80  printf ("clear_comments.c: clear_comments () open temporary file error.\n");
81  return false;
82  }
83  else
84  {
85  while (fgets (temp_line, STRING_LENGTH, input_fp) != NULL)
86  {
87  int i = 0, j = 0;
88 
89  /* erase all lines including symbol '#' and all blank lines */
90  while (temp_line[i] != '\n')
91  {
92  if (temp_line[i] == '#') {j++;}
93  i++;
94  }
95 
96  if (j == 0 && strlen(temp_line) != 1)
97  {
98  fprintf (temp_fp, "%s", temp_line);
99  }
100  }
101 
102  /* close input and temp file */
103  fclose (input_fp);
104  fclose (temp_fp);
105  }
106  }
107 
108  return true;
109 }
bool clear_comments(char *input, char *temp)
Clear all commented sentences in a islec database file, and then write databank to a temporary file n...
#define STRING_LENGTH
Defined length of a readed line used by the C library function fgets()