LibRCG  3.1.1
util.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include "util.h"
14 
19 #define BUFSIZE 32
20 
21 int rgets(char** str)
22 {
23  char* str1,* str2;
24  int tmp;
25  int length,count,result=0;
26  if(!str) result=-2;
27  else
28  {
29  length=BUFSIZE;
30  str1=malloc((length+1)*sizeof(char));
31  if(!str)
32  {
33  *str=NULL;
34  result=-1;
35  }
36  for(count=0;result>=0&&(tmp=getchar())!='\n';count++)
37  {
38  if(count>=length)
39  {
40  length*=2;
41  str2=realloc(str1,length+1);
42  if(!str2)
43  {
44  free(str1);
45  *str=NULL;
46  result=-1;
47  }
48  else str1=str2;
49  }
50  str1[count]=tmp;
51  }
52  str1[count]='\0';
53  *str=str1;
54  result=count;
55  }
56  return result;
57 }
58 
59 //==============================================================================
60 
61 int rgetsEOF(char** str)
62 {
63  char* str1,* str2;
64  int tmp;
65  int length,count,result=0;
66  if(!str) result=-2;
67  else
68  {
69  length=BUFSIZE;
70  str1=malloc((length+1)*sizeof(char));
71  if(!str1)
72  {
73  *str=NULL;
74  result=-1;
75  }
76  for(count=0;result>=0&&(tmp=getchar())!=EOF;count++)
77  {
78  if(count>=length)
79  {
80  length*=2;
81  str2=realloc(str1,length+1);
82  if(!str2)
83  {
84  free(str1);
85  *str=NULL;
86  result=-1;
87  }
88  else str1=str2;
89  }
90  str1[count]=tmp;
91  }
92  str1[count]='\0';
93  *str=str1;
94  result=count;
95  }
96  return result;
97 }
98 
99 //==============================================================================
100 
101 int rngets(char *str,int dim)
102 {
103  int length;
104  char *ret;
105  str[dim-1]=str[dim-2]=1;
106  ret=fgets(str,dim,stdin);
107  if(ret==NULL||(str[dim-1]=='\0'&&str[dim-2]!='\n'))
108  {
109  while(getchar()!='\n');
110  length=-1;
111  }
112  else
113  {
114  length=strlen(str)-1;
115  str[length]='\0';
116  }
117  return length;
118 }
119 
120 //==============================================================================
121 
122 int getRandom(int min,int max)
123 {
124  static time_t seed = 0;
125  int result;
126  if(max<min) result=0;
127  else
128  {
129  if(!seed)
130  {
131  time(&seed);
132  srand((unsigned)seed);
133  }
134  result=min+rand()%(max-min+1);
135  }
136  return result;
137 }
138 
139 //==============================================================================
140 
155 int merge(void* vals[],int begin,int middle,int end,
156  int(*comp)(void*,void*))
157 {
158  int i,j,k,result=0;
159  int size=end-begin+1;
160  int sizel=middle-begin;
161  void** copy=malloc(size*sizeof(void*));
162  if(!copy) result=1;
163  else
164  {
165  for(i=0,k=begin;i<size;i++,k++)
166  {
167  copy[i]=vals[k];
168  }
169  for(i=0,j=sizel,k=begin;k<=end;k++)
170  {
171  if(j>=size||(i<sizel&&((*comp)(copy[i],copy[j])<0)))
172  {
173  vals[k]=copy[i];
174  i++;
175  }
176  else
177  {
178  vals[k]=copy[j];
179  j++;
180  }
181  }
182  }
183  return result;
184 }
185 
186 //==============================================================================
187 
188 int mergeSort(void* vals[],int begin,int end,int(*comp)(void*,void*))
189 {
190  int m,result=0;
191  if(begin<end)
192  {
193  m=(begin+end)/2;
194  result+=mergeSort(vals,begin,m,*comp);
195  result+=mergeSort(vals,m+1,end,*comp);
196  result+=merge(vals,begin,m+1,end,*comp);
197  }
198  return result;
199 }
int rgets(char **str)
Reads a line from the stdin.
Definition: util.c:21
int mergeSort(void *vals[], int begin, int end, int(*comp)(void *, void *))
Orders an array into ascending order, based on a given comparison function.
Definition: util.c:188
int rgetsEOF(char **str)
Reads a text from the stdin.
Definition: util.c:61
int merge(void *vals[], int begin, int middle, int end, int(*comp)(void *, void *))
Merges two ordered parts of an array of pointers, based on a given comparison function.
Definition: util.c:155
int rngets(char *str, int dim)
Reads a line from the stdin.
Definition: util.c:101
Implementation of some utility functions.
#define BUFSIZE
Size of the initial buffer to be used in functions rgets and rgetsEOF.
Definition: util.c:19
int getRandom(int min, int max)
Generates a random number in a given interval.
Definition: util.c:122

LibRCG © 2004-2015   Rui Carlos Gonçalves