Skip to content

Commit 9963772

Browse files
committed
Refactored text search in file
1 parent e68abd0 commit 9963772

File tree

2 files changed

+64
-62
lines changed

2 files changed

+64
-62
lines changed

src/sys/xfs.c

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,83 +1172,83 @@ static int XFile_CheckCriteria(xfile_search_t *pSearch, const char *pPath, const
11721172
if (pSearch->bInsensitive) xstrcase((char*)pBuffer, XSTR_LOWER);
11731173
int nPosit = xstrsrc(pBuffer, pSearch->sText);
11741174

1175-
if (nPosit >= 0)
1175+
if (nPosit < 0)
11761176
{
1177-
if (pSearch->bSearchLines)
1177+
XByteBuffer_Clear(&buffer);
1178+
return XSTDNON;
1179+
}
1180+
1181+
if (pSearch->bSearchLines)
1182+
{
1183+
xarray_t *pArr = xstrsplite(pBuffer, "\n");
1184+
if (pArr != NULL)
11781185
{
1179-
xarray_t *pArr = xstrsplite(pBuffer, "\n");
1180-
if (pArr != NULL)
1186+
size_t i = 0;
1187+
for (i = 0; i < pArr->nUsed; i++)
11811188
{
1182-
size_t i = 0;
1183-
for (i = 0; i < pArr->nUsed; i++)
1189+
char *pLine = (char*)XArray_GetData(pArr, i);
1190+
if (xstrsrc(pLine, pSearch->sText) >= 0)
11841191
{
1185-
char *pLine = (char*)XArray_GetData(pArr, i);
1186-
if (xstrsrc(pLine, pSearch->sText) >= 0)
1192+
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1193+
if (pEntry == NULL)
1194+
{
1195+
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1196+
XByteBuffer_Clear(&buffer);
1197+
XArray_Destroy(pArr);
1198+
return XSTDERR;
1199+
}
1200+
1201+
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1202+
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1203+
pEntry->nLineNum = i + 1;
1204+
1205+
if (XFile_SearchCallback(pSearch, pEntry) < 0)
11871206
{
1188-
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1189-
if (pEntry == NULL)
1190-
{
1191-
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1192-
XByteBuffer_Clear(&buffer);
1193-
XArray_Destroy(pArr);
1194-
return XSTDERR;
1195-
}
1196-
1197-
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1198-
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1199-
pEntry->nLineNum = i + 1;
1200-
1201-
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1202-
{
1203-
XByteBuffer_Clear(&buffer);
1204-
XArray_Destroy(pArr);
1205-
return XSTDERR;
1206-
}
1207+
XByteBuffer_Clear(&buffer);
1208+
XArray_Destroy(pArr);
1209+
return XSTDERR;
12071210
}
12081211
}
12091212
}
1210-
1211-
XByteBuffer_Clear(&buffer);
1212-
XArray_Destroy(pArr);
1213-
return XSTDNON;
12141213
}
1215-
else
1216-
{
1217-
while (nPosit && pBuffer[nPosit] != '\n') nPosit--;
1218-
if (pBuffer[nPosit] == '\n') nPosit++;
12191214

1220-
int nEnd = xstrsrc(&pBuffer[nPosit], "\n");
1221-
if (nEnd > 0)
1222-
{
1223-
char sLine[XSTR_MAX];
1224-
xstrncpys(sLine, sizeof(sLine), &pBuffer[nPosit], nEnd);
1215+
XByteBuffer_Clear(&buffer);
1216+
XArray_Destroy(pArr);
1217+
return XSTDNON;
1218+
}
12251219

1226-
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1227-
if (pEntry == NULL)
1228-
{
1229-
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1230-
XByteBuffer_Clear(&buffer);
1231-
return XSTDERR;
1232-
}
1220+
while (nPosit && pBuffer[nPosit] != '\n') nPosit--;
1221+
if (pBuffer[nPosit] == '\n') nPosit++;
12331222

1234-
char *pLine = sLine;
1235-
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1236-
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1223+
int nEnd = xstrsrc(&pBuffer[nPosit], "\n");
1224+
if (nEnd > 0)
1225+
{
1226+
char sLine[XSTR_MAX];
1227+
xstrncpys(sLine, sizeof(sLine), &pBuffer[nPosit], nEnd);
12371228

1238-
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1239-
{
1240-
XByteBuffer_Clear(&buffer);
1241-
return XSTDERR;
1242-
}
1229+
xfile_entry_t *pEntry = XFile_NewEntry(pName, pPath, pStat);
1230+
if (pEntry == NULL)
1231+
{
1232+
XFile_ErrorCallback(pSearch, "Failed to alloc entry: %s", pPath);
1233+
XByteBuffer_Clear(&buffer);
1234+
return XSTDERR;
1235+
}
12431236

1244-
XByteBuffer_Clear(&buffer);
1245-
return XSTDNON;
1246-
}
1237+
char *pLine = sLine;
1238+
while (*pLine && isspace((unsigned char)*pLine)) pLine++;
1239+
xstrncpy(pEntry->sLine, sizeof(pEntry->sLine), pLine);
1240+
1241+
if (XFile_SearchCallback(pSearch, pEntry) < 0)
1242+
{
1243+
XByteBuffer_Clear(&buffer);
1244+
return XSTDERR;
12471245
}
1246+
1247+
XByteBuffer_Clear(&buffer);
1248+
return XSTDNON;
12481249
}
12491250

12501251
XByteBuffer_Clear(&buffer);
1251-
if (nPosit < 0) return XSTDNON;
12521252
}
12531253

12541254
return XSTDOK;

tools/xsrc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,11 @@ static void XSearch_DisplayEntry(xfile_search_t *pSearch, xfile_entry_t *pEntry)
382382
/* Do not display additional info if verbose is not set */
383383
if (!((xsearch_args_t*)pSearch->pUserCtx)->bVerbose)
384384
{
385-
if (!pEntry->nLineNum && !xstrused(sLine)) xlog("%s%s%s", sEntry, pArrow, sLinkPath);
386-
else if (xstrused(pEntry->sLine) && xstrused(sLine)) xlog("%s: %s", sEntry, sLine);
387-
else xlog("%s:%s%d%s %s", sEntry, XSTR_FMT_BOLD, pEntry->nLineNum, XSTR_FMT_RESET, sLine);
385+
if (pEntry->nLineNum && xstrused(sLine))
386+
xlog("%s:%s%d%s %s", sEntry, XSTR_FMT_BOLD, pEntry->nLineNum, XSTR_FMT_RESET, sLine);
387+
else if (xstrused(sLine)) xlog("%s: %s", sEntry, sLine);
388+
else xlog("%s%s%s", sEntry, pArrow, sLinkPath);
389+
388390
return;
389391
}
390392

0 commit comments

Comments
 (0)