/* Copyright (c) 2022 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "document/DocumentList.h" #include #include #include #define stemobject_implementation DocumentList stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); DocumentList * DocumentList_create(void) { stemobject_create_implementation(init) } bool DocumentList_init(DocumentList * self) { call_super(init, self); self->documentCount = 0; self->documents = NULL; return true; } void DocumentList_dispose(DocumentList * self) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { free(self->documents[documentIndex].filePath); } free(self->documents); call_super_virtual(dispose, self); } void DocumentList_addDocument(DocumentList * self, const char * filePath, unsigned int pruneToCount) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { if (!strcmp(self->documents[documentIndex].filePath, filePath)) { if (documentIndex < self->documentCount - 1) { DocumentList_reorderDocument(self, documentIndex, self->documentCount - 1); } if (pruneToCount > 0) { DocumentList_pruneToCount(self, pruneToCount); } return; } } if (pruneToCount > 0 && self->documentCount + 1 > pruneToCount) { DocumentList_pruneToCount(self, pruneToCount - 1); } else { self->documents = realloc(self->documents, (self->documentCount + 1) * sizeof(*self->documents)); } self->documents[self->documentCount].filePath = strdup(filePath); self->documents[self->documentCount].fileExists = false; self->documentCount++; } void DocumentList_removeAllDocuments(DocumentList * self) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { free(self->documents[documentIndex].filePath); } free(self->documents); self->documentCount = 0; self->documents = NULL; } void DocumentList_removeDeletedDocuments(DocumentList * self) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { if (!self->documents[documentIndex].fileExists) { DocumentList_removeDocumentAtIndex(self, documentIndex); documentIndex--; } } } void DocumentList_removeDocumentAtIndex(DocumentList * self, unsigned int documentIndex) { if (documentIndex < self->documentCount) { free(self->documents[documentIndex].filePath); self->documentCount--; for (; documentIndex < self->documentCount; documentIndex++) { self->documents[documentIndex] = self->documents[documentIndex + 1]; } } } void DocumentList_removeDocumentWithFilePath(DocumentList * self, const char * filePath) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { if (!strcmp(self->documents[documentIndex].filePath, filePath)) { free(self->documents[documentIndex].filePath); self->documentCount--; for (; documentIndex < self->documentCount; documentIndex++) { self->documents[documentIndex] = self->documents[documentIndex + 1]; } break; } } } void DocumentList_pruneToCount(DocumentList * self, unsigned int countMax) { if (countMax < self->documentCount) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount - countMax; documentIndex++) { free(self->documents[documentIndex].filePath); self->documents[documentIndex] = self->documents[documentIndex + self->documentCount - countMax]; } for (unsigned int documentIndex = self->documentCount - countMax; documentIndex < countMax; documentIndex++) { self->documents[documentIndex] = self->documents[documentIndex + self->documentCount - countMax]; } self->documentCount = countMax; } } void DocumentList_reorderDocument(DocumentList * self, unsigned int fromIndex, unsigned int toIndex) { if (fromIndex < self->documentCount && toIndex < self->documentCount && fromIndex != toIndex) { DocumentListEntry entry = self->documents[fromIndex]; int direction = (toIndex > fromIndex) * 2 - 1; for (unsigned int documentIndex = fromIndex; documentIndex != toIndex; documentIndex += direction) { self->documents[documentIndex] = self->documents[documentIndex + direction]; } self->documents[toIndex] = entry; } } void DocumentList_validateFilesExist(DocumentList * self) { for (unsigned int documentIndex = 0; documentIndex < self->documentCount; documentIndex++) { struct stat statbuf; self->documents[documentIndex].fileExists = !stat(self->documents[documentIndex].filePath, &statbuf); } }