Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #57 - Recover responsiveness to stdin and upgrade strOrFile #140

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0086563
Initial tests indicate I am successfully ungetting content to the ung…
wbkboyer Jan 24, 2025
72509a7
Implemented sf_ReadInteger() and confirmed AdjList version of nauty e…
wbkboyer Jan 27, 2025
60ce360
Want to make sure we only allow valid negative integers and bail out …
wbkboyer Jan 28, 2025
a2cab2e
Refactored to use stack for ungetBuf
wbkboyer Jan 29, 2025
c44a9ff
Merge branch 'master' into Issue57-strOrFileHandleStdin
wbkboyer Jan 30, 2025
12d7aec
Confirmed reading LEDA graph works by transforming nauty example (in …
wbkboyer Jan 30, 2025
271e191
Added sf_ReadSingleDigit() to help _ReadAdjMatrix(), updated paramete…
wbkboyer Jan 31, 2025
75406da
Added new function to g6-read-iterator.c, _ReadGraphFromG6StrOrFile()…
wbkboyer Jan 31, 2025
d02c8b3
-test passes, indicating reading from both pFile and theStr is succes…
wbkboyer Feb 4, 2025
c6af035
Debugging required; seems to be breaking down on -test for in-memory…
wbkboyer Feb 6, 2025
a74a1f9
Fixed bug where G6WriteIterator cleanup meant that outputContainer wa…
wbkboyer Feb 6, 2025
8782df0
-test passes; must still test to ensure RandomGraphs() and TestAllGra…
wbkboyer Feb 7, 2025
8e1714a
Changed function signatures of sf_ReadSkipWhitespace() and sf_ReadSki…
wbkboyer Feb 7, 2025
c456e80
Resolving some warnings from MSVC cl and MinGW-w64 UCRT gcc
wbkboyer Feb 10, 2025
ca03869
Merge branch 'master' into Issue57-strOrFileHandleStdin
wbkboyer Feb 10, 2025
2fcc012
Refactor so that extraData is now a null-terminated char * (rather th…
wbkboyer Feb 10, 2025
1540b64
Added newline to end of hand-written LEDA translation of nauty_exampl…
wbkboyer Feb 10, 2025
74c3339
Minor fix - need to make sure outputStr is not NULL before trying to …
wbkboyer Feb 11, 2025
8a25a54
Return NULL from sf_New() if containerType is OUTPUT_CONTAINER and ye…
wbkboyer Feb 12, 2025
b285c85
Minor bugfix: when stdout or stderr chosen as output stream, incorrec…
wbkboyer Feb 13, 2025
c8fe178
Fixed bug where accessing SpecificGraph() from menu allowed user to s…
wbkboyer Feb 13, 2025
c3f34f5
Addressing PR comments; all manual tests plus command-line -test pass
wbkboyer Feb 14, 2025
7f4bc04
Addressing PR comments:
wbkboyer Feb 14, 2025
c2f48da
Fixed issue with incorrect exitCode being returned from _ReadGraphFro…
wbkboyer Feb 20, 2025
cf568bf
Changed amount of stack-allocated memory for containing the numberStr…
wbkboyer Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions c/graphLib/io/g6-read-iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ int _ReadGraphFromG6String(graphP pGraphToRead, char *g6EncodedString)

int _ReadGraphFromG6StrOrFile(graphP pGraphToRead, strOrFileP g6InputContainer)
{
int exitCode = OK;

G6ReadIterator *pG6ReadIterator = NULL;

if (sf_ValidateStrOrFile(g6InputContainer) != OK)
Expand All @@ -704,14 +706,17 @@ int _ReadGraphFromG6StrOrFile(graphP pGraphToRead, strOrFileP g6InputContainer)
return NOTOK;
}

if (readGraphUsingG6ReadIterator(pG6ReadIterator) != OK)
exitCode = readGraphUsingG6ReadIterator(pG6ReadIterator);
if (exitCode != OK)
ErrorMessage("Unable to read graph from .g6 read iterator.\n");

if (endG6ReadIteration(pG6ReadIterator) != OK)
exitCode = endG6ReadIteration(pG6ReadIterator);
if (exitCode != OK)
ErrorMessage("Unable to end G6ReadIterator.\n");

if (freeG6ReadIterator(&pG6ReadIterator) != OK)
exitCode = freeG6ReadIterator(&pG6ReadIterator);
if (exitCode != OK)
ErrorMessage("Unable to free G6ReadIterator.\n");

return OK;
return exitCode;
}
14 changes: 7 additions & 7 deletions c/graphLib/io/graphIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ int _ReadAdjList(graphP theGraph, strOrFileP inputContainer)

int _ReadLEDAGraph(graphP theGraph, strOrFileP inputContainer)
{
char Line[256];
char Line[MAXLINE + 1];
int N = -1;
int graphType, M, m, u, v, ErrorCode;
int zeroBasedOffset = gp_GetFirstVertex(theGraph) == 0 ? 1 : 0;
Expand Down Expand Up @@ -350,7 +350,7 @@ int _ReadLEDAGraph(graphP theGraph, strOrFileP inputContainer)
return NOTOK;

for (v = gp_GetFirstVertex(theGraph); gp_VertexInRange(theGraph, v); v++)
if (sf_fgets(Line, 255, inputContainer) == NULL)
if (sf_fgets(Line, MAXLINE, inputContainer) == NULL)
return NOTOK;

/* Read the number of edges */
Expand Down Expand Up @@ -454,12 +454,12 @@ int _ReadGraph(graphP theGraph, strOrFileP inputContainer)
{
int RetVal = OK;
bool extraDataAllowed = false;
char lineBuff[256];
char lineBuff[MAXLINE + 1];

if (sf_ValidateStrOrFile(inputContainer) != OK)
return NOTOK;

if (sf_fgets(lineBuff, 255, inputContainer) == NULL)
if (sf_fgets(lineBuff, MAXLINE, inputContainer) == NULL)
{
sf_Free(&inputContainer);
return NOTOK;
Expand Down Expand Up @@ -518,7 +518,7 @@ int _ReadGraph(graphP theGraph, strOrFileP inputContainer)
else
{
// FIXME: how do I distinguish between "there's no more content on input stream" and "I've hit an error state"
while (sf_fgets(lineBuff, 255, inputContainer) != NULL)
while (sf_fgets(lineBuff, MAXLINE, inputContainer) != NULL)
{
if (sb_ConcatString(extraData, lineBuff) != OK)
{
Expand Down Expand Up @@ -723,8 +723,8 @@ char _GetVertexObstructionTypeChar(graphP theGraph, int v)
int _WriteDebugInfo(graphP theGraph, strOrFileP outputContainer)
{
int v, e, EsizeOccupied;
char lineBuf[256];
memset(lineBuf, '\0', 256 * sizeof(char));
char lineBuf[MAXLINE + 1];
memset(lineBuf, '\0', (MAXLINE + 1) * sizeof(char));

if (theGraph == NULL || sf_ValidateStrOrFile(outputContainer) != OK)
return NOTOK;
Expand Down
14 changes: 7 additions & 7 deletions c/graphLib/io/strOrFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ strOrFileP sf_New(char *theStr, char *fileName, char *ioMode)
}
else if (strcmp(fileName, "stdout") == 0)
{
if (ioMode != NULL && strncmp(ioMode, READTEXT, strlen(READTEXT)) != 0)
if (ioMode != NULL && strncmp(ioMode, WRITETEXT, strlen(WRITETEXT)) != 0)
{
sf_Free(&theStrOrFile);
return NULL;
Expand All @@ -74,7 +74,7 @@ strOrFileP sf_New(char *theStr, char *fileName, char *ioMode)
}
else if (strcmp(fileName, "stderr") == 0)
{
if (ioMode != NULL && strncmp(ioMode, READTEXT, strlen(READTEXT)) != 0)
if (ioMode != NULL && strncmp(ioMode, WRITETEXT, strlen(WRITETEXT)) != 0)
{
sf_Free(&theStrOrFile);
return NULL;
Expand Down Expand Up @@ -300,7 +300,7 @@ int sf_ReadInteger(int *intToRead, strOrFileP theStrOrFile)
theStrOrFile->containerType != INPUT_CONTAINER)
return NOTOK;

char intCandidateStr[MAXCHARSFOR32BITINT];
char intCandidateStr[MAXCHARSFOR32BITINT + 1];
memset(intCandidateStr, '\0', MAXCHARSFOR32BITINT * sizeof(char));

int intCandidate = 0;
Expand All @@ -309,7 +309,7 @@ int sf_ReadInteger(int *intToRead, strOrFileP theStrOrFile)
int intCandidateIndex = 0;
while ((currChar = sf_getc(theStrOrFile)) != EOF)
{
if (intCandidateIndex > (MAXCHARSFOR32BITINT - 2))
if (intCandidateIndex > (MAXCHARSFOR32BITINT - 1))
{
exitCode = NOTOK;
break;
Expand Down Expand Up @@ -391,10 +391,10 @@ int sf_ReadSkipInteger(strOrFileP theStrOrFile)

int sf_ReadSkipLineRemainder(strOrFileP theStrOrFile)
{
char lineRemainderToSkip[255];
memset(lineRemainderToSkip, '\0', 255);
char lineRemainderToSkip[MAXLINE + 1];
memset(lineRemainderToSkip, '\0', (MAXLINE + 1));

if (sf_fgets(lineRemainderToSkip, 254, theStrOrFile) == NULL)
if (sf_fgets(lineRemainderToSkip, MAXLINE, theStrOrFile) == NULL)
return NOTOK;

return OK;
Expand Down
8 changes: 4 additions & 4 deletions c/graphLib/lowLevelUtils/apiutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ extern "C"
#define MAXLINE 1024

// N.B. Every time you're trying to read a 32-bit int from a string,
// you should only need this many characters: an optional '-',
// followed by 10 digits (max signed int value is 2,147,483,647) and
// a null-terminator)
#define MAXCHARSFOR32BITINT 12
// you should only need to read this many characters: an optional '-',
// followed by 10 digits (max signed int value is 2,147,483,647). One
// must always allocate an additional byte for the null-terminator!
#define MAXCHARSFOR32BITINT 11

extern int quietMode;

Expand Down
76 changes: 22 additions & 54 deletions c/planarityApp/planarityMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ void TransformGraphMenu(void)
infileName[0] = '\0';
char outfileName[MAXLINE + 1];
outfileName[0] = '\0';
char *outputStr = NULL;
char outputFormat = '\0';
char commandStr[4];
commandStr[0] = '\0';
Expand All @@ -119,16 +118,19 @@ void TransformGraphMenu(void)
fflush(stdin);
scanf(fileNameFormat, infileName);

if (strncmp(infileName, "stdin", 5) == 0)
if (strncmp(infileName, "stdin", strlen("stdin")) == 0)
{
ErrorMessage("stdin not supported from menu.\n");
ErrorMessage("\n\tPlease choose an input file path: stdin not supported from menu.\n\n");
infileName[0] = '\0';
}
} while (strlen(infileName) == 0);

Prompt("Enter output filename, or press return to output to console:\n");
fflush(stdin);
scanf(fileNameFormat, outfileName);
do
{
Prompt("Enter output filename, or type \"stdout\" to output to console:\n");
fflush(stdin);
scanf(fileNameFormat, outfileName);
} while (strlen(outfileName) == 0);

do
{
Expand All @@ -141,30 +143,9 @@ void TransformGraphMenu(void)
sprintf(commandStr, "-%c", outputFormat);
} while (strlen(commandStr) == 0);

if (strlen(outfileName) == 0)
{
Result = TransformGraph(commandStr, infileName, NULL, NULL, NULL, &outputStr);
if (Result != OK || outputStr == NULL)
ErrorMessage("Failed to perform transformation.\n");
else
{
Message("Output:\n");
Message(outputStr);
Message("\n");
}
}
else
{
Result = TransformGraph(commandStr, infileName, NULL, NULL, outfileName, NULL);
if (Result != OK)
ErrorMessage("Failed to perform transformation.\n");
}

if (outputStr != NULL)
{
free(outputStr);
outputStr = NULL;
}
Result = TransformGraph(commandStr, infileName, NULL, NULL, outfileName, NULL);
if (Result != OK)
ErrorMessage("Failed to perform transformation.\n");
}

void TestAllGraphsMenu(void)
Expand All @@ -175,7 +156,6 @@ void TestAllGraphsMenu(void)
infileName[0] = '\0';
char outfileName[MAXLINE + 1];
outfileName[0] = '\0';
char *outputStr = NULL;
char algorithmSpecifier = '\0';
char commandStr[3];
commandStr[0] = '\0';
Expand All @@ -190,16 +170,19 @@ void TestAllGraphsMenu(void)
fflush(stdin);
scanf(fileNameFormat, infileName);

if (strncmp(infileName, "stdin", 5) == 0)
if (strncmp(infileName, "stdin", strlen("stdin")) == 0)
{
ErrorMessage("stdin not supported from menu.\n");
ErrorMessage("\n\tPlease choose an input file path: stdin not supported from menu.\n\n");
infileName[0] = '\0';
}
} while (strlen(infileName) == 0);

Prompt("Enter output filename, or press return to output to console:\n");
fflush(stdin);
scanf(fileNameFormat, outfileName);
do
{
Prompt("Enter output filename, or type \"stdout\" to output to console:\n");
fflush(stdin);
scanf(fileNameFormat, outfileName);
} while (strlen(outfileName) == 0);

do
{
Expand All @@ -213,22 +196,7 @@ void TestAllGraphsMenu(void)
sprintf(commandStr, "-%c", algorithmSpecifier);
} while (strlen(commandStr) == 0);

if (strlen(outfileName) == 0)
{
Result = TestAllGraphs(commandStr, infileName, NULL, &outputStr);
if (Result != OK || outputStr == NULL)
ErrorMessage("Algorithm test on all graphs in .g6 input file failed.\n");
else
{
Message("Output:\n");
Message(outputStr);
Message("\n");
}
}
else
{
Result = TestAllGraphs(commandStr, infileName, outfileName, NULL);
if (Result != OK)
ErrorMessage("Algorithm test on all graphs in .g6 input file failed.\n");
}
Result = TestAllGraphs(commandStr, infileName, outfileName, NULL);
if (Result != OK)
ErrorMessage("Algorithm test on all graphs in .g6 input file failed.\n");
}
2 changes: 1 addition & 1 deletion c/planarityApp/planarityRandomGraphs.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ int RandomGraph(char command, int extraEdges, int numVertices, char *outfileName

if (tolower(saveEdgeListFormat) == 'y')
{
char theFileName[256];
char theFileName[MAXLINE + 1];

if (extraEdges > 0)
strcpy(theFileName, "nonPlanarEdgeList.txt");
Expand Down
19 changes: 17 additions & 2 deletions c/planarityApp/planaritySpecificGraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,23 @@ int SpecificGraph(
// Get the filename of the graph to test
if (inputStr == NULL)
{
if ((infileName = ConstructInputFilename(infileName)) == NULL)
return NOTOK;
if (infileName != NULL)
{
if ((infileName = ConstructInputFilename(infileName)) == NULL)
return NOTOK;
}
else
{
do
{
infileName = ConstructInputFilename(infileName);
if (infileName != NULL && strncmp(infileName, "stdin", strlen("stdin")) == 0)
{
ErrorMessage("\n\tPlease choose an input file path: stdin not supported from menu.\n\n");
infileName = NULL;
}
} while (infileName == NULL || strlen(infileName) == 0);
}
}

// Create the graph and, if needed, attach the correct algorithm to it
Expand Down
21 changes: 16 additions & 5 deletions c/planarityApp/planarityUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,18 @@ char *ConstructInputFilename(char *infileName)
{
if (infileName == NULL)
{
Prompt("Enter graph file name: ");
fflush(stdin);
scanf(" %s", theFileName);
do
{
Prompt("Enter graph file name: ");
fflush(stdin);
scanf(" %s", theFileName);

if (!strchr(theFileName, '.'))
strcat(theFileName, ".txt");
if (strncmp(theFileName, "stdin", strlen("stdin")) != 0 && !strchr(theFileName, '.'))
{
Message("Graph file name does not have extension; automatically appending \".txt\".\n");
strcat(theFileName, ".txt");
}
} while (strlen(theFileName) == 0);
}
else
{
Expand All @@ -537,6 +543,11 @@ char *ConstructInputFilename(char *infileName)
ErrorMessage("Filename is too long");
return NULL;
}
else if (strlen(infileName) == 0)
{
ErrorMessage("Filename is empty");
return NULL;
}
strcpy(theFileName, infileName);
}

Expand Down