Anpassung input.c nach Unit Tests
This commit is contained in:
1 parent
9e53289322
commit
852ae19472
1 file changed
+47
-12
+47
-12
@@ -7,36 +7,71 @@
|
|||||||
char* readSingleWord(FILE *file, char *destination, int maxLen)
|
char* readSingleWord(FILE *file, char *destination, int maxLen)
|
||||||
{
|
{
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
|
int ch;
|
||||||
|
int bufIndex = 0;
|
||||||
|
|
||||||
// %255s schützt vor Pufferüberläufen des 256-Zeichen-buffers
|
// Alle Trennzeichen überspringen, bis ein echter Buchstabe kommt
|
||||||
if (fscanf(file, "%255s", buffer) == 1)
|
while ((ch = fgetc(file)) != EOF)
|
||||||
{
|
{
|
||||||
|
// Prüfen der Buchstaben
|
||||||
|
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
|
||||||
|
{
|
||||||
|
// Wenn Kleinbuchstabe, in Großbuchstabe umwandeln
|
||||||
|
if (ch >= 'a' && ch <= 'z')
|
||||||
|
{
|
||||||
|
ch = ch - 32;
|
||||||
|
}
|
||||||
|
buffer[bufIndex] = ch;
|
||||||
|
bufIndex++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == EOF)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Den Rest des Wortes einlesen (bricht bei Kommas, Leerzeichen etc. ab)
|
||||||
|
while ((ch = fgetc(file)) != EOF && ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')))
|
||||||
|
{
|
||||||
|
if (bufIndex < 255)
|
||||||
|
{
|
||||||
|
if (ch >= 'a' && ch <= 'z')
|
||||||
|
{
|
||||||
|
ch = ch - 32;
|
||||||
|
}
|
||||||
|
buffer[bufIndex] = ch;
|
||||||
|
bufIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zwischenspeicher abschließen
|
||||||
|
buffer[bufIndex] = '\0';
|
||||||
|
|
||||||
|
// Sicheres Kopieren in das Ziel-Array
|
||||||
strncpy(destination, buffer, maxLen - 1);
|
strncpy(destination, buffer, maxLen - 1);
|
||||||
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen
|
destination[maxLen - 1] = '\0';
|
||||||
|
|
||||||
return destination; // Rückgabe bei Erfolg
|
return destination;
|
||||||
}
|
}
|
||||||
return NULL; // Rückgabe bei Dateiende
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
|
// Schleife läuft, solange noch Platz im Array ist
|
||||||
while (count < maxWordCount)
|
while (count < maxWordCount)
|
||||||
{
|
{
|
||||||
// Aufruf Hilfsfunktion mit Arraygröße als Parameter
|
|
||||||
if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL)
|
if (readSingleWord(file, words[count], MAX_WORD_LEN) != NULL)
|
||||||
{
|
{
|
||||||
if (strlen(words[count]) > 0)
|
if (strlen(words[count]) > 0) // Prüfen, ob das Wort nicht leer ist
|
||||||
{
|
{
|
||||||
count++;
|
count++; //nächstes Wort
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
break; // Ende der Datei erreicht
|
break; // Bei Dateiende wird die Schleife abgebrochen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
|
|||||||
Reference in new issue
Block a user